本文介绍了当用户退出我的 Firebase 应用程序时,为什么它不也从身份验证提供商处退出,比如 Google?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Google 作为身份验证提供商来登录我的应用.我的代码调用了重定向到登录页面的 Firebase 注销方法,但是当用户再次单击 Google 登录按钮时,它会自动进行身份验证并登录到应用程序而不提示用户.这是登录代码:

I am using Google as the auth provider to sign in with my app. My code calls the Firebase sign out method which redirects to the login page, but when user again clicks on the Google sign in button, it automatically authenticates and logs in into the app without prompting the user. Here is the code for sign-in:

 $('#GooglePluseLogin').click(function (e) {
        showLoaderPF(true);
        if (!firebase.auth().currentUser) {

            var provider = new firebase.auth.GoogleAuthProvider();
            provider.addScope('https://www.googleapis.com/auth/plus.login');
            firebase.auth().signInWithRedirect(provider);


        } else {
            firebase.auth().signOut();
            showLoaderPF(true);
        }
    });

还有,这里是注销代码:

And, here is the code for signout:

firebase.auth().signOut().then(function () {
            debugger;
            localStorage.clear();
            deleteAllCookies();

           // firebase.auth().unauth();

            window.location.href = "index.html";

        }, function (error) {
            showLoaderPF(false);
            console.error('Sign Out Error', error);
        });

推荐答案

主要答案:

@Shib 提供的答案基本上是正确的,但没有为到达这里的每个人提供足够的背景信息,所以让我们我详细说明.当您的代码调用 firebase.auth().signOut() 时,您会从您的应用中退出 Firebase,而不是整体上的 auth 提供程序(更多信息在下面的链接中),因为您真的不想在其他标签中退出 Gmail 和其他 Google,对吗?

The answer provided by @Shib is basically correct but doesn't provide enough context for everyone arriving here, so let me elaborate. When your code calls firebase.auth().signOut(), you sign out of Firebase from your app, but not the auth provider overall (more in links below) because you don't really want to be signed out of your Gmail and other Google in your other tabs, right?

您遇到的问题只有在您的浏览器缓存中只有一个单一 Google 登录可用时才会出现.如果是这种情况,下次您使用 Firebase auth 登录时,它会自动重用相同的 Google 凭据(因为您没有退出 Google,而且它是)以使用户能够通过更少的鼠标点击更快地登录.

The issue you're running into only occurs if only a single Google login is available in your browser cache. If this is the case, the next time you use Firebase auth to login, it will automatically reuse the same Google credentials (since you didn't sign out of Google, and also, it's) to enable users to login faster with fewer mouse clicks.

IOW,对于过去使用 >1 个 Google/Gmail 帐户登录的用户来说,这不是问题——这些用户将获得预期的帐户选择器对话框.如果您只有一个 Google 登录名并且想要查看帐户选择器,则需要设置 prompt 自定义参数select_account,如@Shib 所述:

IOW, it's not an issue for users who have signed-in with >1 Google/Gmail accounts in the past -- these users will get the expected account-picker dialog. If you only have a single Google login available and want to see the account-picker, you need to set the prompt custom parameter to select_account as @Shib noted:

var provider = new Firebase.auth.GoogleAuthProvider();
  provider.setCustomParameters({
    prompt: 'select_account'
  });

要了解有关通过 Firebase 身份验证进行 Google 登录的更多信息,请参阅this页.这是一篇帖子,更详细地描述了这种行为.以上,@linasmnew @bojeil 为什么他们评论说这是默认的/预期的(不退出身份验证提供程序)行为,所以这里有一个线程提供 解释.

To learn more about doing Google sign-in from Firebase auth, see this page. Here's a post which describes this behavior in more detail. Above, @linasmnew asked @bojeil why they commented that this (not signing out of the auth provider) is the default/expected behavior, so here is a thread providing that explanation.

第 2 部分(FirebaseUI 库使用仅):

如果您正在使用这个便利库(它位于 Firebase 身份验证之上),如果您只有一个 Google 凭据,您也会遇到此问题,但解决问题的方法可能并不那么简单.该库的目的是提供一个可重用的预构建 UI,让用户可以从各种身份验证提供程序中进行选择 减少开发人员的时间并提供一致的用户体验.

If you're using this convenience library (which sits atop Firebase auth), you'll also run into this if you have a single Google credential, but it may not be as straightforward how to fix the issue. The purpose of this library is to provide a reusable prebuilt UI letting users choose from various auth providers to reduce developer time and provide a consistent user experience.

开发人员没有像上面的主要答案那样实例化特定的身份验证提供程序类,而是 在他们的 signInOptions 配置中列出支持的提供者 ID(特别是第 3 步).(Python 3 Google App Engine构建应用程序"快速入门示例 使用 FirebaseUI,这就是遇到这个问题的地方.)

Instead of instantiating a specific auth provider class like in the main answer above, developers list out the supported provider IDs in their signInOptions config (specfically step 3). (The Python 3 Google App Engine "building an app" Quickstart example uses FirebaseUI, and that's where I ran into this issue.)

例如,如果您决定只使用 Google 和电子邮件身份验证,则这些选项如下所示:

For example, if you decide on using just Google and email auth, those options look like this:

signInOptions: [
  firebase.auth.GoogleAuthProvider.PROVIDER_ID,
  firebase.auth.EmailAuthProvider.PROVIDER_ID,
  //firebase.auth.FacebookAuthProvider.PROVIDER_ID,
  //firebase.auth.TwitterAuthProvider.PROVIDER_ID,
  //firebase.auth.GithubAuthProvider.PROVIDER_ID,
  //firebase.auth.PhoneAuthProvider.PROVIDER_ID
],
            . . .

这正是 App Engine 示例所做的,并且 这是代码.(这个 FirebaseUI JS 也可以嵌入 HTML.) 当这个错误(嗯,功能")抬头 此处,添加prompt 自定义参数以强制帐户选择器显示如下:

This is exactly what the App Engine sample does, and here's the code. (This FirebaseUI JS can also be embedded in HTML.) When this bug (um, "feature") rears its undesired head here, add that prompt custom parameter to force the account-picker to show up like this:

signInOptions: [
  //firebase.auth.GoogleAuthProvider.PROVIDER_ID,
  { provider: firebase.auth.GoogleAuthProvider.PROVIDER_ID,
      customParameters: { prompt: 'select_account' },
  }
  firebase.auth.EmailAuthProvider.PROVIDER_ID,
  //firebase.auth.FacebookAuthProvider.PROVIDER_ID,
  //firebase.auth.TwitterAuthProvider.PROVIDER_ID,
  //firebase.auth.GithubAuthProvider.PROVIDER_ID,
  //firebase.auth.PhoneAuthProvider.PROVIDER_ID
],
            . . .

这篇关于当用户退出我的 Firebase 应用程序时,为什么它不也从身份验证提供商处退出,比如 Google?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 22:59
查看更多