如何解决谷歌护照回调后设置cookie
我正在使用 facebook 和 google oauth2 登录,使用 passport js,此流程
- 用户点击了登录按钮
- 重定向到 facebook/google auth 页面(取决于用户选择的登录方式)
- 身份验证页面重定向回回调页面 (
/auth/callback/[provider]
) - passport express 中间件将捕获它以解析一些数据,然后将其发送到我自己的远程 api 以让用户登录
- auth 远程 api 将发送一个包含用户令牌的响应
- 自定义 express 中间件将捕获响应以在服务器上设置 cookie
- 快递链通过将其路由到
/profile
结束(在浏览器上设置了带有令牌的 cookie) -
/profile
然后将检查是否有令牌,如果没有:它将重定向到/
在 facebook 登录时执行此流程没问题,用户成功重定向到 /profile
,以及其所有数据和令牌,但 google oauth2 登录似乎正在执行重定向到 /profile
然后设置令牌(步骤 #7 然后 #6),所以每次用户使用 google oauth2 登录时,它总是会被重定向回 /
因为它到达的时间在 /profile
,它没有令牌
这是上面流程中的代码
#./server.js
const express = require('express')
const next = require('next')
const Passport = require('./server/middleware/passport')
const Api = require('./server/api')
const port = parseInt(process.env.PORT,10)
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
app
.prepare()
.then(() => {
const server = express()
// ... other unrelated things
server.use(Passport.initialize())
Api.passport.facebook(server)
Api.passport.facebookCallback(server)
Api.passport.google(server)
Api.passport.googleCallback(server)
// ... other unrelated things
server.all('*',(req,res) => handle(req,res))
server.listen(port,(error) => {
if (error) throw error
// ... other unrelated things
})
})
#./server/api.js
const Passport = require('middleware/passport')
function setCookie(req,res,next) {
res.cookie('token',req.user.auth.token,{
httpOnly: true,sameSite: 'strict',path: '/',secure: process.env.NODE_ENV !== 'development',})
next()
}
function facebook(app) {
return app.get('/auth/facebook',next) => {
Passport.authenticate('facebook',{
scope: ['email','public_profile']
})(req,next)
})
}
function facebookCallback(app) {
return app.get(
'/auth/callback/facebook',Passport.authenticate('facebook',{ session: false,failureRedirect: '/' }),setCookie,res) => {
res.redirect('/profile')
},)
}
function google(app) {
return app.get('/auth/google',next) => {
Passport.authenticate('google',{
scope: [
'https://www.googleapis.com/auth/userinfo.email ','https://www.googleapis.com/auth/userinfo.profile ',],prompt: 'consent',authType: 'rerequest',accesstype: 'offline',})(req,next)
})
}
function googleCallback(app) {
return app.get(
'/auth/callback/google',Passport.authenticate('google',{ failureRedirect: '/',session: false }),)
}
module.exports = {
passport: {
facebook,facebookCallback,google,googleCallback,}
}
#./server/middleware/passport.js
const axios = require('axios')
const passport = require('passport')
const GoogleStrategy = require('passport-google-oauth20').Strategy
const FacebookStrategy = require('passport-facebook').Strategy
passport.serializeUser((user,done) => {
done(null,user)
})
passport.deserializeUser((obj,obj)
})
function verifyCallback(req,...,done) {
process.nextTick(async () => {
try {
const options = {
baseURL: baseUrl,// My remote api url
method: 'POST',url: '/auth/signin',headers: {
'Content-Type': 'application/json',},data: JSON.stringify({
// email,fullname,etc
}),}
const response = await axios(options)
return done(null,response.data)
} catch (error) {
const { response } = error
return done(JSON.stringify(response.data,null,2),null)
}
})
}
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,clientSecret: process.env.GOOGLE_CLIENT_SECRET,callbackURL: callbackURLGoogle,passReqToCallback: true,verifyCallback))
passport.use(new FacebookStrategy({
clientID: process.env.FACEBOOK_CLIENT_ID,clientSecret: process.env.FACEBOOK_CLIENT_SECRET,callbackURL: callbackURLFacebook,enableProof: true,profileFields: ['id','name','email','picture.type(large)'],verifyCallback))
module.exports = passport
我console.log()
的事情,只是为了弄清楚它是否属于正确的流程顺序,控制台似乎没有记录任何可疑的东西,我在这里遗漏了什么吗?
PS:我也在使用 next js 和 custom server
解决方法
我遇到了同样的问题,并且能够使用自定义回调发送 cookie。
router.get('/google/callback',(req,res) => {
passport.authenticate('google',{session: false,failureRedirect:'/auth/google/failure'},async(err,user) => {
// You can send cookies and data in response here.
})(req,res)
})
请参阅 documentation 中的自定义回调部分以获取解释。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。