Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passport js authentification without sessions

I'am a beginner in expressjs and passportjs. I played with authentication via google using passport with GoogleStrategy. Using the code below i have req.user = { id: '123456' } in /users/hello route handler, but i want to get some like this without session support to send it as the answer to authenticated client. In other words i want to send some token to client if authentication is successful without cookie session start. I can't find the way how to forward user object to target route handler when i turn off sessions.

passport.use(new GoogleStrategy({
    returnURL: 'http://localhost/auth/google/return',
    realm: 'http://localhost/'
  },
  function(identifier, profile, done) {
    done(null, {id: '123456'});
  }
));

passport.serializeUser(function(user, done) {
    done(null, user.id);
});

passport.deserializeUser(function(id, done) {
    done(null, {id: id});
});

app.use(session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());

app.get('/auth/google', passport.authenticate('google');
app.get('/auth/google/return',
    passport.authenticate('google', {
        successRedirect: '/users/hello',
        failureRedirect: '/users/goodbye' 
    }));
like image 523
user3414982 Avatar asked Aug 26 '14 19:08

user3414982


1 Answers

To turn off sessions try changing this:

app.get('/auth/google/return',
passport.authenticate('google', {
    successRedirect: '/users/hello',
    failureRedirect: '/users/goodbye' 
}));

to:

app.get('/auth/google/return',
passport.authenticate('google', {
    session:false
}));
like image 139
mfink Avatar answered Nov 16 '22 00:11

mfink