node.js - hapi-auth-cookie not setting cookie -
for node app im using bell , hapi-auth-cookie plugins use yahoo api. current code, able authenticate yahoo , redirected homepage. however, request.auth seems empty once homepage. can tell, i'm doing example, yet have no authentication once homepage. appreciated! here's i've got:
var path = require('path'); var hapi = require('hapi'); var cookiesession = require('cookie-session'); var serveroptions = { views: { engines: { html: require('handlebars') }, path: path.join(__dirname, './app/www/public/pages'), layoutpath: path.join(__dirname, './app/www/public/pages') } }; var server = new hapi.server(8003, serveroptions); server.pack.register([ require('bell'), require('hapi-auth-cookie') ], function(err) { if (err) { throw err; } server.auth.strategy('yahoo', 'bell', { provider: 'yahoo', password: 'cookie_encryption_password', clientid:'2kj3kj2', clientsecret: '3kj2k3jl', issecure: false // terrible idea required if not using https }); server.auth.strategy('session', 'cookie', { password: 'secret', cookie: 'sid-example', redirectto: '/login', issecure: false }); server.route({ method: ['get', 'post'], // must handle both , post path: '/login', // callback endpoint registered provider config: { auth: 'yahoo', handler: function (request, reply) { var creds = request.auth.credentials; request.auth.session.clear(); request.auth.session.set(creds); return reply.redirect('/'); } } }); server.route({ method: 'get', path: '/', handler: function (request, reply) { reply.view('index', { title: 'hello world' }); } }); server.start(); });
to expound upon , extend eran's answer:
if want have access authentication/session data route doesn't need authentication view (such home page) possible not intuitive in opinion. have set auth scheme on route change mode 'try' , set route specific hapi-auth-cookie parameter prevent unauthenticated user being redirected login page such:
server.route({ method: 'get', path: '/', config: { handler: homepage, auth: { mode: 'try', strategy: 'session' }, plugins: { 'hapi-auth-cookie': { redirectto: false } } } });
mode: 'try'
allow user proceed route path if not authenticated , redirectto: false
stop unauthenticated request route being redirected login page. way, users can route without authentication (typical home page) once authenticated cookie data set via hapi-auth-cookie available use.
Comments
Post a Comment