会话过期时间可在认证提供者端进行配置。例如,假设您正在使用
auth0
作为身份验证提供者,则可以在应用程序设置(
https://auth0.com/docs/tokens/guides/access-token/set-access-token-lifetime)中配置
token
超时时间。
![enter image description here](https://istack.dev59.com/lHIQ5.webp)
就“刷新令牌(refresh token)”而言,Passport本身不支持,我们需要自己实现。对于Auth0,您可以按照
https://auth0.com/docs/tokens/refresh-token/current中的流程来更新令牌。我从该链接中复制了代码。
var request = require("request");
var options = { method: 'POST',
url: 'https://YOUR_DOMAIN/oauth/token',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
form:
{ grant_type: 'refresh_token',
client_id: 'YOUR_CLIENT_ID',
client_secret: 'YOUR_CLIENT_SECRET',
refresh_token: 'YOUR_REFRESH_TOKEN' } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
或者您可以使用一个附加组件来处理护照https://github.com/fiznool/passport-oauth2-refresh
var passport = require('passport'),
, refresh = require('passport-oauth2-refresh')
, FacebookStrategy = require('passport-facebook').Strategy;
var strategy = new FacebookStrategy({
clientID: FACEBOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET,
callbackURL: "http://www.example.com/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done) {
User.findOrCreate(..., function(err, user) {
if (err) { return done(err); }
done(null, user);
});
});
passport.use(strategy);
refresh.use(strategy);
var refresh = require('passport-oauth2-refresh');
refresh.requestNewAccessToken('facebook', 'some_refresh_token', function(err, accessToken, refreshToken) {
});