Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement remember me functionality for authentication in ReactJS when i only receive jwt token from backend api

I currently have to implement remember functionality for remembering my login info on my frontend website. How to implement remember me functionality for authentication in ReactJS when i only receive jwt token from backend API.

like image 603
Bryan Lumbantobing Avatar asked Mar 07 '26 18:03

Bryan Lumbantobing


2 Answers

Once you receive a jwt token, you can set an "expiry time" for your jwt token. For example, if I receive a token with id: "abc_token_123", I will create an object inside sessionstorage, localstorage, or even cookies with a key called expireTime (for example). And I will use a useEffect hook on the main file (App.js) to watch for the time, if the time exceeds the expiry time, log the user out, otherwise, if the expiry key is present inside your storage, keep the user logged in.

like image 103
CookieMaster Avatar answered Mar 10 '26 08:03

CookieMaster


Store the JWT in browser Local Storage if the user chooses the remember me option. Assuming you can change the backend JWT expiry, make the JWT expiry longer (whatever is an acceptable number of days between logins) where the user has chosen remember me.

like image 20
Hopey One Avatar answered Mar 10 '26 07:03

Hopey One