0

在我的 Chrome 扩展程序中,我使用launchWebAuthFlow通过他们的 Google 帐户对用户进行身份验证:

function launchGoogleAuthFlow(interactive) {
  return new Promise((resolve, reject) => {
    const manifest = chrome.runtime.getManifest();
    const clientId = encodeURIComponent(manifest.oauth2.client_id);
    const scopes = encodeURIComponent(manifest.oauth2.scopes.join(' '));
    const redirectUri = encodeURIComponent('https://' + chrome.runtime.id + '.chromiumapp.org');

    const url = 'https://accounts.google.com/o/oauth2/auth' +
      '?client_id=' + clientId +
      '&response_type=id_token' +
      '&access_type=offline' +
      '&redirect_uri=' + redirectUri +
      '&scope=' + scopes

    chrome.identity.launchWebAuthFlow(
      {
        'url': url,
        'interactive': interactive
      },
      (redirectedTo) => {
        if (chrome.runtime.lastError) {
          console.log(chrome.runtime.lastError.message);
          resolve(null)
        }
        else {
          console.log(redirectedTo);
          console.log(JSON.stringify(redirectedTo));
          let idToken = redirectedTo.substring(redirectedTo.indexOf('id_token=') + 9)
          idToken = idToken.substring(0, idToken.indexOf('&'))
          resolve(idToken)
        }
      }
    )
  })
}

初次登录后,我可以使用interactive: false自动再次登录用户。但在浏览器完全关闭后,他们必须再次以交互方式登录。 我必须自己实现刷新令牌逻辑吗?

4

0 回答 0