Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs Passport display username

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

like image 289
The Code Buccaneer Avatar asked Feb 09 '12 17:02

The Code Buccaneer


2 Answers

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 }); }); 
like image 125
Jared Hanson Avatar answered Sep 18 '22 08:09

Jared Hanson


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.

like image 26
Eduardo Nunes Avatar answered Sep 17 '22 08:09

Eduardo Nunes