| title | Auth0 Node.js Express SDK Web App Tutorial | ||||||
|---|---|---|---|---|---|---|---|
| description | This tutorial will show you how to use the Auth0 Node.js Express SDK to add authentication and authorization to your web app. | ||||||
| name | Node.js | ||||||
| image | /media/platforms/node.png | ||||||
| tags |
|
||||||
| snippets |
|
||||||
| alias |
|
||||||
| seo_alias | nodejs |
You can get started by either downloading the seed project or if you would like to add Auth0 to an existing application you can follow the tutorial steps.
::: panel-info System Requirements This tutorial and seed project have been tested with the following:
- NodeJS 4.3 or superior
- Express 4.11 :::
<%= include('../_includes/_package', { pkgRepo: 'node-auth0', pkgBranch: 'master', pkgPath: 'examples/nodejs-regular-webapp', pkgFilePath: null, pkgType: 'server' }) %>
If you have an existing application, follow the steps below.
Just run the following code to install the dependencies and add them to your package.json
${snippet(meta.snippets.dependencies)}
We need to configure Passport to use Auth0 strategy.
Create a file called setup-passport.js and add these contents to it:
${snippet(meta.snippets.setup)}
In the startup file (e.g. server.js or app.js) add:
var passport = require('passport');
// This is the file we created in step 2.
// This will configure Passport to use Auth0
var strategy = require('./setup-passport');
// Session and cookies middlewares to keep user logged in
var cookieParser = require('cookie-parser');
var session = require('express-session');Now, just add the following middlewares to your app:
app.use(cookieParser());
// See express session docs for information on the options: https://github.com/expressjs/session
app.use(session({ secret: 'YOUR_SECRET_HERE', resave: false, saveUninitialized: false }));
...
app.use(passport.initialize());
app.use(passport.session());
...We need to add the handler for the Auth0 callback so that we can authenticate the user and get their information.
// Auth0 callback handler
app.get('/callback',
passport.authenticate('auth0', { failureRedirect: '/url-if-something-fails' }),
function(req, res) {
if (!req.user) {
throw new Error('user null');
}
res.redirect("/user");
});${include('./_callbackRegularWebApp')}
In this case, the callbackURL should look something like:
http://yourUrl/callback
${lockSDK}
Note: Please note that the
callbackURLspecified in theAuth0Lockconstructor must match the one specified in the previous step
You can access the user information via the user field in the request
app.get('/user', function (req, res) {
res.render('user', {
user: req.user
});
});You have configured your NodeJS Webapp to use Auth0. Congrats, you're awesome!
You can add the following middleware to check if the user is authenticated and redirect him to the login page if he's not:
// requiresLogin.js
module.exports = function(req, res, next) {
if (!req.isAuthenticated()) {
return res.redirect('/');
}
next();
}// user.js
var requiresLogin = require('requiresLogin');
app.get('/user',
requiresLogin,
function (req, res) {
res.render('user', {
user: req.user
});
});