Home > OS >  Keep User Logged in on Refresh with Cookie
Keep User Logged in on Refresh with Cookie

Time:05-28

I'm trying to make it so that a Google OAuth user doesn't have to log in every time they refresh the page. I have a cookie stored that saves the login token and is checked on page load. However, it still makes the user log in when the page is refreshed. I've attached the function which handles logins below, but if you want to see the full code for HTML and JS, I've also attached those as well. If anyone could help, it would be deeply appreciated.

Login Function: https://pastebin.com/6rguG783

function handleAuthClick() {
    tokenClient.callback = async (resp) => {
        if (resp.error !== undefined) {
            throw (resp);
        }
        await listUpcomingEvents();
    };
 
    if (document.cookie === null) {
        tokenClient.requestAccessToken({prompt: 'consent'});
    } else {
        tokenClient.requestAccessToken({prompt: ''});
    }
    setTimeout(() => document.cookie = `token=${gapi.client.getToken()}; expires=Tue, 19 Jan 2038 04:14:07 GMT`, 20000);
}

Full JS: https://pastebin.com/7vcaqZXG

Full HTML: https://pastebin.com/eDyT4N2X

CodePudding user response:

Instead of cookies use local storage it will fix the probleme

CodePudding user response:

You can use local storage instead of cookies like this

window['localStorage'].setItem('token', gapi.client.getToken()).
  • Related