-1

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

Wilson T.
  • 11
  • 1
  • 4

1 Answers1

-1

You can use local storage instead of cookies like this

window['localStorage'].setItem('token', gapi.client.getToken()).
donwiktorb
  • 11
  • 2
  • I've changed the code to this: https://pastebin.com/MLAFgT05, but it still gives me the same issue. Why do you think this is? And thank you for your help, it means a lot. – Wilson T. May 28 '22 at 00:08
  • Can you check with devtools (f12 -> Storage -> Local Storage) if token exists there for sure? – donwiktorb May 28 '22 at 13:26
  • The localStorage item is there. However, token is listed as `[object Object]`. So, I added `JSON.stringify()` to `gapi.client.getToken()`. Now, token is listed as this: https://pastebin.com/F60Vmvs9. I noticed that it contains a "expires_in" property. Could this be the issue? It says 3599 for the expiration. – Wilson T. May 28 '22 at 14:29