Nodejs Passport display username

后端 未结 5 1557
感情败类
感情败类 2020-12-08 05:16

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:



        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-08 05:42

    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 });
    });
    

提交回复
热议问题