Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 24, 2021 09:51 am GMT

CURD operations, NodeJs, JWT

What is the CURD operations?

When we create a project with React as a client site and with NodeJs as a server site, we have to process some operations on the server site with NodeJs. CURD is an acronym that stands for Create, Update, Read, and Delete. According to our needs, We use to get, post, put, delete methods.

What is NodeJs?

Node.js is an open-source, cross-platform, back-end, JavaScript runtime build on Chrome V8 engine that can execute JavaScript code outside of web browser.

Node.js runs single-threaded, non-blocking, asynchronous programming, which is very memory efficient.

Important Note:

  • NodeJs is a separate or another runtime of JavaScript.
  • NASA, Uber, IBM, Paypal use NodeJs.
  • The V8 engine makes JavaScript fast Just in time (JIT) compile.
  • npm stands for Node Package Manager.
  • MongoDB keeps the data in JSON structure.

What can NodeJs do:

  • Node.js can generate the dynamic page content
  • Node.js can create, open, read, write, delete, and close files on the server
  • Node.js can collect form data
  • Node.js can add, delete, modify data in your database

What is JWT?

Most web apps use security measures to make sure user data stays private. Authentication is a key part of security and JSON Web Tokens (JWT) are a great way to implement authentication.

So what are JSON Web Tokens?

JWT is a standard that defines a compact and self-contained way to securely transmit information between a client and a server as a JSON object. The compact size makes the tokens easy to transfer through an URL, POST parameter, or inside an HTTP header. Also, since they are self-contained they include all the necessary information about a user so the database does not need to be queried more than once.
The information in a JWT can be trusted because it is digitally signed using a secret or public/private key pair.

Authentication

JWT is mainly used for authentication. After a user logs in to an application, the application will create a JWT and send it back to the user. Subsequent requests by the user will include the JWT. The token tells the server what routes, services, and resources the user is allowed to access. JWT can be easily used across multiple domains so they are often used for Single Sign-On.

Install

npm install react-jwt
or
yarn add react-jwt

Usage

import React from "react";import { useJwt } from "react-jwt";const token = "Your JWT";const Example = () => {  const { decodedToken, isExpired } = useJwt(token);  /*    If is a valid jwt, 'decodedToken' will be a object    it could look like:    {      "name": "Gustavo",      "iat": 1596408259,      "exp": 4752168259    }    'isExpired' will return a boolean    true => your token is expired    false => your token is not expired  */  return (    <div>      ...    </div>  );};

You can also use the methods isExpired(token) and decodeToken(token)

import React from "react";import { isExpired, decodeToken } from "react-jwt";const token = "Your JWT";const Example = () => {  const myDecodedToken = decodeToken(token);  const isMyTokenExpired = isExpired(token);  return (    <div>      ...    </div>  );};

Refresh state

If you use the refreshToken(newToken) method, useJwt's state will be updated

import React from "react";import { useJwt } from "react-jwt";const token = "Your JWT";const Example = () => {  const { decodedToken, isExpired, reEvaluateToken } = useJwt(token);  const updateToken = () => {    const newToken = "A new JWT";    reEvaluateToken(newToken); // decodedToken and isExpired will be updated  }  return (    <div>      ...    </div>  );};

Original Link: https://dev.to/programmershahjalal/curd-operations-nodejs-jwt-pel

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To