问题描述
我是expressjs和passportjs的初学者.我使用带有 GoogleStrategy 的护照通过谷歌进行身份验证.使用下面的代码,我在/users/hello 路由处理程序中有 req.user = { id: '123456' } ,但我想在没有会话支持的情况下得到一些这样的东西,将它作为答案发送给经过身份验证的客户端.换句话说,如果身份验证成功而没有 cookie 会话启动,我想向客户端发送一些令牌.当我关闭会话时,我找不到如何将用户对象转发到目标路由处理程序的方法.
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 successfull 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'
}));
推荐答案
要关闭会话,请尝试更改:
To turn off sessions try changing this:
app.get('/auth/google/return',
passport.authenticate('google', {
successRedirect: '/users/hello',
failureRedirect: '/users/goodbye'
}));
到:
app.get('/auth/google/return',
passport.authenticate('google', {
session:false
}));
这篇关于无需会话的Passport js身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!