如何确定用户以前是否使用过Google Firebase登录

时间:2019-04-26 05:38:26

标签: android firebase firebase-authentication google-cloud-firestore google-signin

我正在将google登录用于我的android项目,并在通过Firebase身份验证成功进行身份验证后,将带有其他默认字段的用户记录添加到Firestore。

问题是,当同一用户尝试使用同一封电子邮件再次登录时,同一用户的新文档被添加到Firestore数据库中(两个具有相同数据的文档)

我需要一种方法来知道用户以前是否登录过,这样我就不会在Firestore中添加新文档

我搜索了堆栈溢出,并且只找到了使用电子邮件和密码进行身份验证的解决方案。该解决方案使用:

  

authResultTask.getException()instanceof   FirebaseAuthUserCollisionException

不适用于Google登录

//google signin result intent
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RequestSignInCode){

            GoogleSignInResult googleSignInResult = Auth.GoogleSignInApi.getSignInResultFromIntent(data);

            if (googleSignInResult.isSuccess()){

                Toast.makeText(WelcomeActivity.this,"Google Sign In result available ..."/* authCredential.getProvider()*/,
                        Toast.LENGTH_LONG).show();


                GoogleSignInAccount googleSignInAccount = googleSignInResult.getSignInAccount();

                firebaseUserAuth(googleSignInAccount);
            }
            //else Toast.makeText(this, "falsa1", Toast.LENGTH_SHORT).show();

        }
        //else Toast.makeText(this, "falsa2", Toast.LENGTH_SHORT).show();
    }

    public void firebaseUserAuth(final GoogleSignInAccount googleSignInAccount) {

            AuthCredential authCredential = GoogleAuthProvider.getCredential(googleSignInAccount.getIdToken(), null);

            mAuth.signInWithCredential(authCredential)
                    .addOnCompleteListener(WelcomeActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task AuthResultTask) {
                            Log.d("auth", "AuthResultTask:Complete ");

                                if (AuthResultTask.isSuccessful()) {
                                    Log.d("auth", "AuthResultTask successful");

                                    String email = googleSignInAccount.getEmail();

                                    Map<String, Object> userProperties = new HashMap<>();
                                    userProperties.put("email", email);
                                    userProperties.put("fullname", "");
                                    userProperties.put("isActivated", false);
                                    userProperties.put("has_rated", false);
                                    userProperties.put("product_key", "");
                                    userProperties.put("time_registered", new Date());
                                    userProperties.put("total_referral_count", 0);



                                    db.collection("users")
                                            .add(userProperties)
                                            .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                                                @Override
                                                public void onSuccess(DocumentReference documentReference) {
                                                    progressBar.setVisibility(View.GONE);
                                                    Log.d("doc_id", documentReference.getId());
                                                    Toast.makeText(WelcomeActivity.this, "Google Sign In successful...",
                                                            Toast.LENGTH_LONG).show();

                                                    startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
                                                }
                                            })
                                            .addOnFailureListener(new OnFailureListener() {
                                                @Override
                                                public void onFailure(@NonNull Exception e) {
                                                    Log.w(DB_TAG, "Error adding document", e);
                                                    progressBar.setVisibility(View.GONE);
                                                }

                                            });


                                } else {
                                    Log.d("auth", "Exception: " + AuthResultTask.getException().toString());
                                    Toast.makeText(WelcomeActivity.this, "Something Went Wrong ...", Toast.LENGTH_LONG).show();
                                    progressBar.setVisibility(View.GONE);
                                }
                            }

                    });
        }

2 个答案:

答案 0 :(得分:2)

如果您使用由Firebase身份验证分配的用户的UID作为文档的ID,而不是使用add()方法创建的随机ID,则对您来说会容易得多。每次用户登录时,UID都将相同。

AuthResult authResult = ...
FirebaseUser user = authResult.getUser();
// this is unique for the user
String uid = user.getUid();

// Create a document for this user using their UID
db.collection("users").document(uid).set(userProperties);

您可能还想先检查文档是否已经存在,然后再尝试这样写,或者使用合并集覆盖其现有内容。

答案 1 :(得分:0)

据我了解,您正在尝试在用户首次注册时创建文档。因此,您必须在用户注册后提交文档。并非在每次登录后。

相关问题