Skip to content

An example of how to make authentication and authorization with auth0 + nodejs + express.

Notifications You must be signed in to change notification settings

limaleandro1999/auth0-nodejs-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Auth0-nodejs-example

This repo was done with the intention of showing how to do authentication and authorization with Node.js and Auth0.

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

What things you need to install the software and how to install them

Installing

A step by step series of examples that tell you how to get a development env running

Install the required modules

npm install

And then

node index.js

Tutorial

Requires

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const jwt = require('express-jwt');
const jwks = require('jwks-rsa');
const request = require('request');

Authentication

app.post('/authenticate', (req, res) => {
    const username = req.body.username; //Username or email
    const password = req.body.password;

    const options = { method: 'POST',
        url: 'YOUR_AUHT0_API_URL/oauth/token',
        headers: { 'content-type': 'application/json' },
        body: { 
            grant_type: 'password',
            username: username,
            password: password,
            audience: YOUR_AUDIENCE,
            scope: 'read:sample',
            client_id: YOUR_CLIENT_ID,
            client_secret: YOUR_CLIENT_SECRET },
        json: true 
    };

    request(options, function (error, response, body) {
        res.status(response.statusCode).json(body);
    });
});

Authorization

const jwtCheck = jwt({
    secret: jwks.expressJwtSecret({
        cache: true,
        rateLimit: true,
        jwksRequestsPerMinute: 5,
        jwksUri: "YOUR_AUHT0_API_URL/.well-known/jwks.json"
    }),
    audience: 'YOUR_AUDIENCE',
    issuer: 'ISSUER',
    algorithms: ['RS256']
});

app.get('/protected', jwtCheck, (req, res) => {
    res.json({message: 'authorized'})
});

Built With

Author

About

An example of how to make authentication and authorization with auth0 + nodejs + express.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published