Nodejs Passport显示用户名

Nodejs Passport显示用户名

问题描述:

在nodeJS中,我正在使用通行证模块进行身份验证.我想显示当前登录用户的用户名.

In nodeJS I am using the passport module for authentication. I would like to show the username of the currently logged in user.

我尝试了以下代码:

passport.displayName

Localstrategy.username

有关更多信息,请参见: http://passportjs.org/docs/profile

And for more info please also see: http://passportjs.org/docs/profile

但这不起作用.有什么建议吗?

But that is not working. Any suggestions?

谢谢

将用户(由verify回调提供)设置为请求在req.user处的属性.

The user (as supplied by the verify callback), is set as a property on the request at req.user.

在您的情况下,通过req.user.usernamereq.user.displayName可以通过该对象访问用户的任何属性.

Any properties of the user can be accessed through that object, in your case req.user.username and req.user.displayName.

如果您使用的是Express,并且希望将用户名作为模板中的变量公开,则可以在渲染时完成:

If you're using Express, and want to expose the username as a variable within a template, that can be done when rendering:

app.get('/hello', function(req, res) {
    res.render('index.jade', { username: req.user.username });
});