为什么在 Passport Oauth 2.0 身份验证中同时检查 req.isAuthenticated() 和 req.user?

问题描述 投票:0回答:1

为什么在 Passport Oauth 2.0 中同时检查 req.isAuthenticated() 和 req.user 进行身份验证?我相信仅 req.isAuthenticated() 就足以进行社交登录。我看到其他人同时使用两者,但我不明白为什么。这里的最佳实践是什么?

我相信仅 req.isAuthenticated() 就足够了

node.js oauth-2.0 backend passport.js web-development-server
1个回答
0
投票

.isAuthenticated()
更好,因为护照允许您重新定义属性名称来存储用户。

app.use(passport.initialize({ userProperty: 'me' }))

now

req.me
应该用于访问当前用户信息,而不是
req.user

让我们检查一下

.isAuthenticated()
的源代码。正如您所看到的,它实际上检查请求中是否存在存储用户信息的属性
(this[property]) ? true : false;

© www.soinside.com 2019 - 2024. All rights reserved.