Auto login
To give our web users a user-friendly web app when logging in, we will create an auto login. The idea is that whenever a token is present in local storage, we will check if it's a valid token and then use it for authentication. By doing this, our web visitors don't need to reenter their login details, which is sometimes annoying for them.
To start, let's update the auth.service.js
file of the src/auth
folder and then import jsonwebtoken
, like so:
import * as jwt from "jsonwebtoken";
We will use the JWT loken later to decode the token.
Now, add two new functions to the auth.service.js
file:
export function isTokenFromLocalStorageValid() { const token = localStorage.getItem(key); if (!token) { return false; } const decoded = jwt.decode(token); const expiresAt = decoded.exp * 1000; const dateNow = Date.now(); return dateNow <...