Extraterrestrial
Extraterrestrial

Reputation: 612

How to stay Signed In for Gmail api without NodeJs?

I want to get the messages of users by gmail api. For that google authorization is needed. I managed to authorize the user by following code -

      let authBtn = document.getElementById('authorize_button');

      const CLIENT_ID = 'XXXXXX-XXXXXXXXXXX.apps.googleusercontent.com';
      const API_KEY = 'XXXXXX-XXXXXXXXXXXXXXX';

      const DISCOVERY_DOC = 'https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest';

      const SCOPES = 'https://www.googleapis.com/auth/gmail.readonly';

      let tokenClient;
      let gapiInited = false;
      let gisInited = false;

      authBtn.style.visibility = 'hidden';

      function gapiLoaded() {
        gapi.load('client', intializeGapiClient);
      }

      async function intializeGapiClient() {
        await gapi.client.init({
          apiKey: API_KEY,
          discoveryDocs: [DISCOVERY_DOC],
        });
        gapiInited = true;
        maybeEnableButtons();
      }

      function gisLoaded() {
        tokenClient = google.accounts.oauth2.initTokenClient({
          client_id: CLIENT_ID,
          scope: SCOPES,
          callback: '',
        });

        gisInited = true;
        maybeEnableButtons();
      }

      function maybeEnableButtons() {
        if (gapiInited && gisInited) {
          authBtn.style.visibility = 'visible';
        }
      }

      function handleAuthClick() {
        tokenClient.callback = async (resp) => {
          if (resp.error !== undefined) throw (resp);

          authBtn.innerText = 'Refresh';
          await getMessages();
        };

        if (gapi.client.getToken() === null) {
          tokenClient.requestAccessToken({prompt: 'consent'});
        } else {
          tokenClient.requestAccessToken({prompt: ''});
        }
      }

In above code gapi.client.getToken() === null is always false. Everytime I refresh the page I have to reauthorize user with prompt: 'consent'. I also want user to stay signed in until user sign out. How can I achieve by modifying the above code? Can Please someone help me?

Upvotes: 3

Views: 466

Answers (1)

Stof
Stof

Reputation: 670

You are using a system that requires a server-side authentication flow, read about properly handling that here:

https://developers.google.com/identity/gsi/web/guides/verify-google-id-token

The gapi JavaScript is browser code (you obviously know this because the question specifies all sorts of DOM related code), and therefore Authentication is fundamentally not going to be possible entirely in the browser without a server-side flow to handle the callbacks from Google that occur out-of-band from the browser.

The only exception I can find to the rule of having a server-side component is to credential manager API:

https://developers.google.com/identity/gsi/web/guides/display-browsers-native-credential-manager

It seems to significantly simplify things, but from what I can tell supports Chrome only (maybe including chrome-based browsers Edge, Brave, etc. but maybe not Chromium as it seems to be needing Google accounts in the browser itself, e.g. login is not managed by your code for your website but the user using the browser directly before they visit your site)

Upvotes: 1

Related Questions