Giter Site home page Giter Site logo

jmw5598 / node-express-jwt-example Goto Github PK

View Code? Open in Web Editor NEW
17.0 3.0 8.0 112 KB

An example project implementing JWT authentication and role base authorization with Node.js, Express.js, Sequelize, and Sqlite

JavaScript 97.74% CSS 0.60% HTML 1.66%
node express jwt authorization authentication role sequelize sequelizejs es6 javascript jsonwebtoken sqlite sqlite3

node-express-jwt-example's Introduction

Node Express JWT Authentication / Authorization Example

An example project implementing JWT authentication and role based authorization.

Demo Setup

  1. Clone the repository with git clone https://github.com/jmw5598/node-express-jwt-example.git
  2. Generate RSA256 public private keys and place them in the config folder with file names public.key and private.key. A 512bit key size should be fine.
  3. Install dependencies with npm install.
  4. Run migrations and seed that Sqlite database with sequelize db:migrate and sequelize db:seed:all.
  5. Run the project with node index.js.

Default Credentials

Username Password Description
admin admin User with ADMIN and USER roles.
user user User with only USER role.

Default Routes

Path Method Description
/auth POST Authenticate user, returns JWT token. Jwt token can the be place in Authorization header prefixed with Bearer
/users GET List all users [requires ADMIN role]
/users POST Create new user [requires ADMIN role]
/api/v1/restricted/usersOnly GET Users only path
/api/v1/restricted/adminOnly GET Admin only path

Middleware Methods

Method Description
verify() Verifies token passed in Authorization request header. Token prefixed with Bearer
hasRole(role) Ensures the authenticated user has appropriate role
hasAnyRole([role, role]) Ensures the authenticated user has ANY of the appropriate roles
hasAllRoles([role, role]) Ensures the authenticated user has ALL the appropriate roles

node-express-jwt-example's People

Contributors

jmw5598 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.