Git Product home page Git Product logo

node-auth's Introduction

Node JS Plain Authentication

In this demo, I created a simple authenticate application without using 3rd party authentication library like Passport. By doing this, I can have a deeper understanding of how things work behind the scenes, including hashing passport, JWT encoding and decoding.

This is an API based application, no interface, you will need to use some forms of API calling like Post Man to run through the application. You also need to create a MongoDB database for storing user info.

Dependencies

  1. bcrypt
  2. config
  3. dotenv
  4. express
  5. jsonwebtoken
  6. mongoose

APIs

  1. Registration

    POST: http://localhost:3000/users/

    Params:

     . first_name
     . last_name
     . email
     . password
    

    Response:

     . code: 422
         { "error": "This email address has been taken." }
         
     . code: 200
         { "message": "Your account has been created successfully." }
    
  2. Login

    POST: http://localhost:3000/users/login

    Params:

     . email
     . password
    

    Response:

     . code: 200
         {
             "message": "You have successfully logged in.",
             "token": "xxxxx",
             "refresh_token": "xxxx"
         }
         
     . code 400
         { "error": "Your email or password is not match." }
    

    token: JWT token, indicate that you have been authenticated successfully. This token also need for further requests which require authentication. It will have an expiration time defined in config/default.json, jwt_lifetime.

    refresh_token: JWT token, used for renew a token in case that token has been expired and also could apply as a log out everywhere feature.

  3. Profile (Protected route)

    GET http://localhost:3000/users

    Header:

     . Authorization: Bearer {token}
    

    Params:

     . NONE
    

    Response:

     . Code 403 Forbidden
     
     . Code 200
         {
             "_id": "5ee45e24378a933143b23b35",
             "first_name": "Vinh",
             "last_name": "Giang",
             "email": "[email protected]",
             "created_at": "2020-06-13T05:03:32.510Z",
             "updated_at": "2020-06-19T00:35:45.692Z",
             "refresh_token": "xxx"
         }
    
  4. Refresh token (Protected route)

    POST http://localhost:3000/users/token

    Params:

     . token: {refresh_token} 
    

    Response:

     . Code 403 Forbidden
     
     . Code 200
         { "token": "xxx" }
    

    token: Use this token to keep user logged in

node-auth's People

Contributors

vinhgiang avatar

Watchers

James Cloos 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.