Coder Social home page Coder Social logo

jwtauth's Introduction

jwtauth

JSON Web Token implementation with HapiJS in collaboration with MongoDB via mongoose.

Usage

Clone the repository and then issue the command to install application dependencies

npm install

In the repository root folder run the command to start the server

npm start

Create a new user

Register a new user to the system

URL : /api/user/create

Method : POST

Auth required : NO

Payload example

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

Success Responses

Condition : Data provided is valid and User is created.

Code : 200 OK

Content example : Response with the new user data

{
  "success": true,
  "data": {
    "user": {
      "created_at": "2018-10-10T21:34:41.417Z",
      "_id": "5bbe7077a613335cef0fd7ec",
      "email": "[email protected]",
      "password": "$2b$10$UmonZBB3bx8sxJU3SQpS.e60Gq.FMsyunTlJ6q58VsXEqXNGcEawi",
      "__v": 0
    },
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjViYmU3MDc3YTYxMzMzNWNlZjBmZDdlYyIsImVtYWlsIjoicnZwYW5vemhoNTUyOUBnbWFpbC5jb20iLCJpYXQiOjE1MzkyMDcyODcsImV4cCI6MTUzOTIxMDg4N30.Y-E__abwHOhJX5UQDpW4uETyRrRRJCbU7QeRha_mjTs"
  }
}

Error Response - Email taken

Condition : If provided email is taken

Code : 400 BAD REQUEST

Content example :

{
  "statusCode": 400,
  "error": "Bad Request",
  "message": "Email taken"
}

Authenticate user

Authenticate a user using email and password

URL : /api/user/authenticate

Method : POST

Auth required : NO

Payload example

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

Success Responses

Condition : Data provided is valid and User is authenticated.

Code : 200 OK

Content example : Response with the user's email and token

{
    "success": true,
    "data": {
        "user": "[email protected]",
        "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InJ2cGFub3poaDU1MzI5QGdtYWlsLmNvbSIsImlhdCI6MTUzOTIwNzk3NiwiZXhwIjoxNTM5MjExNTc2fQ.keGuJZKWMwP4SNhvE_qyNHAIWx979vOFjB7obtrTHdk"
    }
}

Add custom routes

Add your own routes to the routes folder. In order to register the routes with the server import your routes in ./server.js and then issue the command

import { your_routes } from "./routes";

server.route(your_routes);

Notes

  • You can change the default configuration file ./config.js to setup your mongodb installation.

jwtauth's People

Contributors

rvpanoz avatar

Watchers

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