Nodejs Passport 显示用户名

发布于 2025-01-03 17:33:25 字数 315 浏览 4 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我为君王 2025-01-10 17:33:26

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

用户的任何属性都可以通过该对象访问,在您的例子中是 req.user.usernamereq.user.displayName

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

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

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 and req.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:

app.get('/hello', function(req, res) {
    res.render('index.jade', { username: req.user.username });
});
负佳期 2025-01-10 17:33:26

我创建了一个简单的视图助手来访问身份验证状态和用户信息。

var helpers = {};

helpers.auth = function(req, res) {
    var map = {};
    map.isAuthenticated = req.isAuthenticated();
    map.user = req.user;
    return map;
}

app.dynamicHelpers(helpers);

完成此操作后,您将能够访问视图上的对象auth,例如auth.user.xxxx

I've created a simple view helper to have access to authentication status and user information

var helpers = {};

helpers.auth = function(req, res) {
    var map = {};
    map.isAuthenticated = req.isAuthenticated();
    map.user = req.user;
    return map;
}

app.dynamicHelpers(helpers);

After doing that you will be able to acess the object auth on your views, for example auth.user.xxxx.

や三分注定 2025-01-10 17:33:26

Express v4.x 不支持助手

一个好的替代方法是创建一个中间件,例如:

app.use((req, res, next) => {
    res.locals.user = req.user;
    next();
});

然后您可以在视图中使用“user”变量。

Helpers are not supported with Express v4.x

A good alternative is to create a middleware such as:

app.use((req, res, next) => {
    res.locals.user = req.user;
    next();
});

Then you can use the "user" variable in your views.

假装不在乎 2025-01-10 17:33:26

路线代码 -

router.get('/', ensureAuthenticated, function(req, res){
  res.render('administrator/dashboard',{title: 'Dashboard', user:req.user.username });
  console.log(req.user.username);
});

.ejs 文件代码 -

Welcome:  <%= user %>  

Routes Code -

router.get('/', ensureAuthenticated, function(req, res){
  res.render('administrator/dashboard',{title: 'Dashboard', user:req.user.username });
  console.log(req.user.username);
});

.ejs File Code -

Welcome:  <%= user %>  
沉睡月亮 2025-01-10 17:33:26

这可能有帮助
请求会话.passport.user

This might help
req.session.passport.user

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文