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 <...