node.js project based jwt-auth / register-login-logout

Overview

JSON Web Token / Authentication-Authorization

JSON Web Token allows us to check Authorization (Session Control). JSON Web Token (JWT) is an open standard defined in RFC 7519 based on JSON that enables data exchange and authentication between parties. For example, a server can generate a key (token) indicating that the user has administrative privileges and send it to the user. The user can then use the administrative authority defined for him with this key on a client and his authority can be verified by all parties.

JSON Web Token has 2 parts, encoded and decoded. Encoded means encrypted, decoded means decrypted. When encoded, 3 parts emerge:

  1. Header specifies which algorithm will be used in the signature to be created. HS256 indicates that the algorithm is signed with HMAC-SHA256.

header = '{"alg":"HS256","typ":"JWT"}'

  1. Payload contains unique information between parties. iat carries information including the time the key was created and is a suggested use in JWT. Signature, on the other hand, occurs when the header and payload are encoded with a base64url (RFC 4648 §5), combined with a dot symbol, and then encrypted with a secret key.
payload = '{"loggedInAs":"admin","iat":1422779638}'

key           = 'secretkey'
unsignedToken = encodeBase64Url(header) + '.' + encodeBase64Url(payload)
signature     = HMAC-SHA256(key, unsignedToken)
  1. Verify Signature: When the three parts come together and the Signature is encoded with base64url, the key (token) emerges. Its output consists of reliable base64url-encoded characters that can be easily integrated into HTML and HTTP environments. Typical cryptographic algorithms are SHA-256 (HS256) with HMAC and SHA-256 (RS256) with RSA. In this part, the secret key that we have determined can also be used.
token = encodeBase64Url(header) + '.' + encodeBase64Url(payload) + '.' + encodeBase64Url(signature)  
key: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dnZWRJbkFzIjoiYWRtaW4iLCJpYXQiOjE0MjI3Nzk2Mzh9.gzSraSYS8EXBxLN_oWnFSRgCzcmJmMjLiuyu5CSpyHI

Usage

During authentication, if the information sent by the user is also verified, a key (token) is generated and returned to the user and stored in this locale (for example, cookies or web storage). In cases where user authentication is required, an Authorization header with Bearer scheme is sent to the server by the client. Header content will look like this:

Authorization: Bearer eyJhbGci......yu5CSpyHI

This is stateless authentication method and user state is never saved in server memory. The server always checks if the JWT sent from the Authorization header is valid and if it is, the user is allowed to access the protected resources. Since all the information is in the JWT, it reduces the need to query the database more than once.

Override

JWTs can actually be remotely overridden in a number of ways. The first of these methods is to define a lifetime for the token. When this time expires, the key is rejected and becomes invalid. If the system user decides that a key previously generated for them should no longer work, they will want to be able to disable it remotely. In such a scenario, a rather short lifetime should be defined for the keys. When the key expires, the client mechanism requests a key from the server by forwarding the old key for renewal. The server checks the validity of the old key. If it is trusted, it will check if it is on the black list. If it is not blacklisted, it generates and sends a new one from the same key. In such an operation, if the user accesses the system from another device and sends a key to the blacklist, the key will no longer be renewed and become invalid in the next renewal period. In this method, a key can be remotely invalidated after its lifetime (or less) at worst.


Usage in this Project

  • "jsonwebtoken" package is used in this repository and JSON Web Token operations are performed.

  • At the api/auth/register endpoint the user registers and receives a token.

  • At the api/auth/profile endpoint the getAccessToRoute middleware runs and the user token is decrypted. If the token period is still valid, the pass is granted.

  • At the api/auth/logout endpoint, the user logs out and the token expires.

Read its documentation to learn how to use "jsonwebtoken".

You might also like...

A dead simple JWT-based authentication library for Next.js framework.

nexauth A dead simple JWT-based authentication library for Next.js framework. Getting Started Visit https://betagouv.github.io/nexauth/#/install to ge

Aug 2, 2022

Nest.js project used it implement login page functionality.

Basic Login Backend (API) Description This project is the backend (API) portion of a basic login application. It calls a local frontend project basic-

Mar 21, 2022

📝api: Register Employees and Products

📝api: Register Employees and Products

📝 Register Employees and Products Bem-vindo(a). Este é o desafio da 4° sprint! Opa dev tudo bem?! Este é o grupo03, que através desse repositório vem

Jan 26, 2022

A progressive web app to help volunteers track their volunteering activities, register vet concerns and log incident reports.

POOPS - Pets of Older Persons 2022 Introduction This is a Next.js project bootstrapped with create-next-app. Getting Started First, install yarn: npm

Jan 3, 2023

Register your username, log into the app and create your list of a to-do with local storage.

Register your username, log into the app and create your list of a to-do with local storage.

Todofy Todofy is a web app to create your to-do list with local storage. Explore the docs » View Demo · Report Bug · Request Feature Table of Contents

Jun 30, 2022

Ayarlanabilir Register Sistemi (Yakında Moderasyon'da gelicek)

Ayarlanabilir Register Sistemi (Yakında Moderasyon'da gelicek)

Rowy Bots Testleri yapılmıştır, hiçbir hatası bulunmamaktadır. Sizlere Pusha ile geliştirmiş olduğum botlardan biriyle tanıştırmak istiyorum. Botu kod

Dec 8, 2022

We are creating a Library that would ensure developers do not reinvent the wheel anymore as far as Authentication is concerned. Developers can easily register and download authentication codes that suits their need at any point.

We are creating a Library that would ensure developers do not reinvent the wheel anymore as far as Authentication is concerned. Developers can easily register and download authentication codes that suits their need at any point.

#AuthWiki Resource Product Documentation Figma Database Schema First Presentation Live Link API Documentation Individual Contributions User Activity U

Dec 2, 2022

End-to-End type safety for REST APIs written in Fastify. Only problem is you have to explicity export and register route handlers. LOL

Chino intelligence in japaneese End-to-End type safety for REST APIs written in Fastify. Only problem is you have to explicity export and register rou

Sep 12, 2022

may-be.gay is a service in which you can register your own sub-domain for your personal website

may-be.gay is a service in which you can register your own sub-domain for your personal website

may-be.gay is a service in which you can register your own sub-domain for your personal website. How to register New method (Recommended) Create a new

Dec 27, 2022
Owner
mert
full stack developer (html5, css3, javascript, es6+, react.js - node.js, express.js, mongodb - docker, redis, rabbitmq)
mert
React.js Login, Logout, Registration example with JWT and HttpOnly Cookie

React Login and Registration example with JWT and HttpOnly cookie For more detail, please visit: React Login and Registration example with JWT and Htt

null 37 Dec 24, 2022
Angular 14 JWT Authentication & Authorization with Web API and HttpOnly Cookie - Token Based Auth, Router, Forms, HttpClient, BootstrapBootstrap

Angular 14 JWT Authentication with Web API and HttpOnly Cookie example Build Angular 14 JWT Authentication & Authorization example with Web Api, HttpO

null 20 Dec 26, 2022
This Application provides basic authentication features like you can register and create account and then login and access your profile.

Authentication API This Application provides basic authentication features like you can register and create account and then login and access your pro

Rohan Kulkarni 1 Jan 17, 2022
A back-end web app allows you to register and login to access a secrets page

A back-end web app allows you to register and login to access a secrets page

Shreya Christiana Malogi 12 Oct 30, 2022
Projeto com validação JWT Tela de Login

ValidacaoJWT This project was generated with Angular CLI version 13.3.6. Development server Run ng serve for a dev server. Navigate to http://localhos

tzappiello 2 Sep 7, 2022
Base Rails app that includes login, social login, homepage, and basic model for serving as a scaffold app.

Rails7Base I created the Rails7Base as a scaffold application. Countless times, I had to create apps that must have the following features: Login syst

Chim Kan 14 Jul 2, 2022
Learn how to set up Supabase auth for both the frontend and backend of your application using a JWT - JSON web token.

Supabase auth, frontend + backend - example with Next.js Learn how to set up Supabase auth for both the frontend and backend of your application using

YK 7 Nov 20, 2022
Login of app to remind to drink water, using Firebase tools like Firebase Auth and Firebase Firestore

Water Reminder Login App Menu Contents Motivation Final Images How to download the project and run it? Technologies utilized Dev ?? Motivation This ap

Ilda Neta 10 Aug 22, 2022
Auth-Form-Design - Beautiful Auth Form Designed using React 🥰.

?? Auth Form Design ?? Features 1. Check Your Password Strength 2. Can Use Suggested Password 3. Enjoy Responsive Design Getting Started with Create R

Samarpan Dasgupta 2 Dec 24, 2022
Firebase adepter auth process with custom token example in Next Auth

Firebase adepter auth process with custom token example in Next Auth Example of a firebase adapter that works with firebase authentication. A firebase

Low Front 10 Oct 14, 2022