Skip to content

Instantly share code, notes, and snippets.

@afucher
Created November 23, 2016 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afucher/8d75c48f2084caa09e12bc7aad8ce496 to your computer and use it in GitHub Desktop.
Save afucher/8d75c48f2084caa09e12bc7aad8ce496 to your computer and use it in GitHub Desktop.
hapi-auth-cookie-error
"use restrict";
const Hapi = require('hapi');
const CookieAuth = require('hapi-auth-cookie');
const server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 3000
});
server.register(CookieAuth, function (err) {
if(err) throw err;
server.auth.strategy('session', 'cookie', false, {
password: 'm!*"2/),p4:xDs%KEgVr7;e#85Ah^WYC',
cookie: 'my-app',
isSecure: false
});
server.route({
path: '/secret',
method: 'POST',
config: {
auth: {
mode: "try",
strategy: 'session'
},
handler: function (request, reply) {
console.log(request.auth);
if (request.auth.isAuthenticated) {
// session data available
var session = request.auth.credentials;
return reply('AUTENTICADO');
}else{
return reply('NÃO AUTENTICADO');
}
// further processing if not authenticated …
}
}
});
server.route({
path: '/login',
method: 'POST',
handler: (request, reply) => {
var username = request.payload.username
var password = request.payload.password
if(username == "admin" && password == "123456"){
var user = {id:"123"};
// if everything went smooth, set the cookie with "user" specific data
request.cookieAuth.set(user);
reply("login");
}else{
reply("not login");
}
}
});
server.route({
path: '/logout',
method: 'POST',
handler: (request, reply) => {
request.cookieAuth.clear();
reply("logout");
}
});
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at ' + server.info.uri);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment