Code Monkey home page Code Monkey logo

tokenbased_authapi's Introduction

Authentication API with JWT Token in Node.js

In the this code I have implemented jsonwebtoken to learn how to use JWT in Node.js to secure endpoints and even authenticate users.

Basics of Authentication, Authorization, and JWT

So when we talk about Authentication and Authorization it is used in security, particularly when it comes to getting access to a system. In simple terms gaining entry into a house is authentication and what you can do while inside is authorization.

Authentication

Authentication is the process of verifying a user’s identification through the acquisition of credentials and using those credentials to confirm the user’s identity. The authorization process begins if the credentials are legitimate. The authorization process always follows the authentication procedure.

Authorization

Authorization is the process of allowing authenticated users access to resources by determining whether they have system access permissions. By giving or denying specific licenses to an authenticated user, authorization enables you to control access privileges.

So, authorization occurs after the system authenticates your identity, granting you complete access to resources such as information, files, databases, funds, places, and anything else. That said, authorization affects your capacity to access the system and the extent to which you can do so.

JWT Token

JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.

JWT.IO allows you to decode, verify and generate JWT.

Installation

We will use the node package manager to install a few dependencies like express,jsonwebtoken,dotenv.

npm i express jsonwebtoken dotenv

Usage

To start the server

npm start

There are three routes:

  • GET /api --> This is unprotected route
  • POST /api/signup --> This route will generate a token for user with given username and password, you can decode the token at: https://jwt.io.
  • POST /api/login --> When user passes the token along with bearer {token} in Headers and try to access the route user will be able to access the route and user data will be displayed.
  • POST /api/updatepassword --> When user provide new password along with username a new token is generated and user can login with newly generated Token.
Key Value
Authorization string

Note: This code is for my self learning hence it's really simple so that I can learn the main concept.
In future this will be implemented to protect the authenticated routes like /register, /signup, /checkout etc.

Thank You
Happy Coding,
Learn,Code and Earn
Stay Safe and Stay Positive :)

tokenbased_authapi's People

Contributors

anshul-sonpure avatar

Stargazers

 avatar

Watchers

 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.