Coder Social home page Coder Social logo

auth-api's Introduction

title author
API Authentication With JWT
4ugury

Authentication API with nodejs, mongodb, and JWT.

install

npm install
npm start

( please add mongo con string to .env file )

Register User

POST http://localhost:3000/api/user/register

example json data

{
    "name" : "theadmin",
    "email" : "[email protected]",
    "password" : "Kekc8swFgD6zU"
}

responses

Success

{
    "user": "theadmin"
}

Email Exist

Email already Exist

Name Exist

Name already Exist

length of charactors should be more than 6 digits and email should be valid email or else , it will gives you an error

example

"name" length must be at least 6 characters long
"email" must be a valid email
"password" length must be at least 6 characters long


Login User

POST http://localhost:3000/api/user/login

example json data

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

if success you will get a JWT token

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2MTE0NjU0ZDc3ZjlhNTRlMDBmMDU3NzciLCJuYW1lIjoidGhlYWRtaW4iLCJlbWFpbCI6InJvb3RAZGFzaXRoLndvcmtzIiwiaWF0IjoxNjI4NzI3NjY5fQ.PFJldSFVDrSoJ-Pg0HOxkGjxQ69gxVO2Kjn7ozw9Crg

if Email or password is wrong


"email" must be a valid email
"password" length must be at least 6 characters long
Password is wrong
Email is wrong


Access Private Route

in this private route admin users can see if there admins , and normal users can see if there normal and if you are not authenticated you will get a error

GET http://localhost:3000/api/priv

auth-token ( JWT token got from login ) should be in header


auth-token : eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2MTE0NjU0ZDc3ZjlhNTRlMDBmMDU3NzciLCJuYW1lIjoidGhlYWRtaW4iLCJlbWFpbCI6InJvb3RAZGFzaXRoLndvcmtzIiwiaWF0IjoxNjI4NzI3NjY5fQ.PFJldSFVDrSoJ-Pg0HOxkGjxQ69gxVO2Kjn7ozw9Crg

if you are the admin you will get

{
    "role": {
        "role": "you are admin",
        "desc": "this is only for admin"
    }
}

If you are normal user

{
    "role": {
        "role": "you are normal user",
        "desc": "<user name>
    }
}

If you are not Verified

Access Denied

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.