javascript - 当我从页面重定向到登录路由时,Passport openid-connect 策略未进行身份验证
问题描述
现在,当我直接点击它们时,下面的这些路由可以正常工作,但是当我将用户从页面重定向到这些路由时,身份验证永远不会成功,而是被重定向回路由'/auth/openidconnect/undefined'。此外,我发现非常奇怪的是,在至少尝试了 2 次之后(将用户从上述页面重定向到 auth 路由),我成功通过了身份验证。
router.get('/auth/openidconnect',oidcSettings, oidcProviderReq);
router.get('/auth/openidconnect/callback',oidcSettings, oidcCallback);
let oidcSettings = function (req, res, next) {
//provider contains all the required data
var OidcStrategy = require('passport-openidconnect').Strategy;
passport.use('oidc', new OidcStrategy({
issuer: provider.settings.issuer,
authorizationURL: provider.settings.authorizationURL,
tokenURL: provider.settings.tokenURL,
userInfoURL: provider.settings.userInfoURL,
clientID: provider.settings.ClientID,
clientSecret: provider.settings.clientSecret,
callbackURL: provider.settings.callbackURL,
scope: 'openid profile'
}, (issuer, sub, profile, accessToken, refreshToken, done) => {
if (!(profile && profile._json && profile._json.email)) {
return done(null, false);
}
req.params.provider =profile.id
oidcLogin(req, profile, 'oidc_user', done); //basically either logs into the application or creates a new user
}));
next();
}
let oidcProviderReq = function(req, res, next){
passport.authenticate('oidc', {scope: 'openid profile'})(req, res, next);
}
let oidcCallback = function(req, res, next){
passport.authenticate('oidc', function (err, user, info) {
if(err) throw err;
console.log(user)
})(req, res, next);
}
```