我有一个passportJS回调,当新用户尝试进行身份验证时,我不希望新用户数据存储在数据库中,而是希望在以后的快速会话中存储,这可能吗?
当前我的代码是:
function facebookAuthenticate(accessToken, refreshToken, profile, done) {
User.findOne({ facebookID: profile._json.id }, (err, foundUser) => {
if (foundUser) {
done(err, foundUser);
} else {
global.authenticationID = { facebookID: profile._json.id };
done(err, null)
}
});
}
但是由于全局变量不是特定于用户的,因此一次只能用于一种身份验证。
理想情况下,我希望某些东西可以按照这些原则工作,但是我当然不能在路由之外访问req变量:
function facebookAuthenticate(accessToken, refreshToken, profile, done) {
User.findOne({ facebookID: profile._json.id }, (err, foundUser) => {
if (foundUser) {
done(err, foundUser);
} else {
req.session.authenticationID = { facebookID: profile._json.id };
done(err, null)
}
});
}
非常感谢。
是的,全局变量不在路由范围外。一种非常核心的方法是将会话数据存储在磁盘上的文件中,并在需要时将其读回。但是有一些限制。使用jwt auth可以将数据存储在浏览器会话和cookie中。
一个简单的想法是先创建一个快递,
var sessions = require("client-sessions");
app.use(sessions({
cookieName: 'expSessions', // cookie name dictates the key name added to the request object
secret: 'somesuperSecret', // should be a large unguessable string
duration: 24 * 60 * 60 * 1000, // how long the session will stay valid in ms
activeDuration: 1000 * 60 * 5, // if expiresIn < activeDuration, the session will be extended by activeDuration milliseconds
cookie: {
path: '/', // cookie will only be sent to requests under '/api'
maxAge: 60000, // duration of the cookie in milliseconds, defaults to duration above
ephemeral: false, // when true, cookie expires when the browser closes
httpOnly: true, // when true, cookie is not accessible from javascript
secure: true // when true, cookie will only be sent over SSL. use key 'secureProxy' instead if you handle SSL not in your node process
}
}));
或者,有很多npm软件包可用于会话处理,例如'express-session'。周围看看。祝你好运!