检查用户是否已通过密码验证或已通过某些提供程序 [英] Checking user is password authenticated or with some provider

查看:43
本文介绍了检查用户是否已通过密码验证或已通过某些提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中具有简单的用户名和密码,Facebook和Google身份验证.我想对经过身份验证的密码用户执行不同的操作.

I have simple user and password, Facebook and Google authentication in my application. And I want to do different actions on the password Authenticated user.

代码

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user != null) {
        for (UserInfo profile : user.getProviderData()) {
            // Id of the provider (ex: google.com)
            String providerId = profile.getProviderId();
            if(providerId.equals("facebook.com") | providerId.equals("google.com"))
            {
                Toast.makeText(this,"fb or goole method is used",Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(this,"Simmple method is used",Toast.LENGTH_SHORT).show();

            }
        }

问题:if和else都在循环中执行.有解决方案吗?

Problem: Both if and else get executed in the loop. Any Solution?

推荐答案

如果您使用以下if语句:

If you are using the following if statement:

if(providerId.equals("facebook.com") | providerId.equals("google.com")) {
    Toast.makeText(this,"fb or goole method is used",Toast.LENGTH_SHORT).show();
} else {
    Toast.makeText(this,"Simmple method is used",Toast.LENGTH_SHORT).show();
}

这意味着,如果 providerId 不是 facebook.com google.com ,则表示用户已使用用户名和密码进行了身份验证,但是此声明是 inccorect !在这两种情况下,都使用用户名和密码对用户进行身份验证.如果使用 facebook.com ,则将使用来自Facebook的凭据对用户进行身份验证;如果使用 google.com ,则将使用即将使用的凭据对用户进行身份验证.来自 GoogleSignInAccount 对象.

It means that if the providerId is not facebook.com or google.com it means that the user is authenticated with user and password but this statement is inccorect! In both cases, the user is authenticated with user and password. In case of facebook.com the user will be authenticated with the credentials that are coming from facebook and in case google.com the user will be authenticated with the credentials that are coming from GoogleSignInAccount object.

因此,如果您使用该if语句,会发生什么情况,首先它将评估是否已使用用户名和密码对用户进行身份验证,这显然是因为 user!= null ,然后它会尝试查找 providerId ,可以是 facebook.com google.com .这就是为什么显示Toast消息的顺序是使用 Simmple方法和使用第二个 fb或goole方法.

So what is happening if you are using that if statement, first it evaluates if the user is authenticated with user and password which obviously is since user != null and second will try to find the providerId, which can be facebook.com or google.com. That's why the order of displaying the Toast messages is Simmple method is used and second fb or goole method is used.

编辑:我想到的最简单的方法是拥有三个不同的身份验证部分,并跟踪用户访问了哪一个.如果用户按下google登录按钮,那么他将使用google等其他类型的登录信息,

The simples way I can think of is to have three different authentication sections and to track which one was accessed by the user. If the user is pressing the google sign-in button then he is logged in with google and so on for the other types,

这篇关于检查用户是否已通过密码验证或已通过某些提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆