Skip to content

Latest commit

 

History

History
59 lines (41 loc) · 1.01 KB

File metadata and controls

59 lines (41 loc) · 1.01 KB

Authentication with React

gandalf

Clone The repo

In this repo you have an express app that have a login route it was what we did in lesson-week_11-day_01-expres-auth-with-passport but with a little bit of refactoring

Run the backend Server

npm install
npm start

React App

Install dependencies

npm install
npm install jwt-decode

Run the app

npm start

add a service

import jwtDecode from "jwt-decode";

const tokenKey = "authToken";

function setJwt(token) {
  localStorage.setItem(tokenKey, token);
}

function getJwt() {
  return localStorage.getItem(tokenKey);
}

function logout() {
  localStorage.removeItem(tokenKey);
}

function getUser() {
  try {
    const jwt = localStorage.getItem(tokenKey);
    return jwtDecode(jwt);
  } catch (ex) {
    console.log(ex);
  }
}