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, a…


This content originally appeared on DEV Community and was authored by Md Shah Jalal

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>
  );
};


This content originally appeared on DEV Community and was authored by Md Shah Jalal


Print Share Comment Cite Upload Translate Updates
APA

Md Shah Jalal | Sciencx (2021-12-24T09:51:33+00:00) CURD operations, NodeJs, JWT. Retrieved from https://www.scien.cx/2021/12/24/curd-operations-nodejs-jwt/

MLA
" » CURD operations, NodeJs, JWT." Md Shah Jalal | Sciencx - Friday December 24, 2021, https://www.scien.cx/2021/12/24/curd-operations-nodejs-jwt/
HARVARD
Md Shah Jalal | Sciencx Friday December 24, 2021 » CURD operations, NodeJs, JWT., viewed ,<https://www.scien.cx/2021/12/24/curd-operations-nodejs-jwt/>
VANCOUVER
Md Shah Jalal | Sciencx - » CURD operations, NodeJs, JWT. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/24/curd-operations-nodejs-jwt/
CHICAGO
" » CURD operations, NodeJs, JWT." Md Shah Jalal | Sciencx - Accessed . https://www.scien.cx/2021/12/24/curd-operations-nodejs-jwt/
IEEE
" » CURD operations, NodeJs, JWT." Md Shah Jalal | Sciencx [Online]. Available: https://www.scien.cx/2021/12/24/curd-operations-nodejs-jwt/. [Accessed: ]
rf:citation
» CURD operations, NodeJs, JWT | Md Shah Jalal | Sciencx | https://www.scien.cx/2021/12/24/curd-operations-nodejs-jwt/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.