如何使用 Passport 和 Express-session 注销并清除会话?
How to log out and clear session with Passport and Express-session?
我正在使用 Passport-Google-OAuth2.0 和 Express-session 进行身份验证。使用connect-redis
存储会话,
// app.ts
const RedisStore = connectRedis(session)
export const app = express()
app.set('trust proxy', 1)
export const cookieOptions = {
maxAge: +COOKIE_MAX_AGE,
httpOnly: true,
secure: isProduction,
signed: true,
}
app.use(
session({
store: new RedisStore({ client: redis }),
secret: env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: env.cookieOptions,
})
)
app.use(passport.initialize())
app.use(passport.session())
登录有效,但无法注销。这是我的尝试:
// Works
router.get('/auth/google', passport.authenticate('google', { scope: ['profile'] }))
// Works
router.get(
'/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/signin' }),
async function (_req, res, _next) {
res.redirect('/')
}
)
// Not working
router.get('/auth/signout', function (req, res) {
req.session.destroy(function (err) {
if (err) console.log(` >> sess.des:`, err)
req.logout()
res.clearCookie('connect.sid')
// res.clearCookie('connect.sid', cookieOptions) // This throws 500 error
res.redirect('/outed')
})
})
当我转到 http://localhost:8000/auth/signout
时,它会重定向到 /outed
。我预计它会撤销我的身份验证状态并且必须重新登录。
所以,我转到 http://localhost:8000/auth/google
再次登录,但它一直将我重定向回已验证成功的路由 /
。
如何使用 Passport 和 Redis 会话正确注销?
连接-redis: https://github.com/tj/connect-redis
快速会话:https://github.com/expressjs/session
护照-google-oauth2:https://github.com/jaredhanson/passport-google-oauth2
首先,单独使用 req.logout()
。不要调用 req.session.destroy()
,然后在其中尝试调用 req.logout()
(如您问题中的代码当前所示)。
然后,当您第一次进入 /auth/google
google 路线时,它将通过一个 oauth 会话,您必须向 Google 确认您是允许此站点通过 oauth 使用您的 Google 登录。
当您随后注销您的应用程序时,它只会终止您的应用程序登录。它不会终止该浏览器中的 Google 登录。那仍然存在。因此,如果您随后返回 /auth/google
,它会无缝地(无需您进一步批准或参与)以当前登录的 Google 用户身份重新登录。 Google 看到您之前已经批准此应用使用您的 Google 登录,并且看到当前浏览器仍处于登录状态 Google,因此它只是批准了登录而没有再次询问您。
如果您确实希望您的应用程序能够以不同的 Google 用户身份登录(或使用类似机制的任何其他服务),那么您已将此浏览器从 Google 本身注销或以其他用户身份登录 Google 本身,或在 Google 内撤销该应用使用 Google 登录的权限。这将真正让您回到零起点,在那里您可以以不同的 Google 用户身份登录。
仅供参考,如果您使用 Google 登录 Whosebug,您可以对此进行试验并看到完全相同的行为。
我正在使用 Passport-Google-OAuth2.0 和 Express-session 进行身份验证。使用connect-redis
存储会话,
// app.ts
const RedisStore = connectRedis(session)
export const app = express()
app.set('trust proxy', 1)
export const cookieOptions = {
maxAge: +COOKIE_MAX_AGE,
httpOnly: true,
secure: isProduction,
signed: true,
}
app.use(
session({
store: new RedisStore({ client: redis }),
secret: env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: env.cookieOptions,
})
)
app.use(passport.initialize())
app.use(passport.session())
登录有效,但无法注销。这是我的尝试:
// Works
router.get('/auth/google', passport.authenticate('google', { scope: ['profile'] }))
// Works
router.get(
'/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/signin' }),
async function (_req, res, _next) {
res.redirect('/')
}
)
// Not working
router.get('/auth/signout', function (req, res) {
req.session.destroy(function (err) {
if (err) console.log(` >> sess.des:`, err)
req.logout()
res.clearCookie('connect.sid')
// res.clearCookie('connect.sid', cookieOptions) // This throws 500 error
res.redirect('/outed')
})
})
当我转到 http://localhost:8000/auth/signout
时,它会重定向到 /outed
。我预计它会撤销我的身份验证状态并且必须重新登录。
所以,我转到 http://localhost:8000/auth/google
再次登录,但它一直将我重定向回已验证成功的路由 /
。
如何使用 Passport 和 Redis 会话正确注销?
连接-redis: https://github.com/tj/connect-redis
快速会话:https://github.com/expressjs/session
护照-google-oauth2:https://github.com/jaredhanson/passport-google-oauth2
首先,单独使用 req.logout()
。不要调用 req.session.destroy()
,然后在其中尝试调用 req.logout()
(如您问题中的代码当前所示)。
然后,当您第一次进入 /auth/google
google 路线时,它将通过一个 oauth 会话,您必须向 Google 确认您是允许此站点通过 oauth 使用您的 Google 登录。
当您随后注销您的应用程序时,它只会终止您的应用程序登录。它不会终止该浏览器中的 Google 登录。那仍然存在。因此,如果您随后返回 /auth/google
,它会无缝地(无需您进一步批准或参与)以当前登录的 Google 用户身份重新登录。 Google 看到您之前已经批准此应用使用您的 Google 登录,并且看到当前浏览器仍处于登录状态 Google,因此它只是批准了登录而没有再次询问您。
如果您确实希望您的应用程序能够以不同的 Google 用户身份登录(或使用类似机制的任何其他服务),那么您已将此浏览器从 Google 本身注销或以其他用户身份登录 Google 本身,或在 Google 内撤销该应用使用 Google 登录的权限。这将真正让您回到零起点,在那里您可以以不同的 Google 用户身份登录。
仅供参考,如果您使用 Google 登录 Whosebug,您可以对此进行试验并看到完全相同的行为。