Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const Hapi = require('hapi');
- let uuid = 1; // Use seq instead of proper unique identifiers for demo only
- const users = {
- john: {
- id: 'john',
- password: 'password',
- name: 'John Doe'
- }
- };
- const home = function (request, reply) {
- reply('<html><head><title>Login page</title></head><body><h3>Welcome ' +
- '!</h3><br/><form method="get" action="/logout">' +
- '<input type="submit" value="Logout">' +
- '</form></body></html>');
- };
- const login = function (request, reply) {
- if (request.auth.isAuthenticated) {
- return reply.redirect('/');
- }
- let message = '';
- let account = null;
- if (request.method === 'post') {
- if (!request.payload.username ||
- !request.payload.password) {
- message = 'Missing username or password';
- }
- else {
- account = users[request.payload.username];
- if (!account ||
- account.password !== request.payload.password) {
- message = 'Invalid username or password';
- }
- }
- }
- if (request.method === 'get' ||
- message) {
- return reply('<html><head><title>Login page</title></head><body>' +
- (message ? '<h3>' + message + '</h3><br/>' : '') +
- '<form method="post" action="/login">' +
- 'Username: <input type="text" name="username"><br>' +
- 'Password: <input type="password" name="password"><br/>' +
- '<input type="submit" value="Login"></form></body></html>');
- }
- const sid = String(++uuid);
- request.server.app.cache.set(sid, { account: account }, 0, (err) => {
- if (err) {
- reply(err);
- }
- request.cookieAuth.set({ sid: sid });
- return reply.redirect('/');
- });
- };
- const logout = function (request, reply) {
- request.cookieAuth.clear();
- return reply.redirect('/');
- };
- const server = new Hapi.Server();
- server.connection({ port: 8000 });
- server.register(require('hapi-auth-cookie'), (err) => {
- if (err) {
- throw err;
- }
- const cache = server.cache({ segment: 'sessions', expiresIn: 3 * 24 * 60 * 60 * 1000 });
- server.app.cache = cache;
- server.auth.strategy('session', 'cookie', true, {
- password: 'password-should-be-32-characters',
- cookie: 'sid-example',
- redirectTo: '/login',
- isSecure: false,
- validateFunc: function (request, session, callback) {
- cache.get(session.sid, (err, cached) => {
- if (err) {
- return callback(err, false);
- }
- if (!cached) {
- return callback(null, false);
- }
- return callback(null, true, cached.account);
- });
- }
- });
- server.route([
- { method: 'GET', path: '/', config: { handler: home, auth: { mode: 'optional' }, plugins: { 'hapi-auth-cookie': { redirectTo: false } } } },
- { method: ['GET', 'POST'], path: '/login', config: { handler: login, auth: { mode: 'try' }, plugins: { 'hapi-auth-cookie': { redirectTo: false } } } },
- { method: 'GET', path: '/logout', config: { handler: logout } }
- ]);
- server.start(() => {
- console.log('Server ready');
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement