Giter Site home page Giter Site logo

tterimaa / express-jwt-authentication-starter-typescript Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zachgoll/express-jwt-authentication-starter

0.0 1.0 0.0 3.16 MB

A simple JWT Authentication Implementation using Typescript, Node, Express and MongoDB

TypeScript 100.00%

express-jwt-authentication-starter-typescript's Introduction

Express app boilerplate with passportjs-jwt authentication and role-based authorization

Use this boilerplate for your express project that needs passportjs-jwt authentication and Typescript. This boilerplate also comes with role-based authorization.

Originally forked from from express-jwt-authentication-starter.

Project structure inspired by Bulletproof node.js project architecture.

How to use this Repo

This starter is especially suitable for express.js backend that is connected to SPA frontend (React, Angular or similar). More information about the usage of passport.js can be found from the official documents.

You will need to start the Mongo DB database using the mongod process. You can run this process persistently in the background, but you could also just type mongod in your terminal.

Next, you will need to generate a public/private keypair. The .gitignore automatically ignores the private key. To generate the keypair run

npm run genKeypair

Quickstart

Start the Express server in dev mode (http://localhost:3000)

npm run dev

Build the project for production use

npm run build

Run the production build

npm start

Authentication flow

You can test the authentication with HTTP client of your preference, I use Postman. The app currently has four routes: /users/register, /users/login, /users/protected and /users/protected-admin. You should be able to perform the following authentication flow:

  1. POST /users/register with object
{
    "username": "your-username", 
    "password": "your-password" 
}

as the request body.

  1. POST /users/login with the same object in the body as in the registration. You should get a response that contains field "token". Copy the token string starting with "Bearer..". Copy the token without the quotation marks.

  2. GET /users/protected with your token attached to the Authorization header. You should get a response message "You are succesfully authenticated for this route!"

  3. Get /users/protected-admin should give you a response "Unauthorized (role)". To access this route you need to create an admin account by adding "role": ["admin"] in phase 1.

If you completed the previous steps successfully, the app is working correctly and you can start building your own on top of that!

express-jwt-authentication-starter-typescript's People

Contributors

tterimaa avatar zachgoll avatar

Watchers

 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.