简体   繁体   English

如何检查用户是否使用 chrome identity api 登录?

[英]how to check if a user is logged in using chrome identity api?

I am trying to add authentication to my chrome extension and I am using chrome identity-api for that.我正在尝试向我的 chrome 扩展程序添加身份验证,并且为此使用了 chrome identity-api。 I am using the getAuthToken API for fetching the access token.我正在使用 getAuthToken API 来获取访问令牌。 I want to provide login/logout functionality in my popup.html such that login button is displayed only when the user is not logged in (or logged out).我想在 popup.html 中提供登录/注销功能,以便仅在用户未登录(或注销)时显示登录按钮。 However, I am not able to figure out a way to check if the user is logged in or not.但是,我无法找出一种方法来检查用户是否已登录。

You need to check chrome.runtime.lastError你需要检查chrome.runtime.lastError

I'm doing this using interactive: false when my popup loads.当我的弹出窗口加载时,我正在使用interactive: false来执行此操作。

If the user isn't logged in, the call to getAuthToken will fail and my function will return.如果用户未登录,对getAuthToken的调用将失败,我的函数将返回。 If the user is logged in, I display a logged in element and display a logout button.如果用户已登录,我会显示一个登录元素并显示一个注销按钮。

If a user isn't logged in, I have a sign in with google button for them to click.如果用户未登录,我会使用谷歌按钮登录,供他们点击。

window.onload = () => {
  console.log("starting popup.js");

  chrome.identity.getAuthToken({ interactive: false }, function (token) {
    // If the user isn't logged in yet, make them login via clicking button
    if (chrome.runtime.lastError) {
      // Something went wrong
      console.warn("Whoops.. " + chrome.runtime.lastError.message);
      // Maybe explain that to the user too?
      return;
    }
    document.getElementById("logged-in").style.display = "block";
    document.getElementById("btn-logout").disabled = false;

};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM