Search code examples
javascripthtmlgoogle-signingoogle-identity

Google Identity Services - stay signed in


I'm migrating from the Google sign-in platform library to the new Google Identity Services with one-tap sign in, but the website automatically logs the user out when they refresh or go to a different page.

This is the code that's present on every page that loads the one-tap sign in:

<div id="g_id_onload"
     data-client_id="[CLIENT ID]"
     data-callback="onSignIn"
     data-auto_select="true">
</div>

Removing this just keeps the user logged out with no way to log back in. There is a cookie, g_state, that stores the user's login, as well as attributes to display the popup based on its presence, but working with this also keeps the user logged out with no way to log back in.

Is there any way to prevent automatic logout on refresh?


Solution

  • Setting a cookie to track user sign-in status to your site should do it.

    A few things to be aware of:

    • Your callback handler will manage the signed-in or signed-out status for users, here OnSignIn.
    • The cookie name set by data-skip_prompt_cookie is used to suppress One Tap after the user successfully signs into your website -- you've already logged in right, so stop bothering me with One Tap prompts. So, after someone visits any page and signs in, you'll issue a cookie so that One Tap isn't displayed when the next page is loaded. You'd want to clear this cookie when they sign out of your website.
    • The data-skip_prompt_cookie helps you control when to display One Tap when you're using static HTML, when using JS you'd choose to display One Tap by calling google.accounts.id.prompt or skip calling it and displaying One Tap.
    • Avoid using or referencing g_state entirely, it currently helps manage how to display the UI and isn't intended to be used as a means to try and track signed-in or session status. Instead, track user sign-in using your own cookie and data-skip_prompt_cookie.