'use strict';
const Cookie = require('..');
const Hapi = require('@hapi/hapi');
const internals = {
uuid: 1 // Use seq instead of proper unique identifiers for demo only
};
internals.users = {
john: {
id: 'john',
password: 'password',
name: 'John Doe'
}
};
internals.home = function (request, h) {
return '
Login pageWelcome ' +
request.auth.credentials.name +
'!
';
};
internals.login = async function (request, h) {
if (request.auth.isAuthenticated) {
return h.redirect('/');
}
let message = '';
let account = null;
if (request.method === 'post') {
if (!request.payload.username ||
!request.payload.password) {
message = 'Missing username or password';
}
else {
account = internals.users[request.payload.username];
if (!account ||
account.password !== request.payload.password) {
message = 'Invalid username or password';
}
}
}
if (request.method === 'get' ||
message) {
return 'Login page' +
(message ? '' + message + '
' : '') +
'';
}
const sid = String(++internals.uuid);
await request.server.app.cache.set(sid, { account }, 0);
request.cookieAuth.set({ sid });
return h.redirect('/');
};
internals.logout = function (request, h) {
request.server.app.cache.drop(request.state['sid-example'].sid);
request.cookieAuth.clear();
return h.redirect('/');
};
internals.start = async function () {
const server = Hapi.server({ port: 8000 });
await server.register(Cookie);
const cache = server.cache({ segment: 'sessions', expiresIn: 3 * 24 * 60 * 60 * 1000 });
server.app.cache = cache;
server.auth.strategy('session', 'cookie', {
cookie: {
name: 'sid-example',
password: 'password-should-be-32-characters',
isSecure: false
},
redirectTo: '/login',
validate: async (request, session) => {
const cached = await cache.get(session.sid);
const out = {
isValid: !!cached
};
if (out.isValid) {
out.credentials = cached.account;
}
return out;
}
});
server.auth.default('session');
server.route([
{
method: 'GET',
path: '/',
options: {
handler: internals.home
}
},
{
method: ['GET', 'POST'],
path: '/login',
options: {
handler: internals.login,
auth: {
mode: 'try'
},
plugins: {
cookie: {
redirectTo: false
}
}
}
},
{
method: 'GET',
path: '/logout',
options: {
handler: internals.logout
}
}
]);
await server.start();
console.log(`Server started at: ${server.info.uri}`);
};
internals.start();