![](/img/trans.png)
[英]Local and Google strategy in passport.js: issue when serializing user
[英]Passport.js strategy fails when not using session
我試圖弄清楚如何將Oauth策略(github)集成到使用express和websockets的應用程序中。
我正在遵循本指南,該指南說明了如何使用JWT令牌而不是使用默認的護照會話
https://blog.hyphe.me/token-based-authentication-with-node/
這是我到目前為止的代碼
app.use(passport.initialize())
app.get('/auth/github',passport.authenticate('github',{session:false}),serialize, generateToken, respond)
app.get('/auth/github/callback',passport.authenticate('github',{failureRedirect:'/'}),
function(req,res){
res.redirect('/')
}
)
當我嘗試通過github登錄時-出現以下錯誤
Error: Failed to serialize user into session
at pass (/home/avernus/Desktop/experiments/oauth/node_modules/passport/lib/authenticator.js:271:19)
at Authenticator.serializeUser (/home/avernus/Desktop/experiments/oauth/node_modules/passport/lib/authenticator.js:289:5)
at IncomingMessage.req.login.req.logIn (/home/avernus/Desktop/experiments/oauth/node_modules/passport/lib/http/request.js:50:29)
at Strategy.strategy.success (/home/avernus/Desktop/experiments/oauth/node_modules/passport/lib/middleware/authenticate.js:235:13)
at verified (/home/avernus/Desktop/experiments/oauth/node_modules/passport-oauth2/lib/strategy.js:177:20)
at Strategy._verify (/home/avernus/Desktop/experiments/oauth/passport.js:13:12)
at /home/avernus/Desktop/experiments/oauth/node_modules/passport-oauth2/lib/strategy.js:193:24
at /home/avernus/Desktop/experiments/oauth/node_modules/passport-github/lib/strategy.js:174:7
at passBackControl (/home/avernus/Desktop/experiments/oauth/node_modules/oauth/lib/oauth2.js:125:9)
at IncomingMessage.<anonymous> (/home/avernus/Desktop/experiments/oauth/node_modules/oauth/lib/oauth2.js:143:7)
我不確定問題出在哪里
這是我的github策略
passport.use(new githubStrategy({
clientID:'********',
clientSecret:'*******',
callbackURL:'http://localhost:3000/auth/github/callback'
},
function(accessToken,refreshToken,profile,done){
console.log('accessToken: ',accessToken,' refreshToken: ',refreshToken,' profile: ',profile)
return done(null,profile)
}
))
我可以從github成功獲取個人資料
序列化功能
function serialize(req, res, next) {
db.updateOrCreate(req.user, function(err, user){
if(err) {return next(err);}
// we store the updated information in req.user again
req.user = {
id: user.id
};
next();
});
}
根據我的經驗,即使使用session:false選項,使用帶有oauth的passwordjs始終需要運行會話。
我相信底層的oauth庫依賴項無論如何都會尋找會話。 它非常令人沮喪。
編輯:為此添加更多細節,您鏈接到的示例使用默認策略,該策略不是基於oauth的。 在這種情況下,您可以選擇不使用會話。 您正在使用使用oauth的github策略,因此需要會話
您是否在回調中缺少{session:false}選項?
app.get('/auth/github/callback',passport.authenticate('github',{failureRedirect:'/', session: false}),
function(req,res){
res.redirect('/')
})
我在這里猜測是因為我從未使用過需要回調的策略。 但是我會想象護照會嘗試在回調中序列化用戶,因為那是您從Github接收個人資料的地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.