谷歌Facebook和电子邮件Auth在Android应用程序与Firebase

时间:2018-03-18 16:24:48

标签: android firebase firebase-authentication

我正在使用Firebase开发一个Android应用程序,我想添加3个登录选项:Facebook,Google和电子邮件(和密码)。

我想在用户登录时为其提供3个选项:使用电子邮件和密码登录或使用Facebook或Google登录。 如果用户选择Facebook或Google,我想检查Facebook / Google的电子邮件是否属于另一个凭证,如果是,我想将用户与新凭证链接。否则,以正常方式登录。

例如,我们假设我有Facebook和Google帐户使用相同的电子邮件,我已经通过电子邮件和密码验证登录。当我点击Facebook登录按钮时,应用程序会注意到我的电子邮件属于现有用户,并将我的Facebook帐户连接到现有用户(可能是user.linkWithCredential())。如果我尝试通过Google登录,或者如果我已经使用电子邮件和密码创建新用户(如果他已经通过Google登录),也是如此...

我曾尝试做过类似的事情,但我仍然不确定如何获取其他凭据(不要求用户从其他提供商登录)或firebase现有用户,以便将其与新链接相关联凭据。

authResultTask = mAuth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        if (task.isSuccessful()) {
            // Sign in success, update UI with the signed-in user's information
            Log.d(TAG, "signInWithCredential:success");
            loggedIn(LoginType.FACEBOOK);
        } else {
            // If sign in fails, display a message to the user.
            Log.w(TAG, "signInWithCredential:failure", task.getException());
            if (task.getException() instanceof FirebaseAuthUserCollisionException) {
                mAuth.fetchProvidersForEmail(email /*not sure how to get the email also*/).addOnCompleteListener(new OnCompleteListener<ProviderQueryResult>() {
                    @Override
                    public void onComplete(@NonNull Task<ProviderQueryResult> task) {
                        if (task.isSuccessful()) {
                            if (task.getResult().getProviders().contains(
                                    GoogleAuthProvider.PROVIDER_ID)) {

                                mAuth.signInWithCredential(credential)
                                        .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                                            @Override
                                            public void onComplete(@NonNull Task<AuthResult> task) {
                                                if (task.isSuccessful()) {
                                                // Link initial credential to existing account.
                                                        getExistingUser()/*somehow get the existing user*/.linkWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                                                        @Override
                                                        public void onComplete(@NonNull Task<AuthResult> task) {

                                                        }
                                                    });
                                                } else {
                                                    makeToast(R.string.error_occurred);
                                                    disable.enableAndRemoveProgress();
                                                }
                                            }
                                        });
                            }
                        } else {
                            makeToast(R.string.error_occurred);
                        }
                    }
                });
            } else {
                makeToast(R.string.error_occurred);
            }
        }
    }
});

你知道怎么做吗?

0 个答案:

没有答案