Node.js: Login

Gravatar for
By Andres Aguiar

Sample Project

Download a sample project specific to this tutorial configured with your Auth0 API Keys.

System Requirements
  • NodeJS 4.3 or higher
  • Express 4.16
Show requirements

Before you start

This guide walks you through setting up authentication and authorization in your Node.js apps with Auth0. If you are new to Auth0 we suggest you check our Overview. For a complete picture of authentication and authorization for regular web apps, check our Single Sign-On for Regular Web Apps documentation.

Auth0 uses OAuth. If you want to learn more about the OAuth flows used by regular web apps, read about Authentication for Server-side Web Apps.

Get Your Application Keys

When you signed up for Auth0, you created a new application.

You will need some details about this application to communicate with Auth0. You can get them from the Application Settings in the Auth0 dashboard.

You need the following information:

  • Client ID
  • Domain

If you download the sample from the top of this page, these details are filled out for you. If you have more than one application in your account, the sample comes with the values for your Default App.

App Dashboard

Configure Callback URLs

A callback URL is a URL in your application where Auth0 redirects the user after they have authenticated.

You need to whitelist the callback URL for your app in the Allowed Callback URLs field in your Application Settings. If you do not set any callback URL, your users will see a mismatch error when they log in.

If you are following along with the sample project you downloaded from the top of this page, Callback URL should be set to http://localhost:3000/callback.

Install the Middleware Dependencies

Install the necessary middleware.

# installation with npm
npm install passport passport-auth0 connect-ensure-login --save

# installation with yarn
yarn add passport passport-auth0 connect-ensure-login

Configure the Middleware

Create a new instance of the Auth0Strategy strategy. Enter your Auth0 application details as configuration values. Tell passport to use this strategy.

// app.js

const passport = require('passport');
const Auth0Strategy = require('passport-auth0');

// Configure Passport to use Auth0
const strategy = new Auth0Strategy(
    domain: 'YOUR_AUTH0_DOMAIN',
    clientID: 'YOUR_CLIENT_ID',
    clientSecret: 'YOUR_CLIENT_SECRET',
    callbackURL: 'http://localhost:3000/callback'
  (accessToken, refreshToken, extraParams, profile, done) => {
    return done(null, profile);


// This can be used to keep a smaller payload
passport.serializeUser(function(user, done) {
  done(null, user);

passport.deserializeUser(function(user, done) {
  done(null, user);

// ...

Trigger Authentication

Universal Login is the easiest way to set up authentication in your application. We recommend using it for the best experience, best security and the fullest array of features.

You can also embed the login dialog directly in your application using the Lock widget. If you use this method, some features, such as single sign-on, will not be accessible. To learn how to embed the Lock widget in your application, follow the Embedded Login sample.

Add a route called /login. Use the env object to set the following properties for your application:

  • Client ID
  • Domain
  • Callback URL

The route creates an instance of the auth0.WebAuth object. Then, the route calls the authorize method and redirects the user to the login page.

// routes/index.js

const express = require('express');
const passport = require('passport');
const router = express.Router();

const env = {
  AUTH0_CALLBACK_URL: 'http://localhost:3000/callback'

/* GET home page. */
router.get('/', function(req, res, next) {

// Perform the 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'
  function(req, res) {

// Perform session logout and redirect to homepage
router.get('/logout', (req, res) => {

// Perform the final stage of authentication and redirect to '/user'
  passport.authenticate('auth0', {
    failureRedirect: '/'
  function(req, res) {
    res.redirect(req.session.returnTo || '/user');

hosted login

Use Auth0 for FREECreate free Account