Home > OS >  React: trigger re-auth after some time
React: trigger re-auth after some time

Time:11-24

So I am working on this app, and when I log in I put my accessToken and some basic infos to the localStorage. the API i'm working on doesnt

Now what I want to happen is to prompt a modal 'fill in the password to continue' in a set amount of time lets say 1hr which is like a re-auth thing for the users.

I already have my modal, things I tried was putting a 'setTimeout' within the login function to fire the prompt in an amount of time - yeah works but if I try to refresh the page before the time then it won't.

Next was I tried putting the same function to a useEffect on my homepage - it also works but when I refresh the page, it won't work the way it should because it resets every refresh.

So what are my options here? halp! should I try putting in the localstorage or variable, the time the user logged in? and a time when it should expire? if so how will I trigger the function to run the check?

CodePudding user response:

Why don't you add the next time the prompt should appear(say one hour from now) to the basic info in localStorage? Then use setInterval (instead of setTimeout) to periodically check the localStorage if that saved time has been reached? Then display the modal(and delete the accessToken) if the current time is greater than that.

  • Related