Nodejs Passport 显示用户名
在nodeJS中,我使用passport模块进行身份验证。我想显示当前登录用户的用户名。
我尝试了以下代码:
passport.displayName
并且
Localstrategy.username
有关更多信息,另请参阅: http://passportjs.org/docs/profile
但这不起作用。有什么建议吗?
谢谢
In nodeJS I am using the passport module for authentication. I would like to show the username of the currently logged in user.
I tried the following code:
passport.displayName
and
Localstrategy.username
And for more info please also see:
http://passportjs.org/docs/profile
But that is not working. Any suggestions?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
用户(由验证回调提供)在
req.user
处设置为请求的属性。用户的任何属性都可以通过该对象访问,在您的例子中是
req.user.username
和req.user.displayName
。如果您使用的是 Express,并且希望将用户名公开为模板中的变量,则可以在渲染时完成:
The user (as supplied by the verify callback), is set as a property on the request at
req.user
.Any properties of the user can be accessed through that object, in your case
req.user.username
andreq.user.displayName
.If you're using Express, and want to expose the username as a variable within a template, that can be done when rendering:
我创建了一个简单的视图助手来访问身份验证状态和用户信息。
完成此操作后,您将能够访问视图上的对象
auth
,例如auth.user.xxxx
。I've created a simple view helper to have access to authentication status and user information
After doing that you will be able to acess the object
auth
on your views, for exampleauth.user.xxxx
.Express v4.x 不支持助手
一个好的替代方法是创建一个中间件,例如:
然后您可以在视图中使用“user”变量。
Helpers are not supported with Express v4.x
A good alternative is to create a middleware such as:
Then you can use the "user" variable in your views.
路线代码 -
.ejs 文件代码 -
Routes Code -
.ejs File Code -
这可能有帮助
请求会话.passport.user
This might help
req.session.passport.user