Git Product home page Git Product logo

basic_jwt_auth's Introduction

Basic_jwt_auth

This is just part of web security Some helpful code :

1. signing jwt(sending jwt from server to client)

To achieve this:

  1. const payload = { id: user.id, name: user.name };
    let token = jwt.sign(payload, process.env.SECRET, { expiresIn: '1m' });
    res.json({
    accessToken: token,
    message: "Logined succesfully"
    });
  2. axios.post('http://localhost:3001/login', { name: name, password: pass })
    .then(response => {
    setmessage(response.data.message);
    if (response.data.accessToken) {
    localStorage.setItem("jwt", JSON.stringify({ token: response.data.accessToken }));
    }
    history.push("/");
    })
    .catch(error => console.error('There was an error!', error));

2. verifying jwt(sending jwt from client to server)

Method 1: work like a charm (successful or effective)

let token = localStorage.getItem("jwt");
let decodedToken = jwt_decode(token);
let currentDate = new Date();
// JWT exp is in seconds
if (decodedToken.exp * 1000 < currentDate.getTime()) {
localStorage.removeItem("jwt")
console.log("Token expired.");
return false;
} else {
console.log("Valid token");
return true;
}

Method 2:

  1. function checkAuth() {
    const token = JSON.parse(localStorage.getItem("jwt")).token;
    const headers = {
    'Authorization': token
    };
    axios.get('http://localhost:3001/checkauth', { headers })
    .then(response => {
    console.log(response);
    if(response.data.message!=null)
    setmessage(response.data.message);
    else if(response.data.name!=null)
    setmessage(response.data.name);
    setTimeout(() => {
    setmessage("");
    }, 1000);
    })
    .catch(error => console.error('There was an error!', error));
    }

  2. const token = req.headers.authorization;
    if (!token) {
    console.log("there is no token came from frontend");
    }
    else {
    jwt.verify(token, process.env.SECRET, function (err, decoded) {
    if (err) {
    console.log(err);
    res.send(err);
    }
    else {
    console.log(decoded);
    res.send(decoded);
    }
    });
    }

3. redirecting to private page just after clicking login submit button

To achieve this:
a. import { useHistory } from 'react-router';
b. const history = useHistory();
c. history.push("/");

4. redirecting to login page just after clicking logout submit button

To achieve this:
a. import { useHistory } from 'react-router';
b. const history = useHistory();
c. history.push("/login");

basic_jwt_auth's People

Contributors

29ravikant-akash avatar

Watchers

 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.