Nodejs Passport显示用户名 [英] Nodejs Passport display username

查看:138
本文介绍了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 });
});

这篇关于Nodejs Passport显示用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆