Code Monkey home page Code Monkey logo

node-auth's Introduction

NodeJS - JWT Auth

Table of contents

Requirements

Install Node.JS and npm:

sudo apt install nodejs
node -v
npm -v

npm is a package manager for Javascript that also keeps track of the packages in your projects.

Then install the following NodeJS packages:

  • express - our web application framework
  • body-parser - parses Content-Types in Request
  • jsonwebtoken - our JWT token manager
  • mongodb - our database
  • mongoose - communicates with our MongoDB database
  • morgan - logs our middleware
npm express body-parser jsonwebtoken mongodb mongoose morgan --save
  • --save just makes sure our packages are saved in our package.json file

Then install nodemon which is a library that helps our server reload everytime a file is changed and saved. Without nodemon we'd have to shut down the server and restart it:

npm install --save-dev nodemon
  • --save-dev saves nodemon as a development dependecy

Deployment

Run:

nodemon server.js

Authenticate

POST

/api/authenticate

Request:

{
    "email":"[email protected]",
    "password":"password"
}

Response;

{
    "id": "5beca3a6d118642f620fe04b",
    "email": "[email protected]",
    "first_name": "Nick",
    "last_name": "Cerminara",
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhZG1pbiI6dHJ1ZSwiaWF0IjoxNTQyMjM3MTY2LCJleHAiOjE1NDIzMjM1NjZ9.96a4g7Btd6DCgBzt4Ysx6Hu5y60nBMu8fYiQ0pJ1LnI"
}

Status Codes:

  • 200 - OK - If authentication was validated
  • 404 - Not Found - If user is not found
  • 403 - Forbidden - Wrong password

Users

This endpoint return a list of all the users in the database. This happens only because my 'dummy' user is and admin.

GET

/api/users

Request:

A request can be made different ways:

Header:

x-access-token

Parameter on the url request:

localhost:8080/api/user?token=2394829384

Or an application/json body

{
    "token":"akdh9384wherofa"
}

Response:

[
    {
        "id": "5beca3a6d118642f620fe04b",
        "email": "[email protected]",
        "first_name": "Nick",
        "last_name": "Cerminara"
    }
]

Status Codes:

  • 200 - OK - If authentication was validated
  • 403 - Forbidden - No token given

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.