Node.js: User Profile

View on Github

Node.js: User Profile

Gravatar for andres.aguiar@auth0.com
By Andres Aguiar
Auth0

This tutorial demonstrates how to get the user's profile and display it. We recommend you to Log in to follow this quickstart with examples configured for your account.

I want to integrate with my app

15 minutes
  1. Specify a profile Scope
  2. Display User Information
Or

I want to explore a sample app

2 minutes

Get a sample configured with your account settings or check it on Github.

View on Github
System requirements: NodeJS 4.3 or higher | Express 4.16

Specify a profile Scope

After the user has authenticated, every express request has a user object with the entire user profile. By default, it is a minimal profile. Add the profile scope to the authentication scopes to get additional profile information.

Add the openid profile value to the scope parameter in the /login route.

// routes/index.js

router.get(
  '/login',
  passport.authenticate('auth0', {
    clientID: env.AUTH0_CLIENT_ID,
    domain: env.AUTH0_DOMAIN,
    redirectUri: env.AUTH0_CALLBACK_URL,
    audience: 'https://' + env.AUTH0_DOMAIN + '/userinfo',
    responseType: 'code',
    scope: 'openid profile'
  }),
  function(req, res) {
    res.redirect('/');
  }
);

Display User Information

Modify the /user endpoint to display the user object. Update the views/user.pug template.

// views/user.pug

extends layout

block content
  img(src=user.picture)
  h2 Welcome #{user.nickname}!
  br
  h2 User Profile
  pre #{userProfile}
  br
  a(href='/logout') Logout

To have full access to the user profile on userProfile, stringify the user object. Modify the / endpoint in routes/user.js to include userProfile.

// routes/user.js

// Get the user profile
router.get('/', ensureLoggedIn, function(req, res, next) {
  res.render('user', {
    user: req.user,
    userProfile: JSON.stringify(req.user, null, '  ')
  });
});

You can now present a basic user profile.

Use Auth0 for FREECreate free Account