Android Firebase将多个帐户提供商与相匹配的电子邮件相关 [英] Android Firebase linking multiple account providers with matching email

查看:236
本文介绍了Android Firebase将多个帐户提供商与相匹配的电子邮件相关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的firebase仪表板中,我为一个电子邮件选项设置了多个帐户。



Facebook和Google Plus身份验证。



我在LoginActivity中像这样处理每一个:



Google Plus:

  private void firebaseAuthWithGoogle(GoogleSignInAccount acct){
AuthCredential凭证= GoogleAuthProvider .getCredential(acct.getIdToken(),null);
mAuth.signInWithCredential(凭证)
.addOnCompleteListener(this,new OnCompleteListener< AuthResult>(){
@Override
public void onComplete(@NonNull Task< AuthResult> task){
dialog.dismiss();
if(task.isSuccessful()){
//登录成功,用已登录的用户信息更新用户界面
FirebaseUser user = .getCurrentUser();
();
} else {
//如果登录失败,向用户显示一条消息
Toast.makeText(LoginActivity.this, getMessage(),
Toast.LENGTH_SHORT).show();

}

// ...
}
});

Facebook: $ b

  private void handleFacebookAccessToken(AccessToken token){
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
dialog.show();
mAuth.signInWithCredential(凭证)
.addOnCompleteListener(this,new OnCompleteListener< AuthResult>(){
@Override
public void onComplete(@NonNull Task< AuthResult> task){
dialog.dismiss();
if(task.isSuccessful()){
//登录成功,用已登录的用户信息更新用户界面
FirebaseUser user = .getCurrentUser();
();
} else {
Toast.makeText(LoginActivity.this,task.getException()。getMessage(),
Toast.LENGTH_SHORT) .show();
}

}
});

$ b

简单电子邮件:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b @Override
public void onComplete(@NonNull Task< AuthResult> task){
if(task.isSuccessful()){
dialog.dismiss();
proceed() ;
} else {
Toast.makeText(LoginActivity.this,错误的电子邮件或密码,Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
});

现在我想要让拥有与Facebook和Google Plus相同电子邮件的用户能够使用Facebook和Google Plus。



此文档文档说,我应该跳过 FirebaseAuth.signInWith 方法并调用这些函数:

  AuthCredential凭证= GoogleAuthProvider.getCredential(googleIdToken,null); 
mAuth.getCurrentUser()。linkWithCredential(凭证)

现在令人困惑。 我怎么能调用getCurrentUser当它仍然为空,因为我已经跳过signInWith方法?



文档也说,我处理合并,我不明白。

  currentUser = auth.signInWithCredential(凭证).await()。getUser(); 

另外,signInWithCredenial没有 await 方法。

这意味着我应该在登录后使用相同的电子邮件连接多个账户?

解决方案

要连接帐户,应该有一个现有的会话。例如,假设一个新用户创建一个使用Google作为auth提供程序的帐户。



总之,要做到这一点,您需要:




  • 使用GIDSignIn通过Google对用户进行身份验证。
  • 然后,Google返回一个Id令牌(如果一切顺利的话)。
  • 您将使用该标记创建一个GoogleAuthProvider凭证对象。
  • 然后,您使用此凭据在Firebase中调用signInWithCredential进行身份验证。 >


这个过程与Facebook等其他auth提供者类似。要将帐户与Facebook关联起来,您需要执行上述前三个步骤(与 Facebook 认证),而不是signInWithCredential,你将需要调用linkWithCredential。如果一切顺利,现在用户将能够通过Google或Facebook对相同的帐户进行身份验证。



如果您调用signInWithCredential,您将创建一个新帐户使用Facebook作为身份验证提供程序。因此,用户不能访问具有两个(或更多)auth提供者的单个账户,那么该用户对于每个auth提供者将具有两个单独的账户。这就是为什么文档说明您应该跳过调用FirebaseAuth.signInWith

关于合并的问题,文档中提到:如果证书已经链接到另一个用户帐户,那么linkWithCredential的调用将失败。这意味着一个用户已经有一个auth提供者的帐户。如果您希望用户从两个帐户访问信息,则需要创建合并信息从一个帐户到另一个帐户的逻辑。

In my firebase dashboard I have set multiple accounts for one email option.

I have simple email, Facebook and Google Plus authentication in my application.

I handle each of them like this in my LoginActivity:

Google Plus:

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    dialog.dismiss();
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        FirebaseUser user = mAuth.getCurrentUser();
                        proceed();
                    } else {
                        // If sign in fails, display a message to the user.
                        Toast.makeText(LoginActivity.this, task.getException().getMessage(),
                                Toast.LENGTH_SHORT).show();

                    }

                    // ...
                }
            });
}

Facebook:

private void handleFacebookAccessToken(AccessToken token) {
    AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
    dialog.show();
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    dialog.dismiss();
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        FirebaseUser user = mAuth.getCurrentUser();
                        proceed();
                    } else {
                        Toast.makeText(LoginActivity.this, task.getException().getMessage(),
                                Toast.LENGTH_SHORT).show();
                    }

                }
            });

}

Simple Email:

mAuth.signInWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        dialog.dismiss();
                        proceed();
                    } else {
                        Toast.makeText(LoginActivity.this, "Wrong email or password", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                }
            });

Now I want to make users that has same emails for Facebook and Google Plus able to authorize either with Facebook and Google Plus.

This this documentation documentation says that I should skip FirebaseAuth.signInWith methods and call these functions:

AuthCredential credential = GoogleAuthProvider.getCredential(googleIdToken, null);
mAuth.getCurrentUser().linkWithCredential(credential)

Now there is confusing. How I can call getCurrentUser when it is still null because I have skipped signInWith methods?

Documentation also says that I handle merging whic I dont understand.

currentUser = auth.signInWithCredential(credential).await().getUser();

Also, signInWithCredenial does not have an await method.

That means that I should be linking multiple accounts with same email after Logining?

解决方案

To link accounts, there should be an existing session. For example lets say that a new user creates an account using Google as the auth provider.

In summary, to do this you will need to:

  • Use GIDSignIn to authenticate the user with Google.
  • Then Google returns an Id Token (if everything goes well).
  • You will use the token to create a GoogleAuthProvider credential object.
  • And then you use this credential to authenticate in Firebase calling signInWithCredential.

The process is similar with other auth providers like Facebook. In order to link the account with Facebook, you will need to do the first three steps mentioned above (related to Facebook auth) but instead of "signInWithCredential" you will need to call "linkWithCredential". If everything goes well, now the user will be able to authenticate to the same account with Google or Facebook.

If you call "signInWithCredential", you will create a new account that uses Facebook as auth provider. So a user instead of being able to access to a single account with two(or more) auth providers, that user will have two separate accounts for each auth provider. This is why the documentation says that you should skip calling FirebaseAuth.signInWith methods.

Regarding your question about merging, the documentation mentions: "The call to linkWithCredential will fail if the credentials are already linked to another user account". This means that a user has already an account with the auth provider. If you want the user to access the information from both accounts, you will need to create the logic to merge the information from one account to the other.

这篇关于Android Firebase将多个帐户提供商与相匹配的电子邮件相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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