检查Cloud Firestore中是否已经存在用户Google登录凭据

拉明:

我在Google登录时遇到问题,每当我使用Google登录时,我都将凭据设置为也要添加到数据库中(例如:name和email),但是除了我每次登录外,所有问题都在起作用在使用相同的Google帐户的情况下,它将再次添加到数据库中(一遍又一遍地使用相同的凭据),因此我想检查数据库中是否已存在Google登录凭据,而无需全部添加凭据。

这是onCreate中的Google登录代码:

private FirebaseAuth firebaseAuth;
private FirebaseFirestore firebaseFirestore;
private GoogleSignInClient mGoogleSignInClient;
private static final int RC_SIGN_IN = 100;


googleImageBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            progresBarDim.setVisibility(View.VISIBLE);
            signIn();
        }
    });

// TODO : Firebase Authentication
    firebaseAuth = FirebaseAuth.getInstance();
    firebaseFirestore = FirebaseFirestore.getInstance();
    googleRequest();

这是google signIn的方法:

private void googleRequest(){
    // Configure Google Sign In
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();

    // Build a GoogleSignInClient with the options specified by gso.
    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
}

private void signIn() {
    Intent signInIntent = mGoogleSignInClient.getSignInIntent();
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);


    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            // Google Sign In was successful, authenticate with Firebase
            GoogleSignInAccount account = task.getResult(ApiException.class);
            firebaseAuthWithGoogle(account);
        } catch (ApiException e) {
            progresBarDim.setVisibility(View.GONE);
            // Google Sign In failed, update UI appropriately
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
            // ...
        }
    }
}

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {


    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    firebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        GoogleSignInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(LoginActivity.this);
                        final String name = signInAccount.getDisplayName().trim();
                        final String email =signInAccount.getEmail().trim();

                        Map<Object, String> userdata = new HashMap<>();
                        userdata.put("Nom et prénom", name);
                        userdata.put("Address émail", email);

                        firebaseFirestore.collection("USERS")
                                .add(userdata);

                        // Sign in success, update UI with the signed-in user's information
                        FirebaseUser user = firebaseAuth.getCurrentUser();
                        Intent intent = new Intent(LoginActivity.this, ProfileActivity.class);
                        startActivity(intent);

                    } else {
                        progresBarDim.setVisibility(View.GONE);
                        // If sign in fails, display a message to the user.
                        Toast.makeText(LoginActivity.this, "sorry auth failed!", Toast.LENGTH_SHORT).show();
                    }
                    progresBarDim.setVisibility(View.GONE);

                    // ...
                }
            });
}
亚历克斯·莫莫:

它将再次添加到实时数据库中(一遍又一遍相同的凭据)

发生这种情况是因为您要一遍又一遍地添加数据。下面的代码行负责此行为:

firebaseFirestore.collection("USERS")
                            .add(userdata);

当您使用add(对象数据)时

将具有指定数据的新文档添加到此集合中,并自动为其分配一个文档ID。

因此,每次您调用上面的代码行时,都会创建一个新文档。要解决此问题,您应该将数据添加到特定位置,例如,在以下参考资料中:

Firestore-root
  |
  --- users (collection)
       |
       --- uid  (document)
            |
            --- Nom et prénom: "User Name"
            |
            --- Address émail: "User Email"

您可以使用以下代码行uidFirebaseUser对象获取

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

现在,要写入数据,请使用以下代码:

firebaseFirestore.collection("USERS").document(uid)
                            .set(userdata);

但是add()我没有使用而是使用set()了DocumentReference对象。为了避免每次用户使用Google登录时都写入数据,您需要检查用户是否已经存在,可以使用以下帖子中的答案进行操作:

但是,您应该检查而不是检查用户名uid

PS您的代码说,设置标签时,您正在使用Cloud Firestore,而不是 Firebase Realtime Database。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何检查Cloud Firestore中是否存在用户?

检查数据库中是否已经存在用户名

检查数据库中是否已经存在用户名

** WebSecurity.CreateUserAndAccount()**如何检查数据库中是否已经存在用户名?

Cloud Firestore:查询以检查用户名是否存在

检查用户是否在Firestore Cloud Function中为匿名用户

Firestore-检查用户名是否已经存在

如何检查文档在Cloud Firestore中是否存在抖动?

如何检查Firebase中是否存在用户?

如何检查Linux中是否存在用户(JSch)

Symfony检查数据库中是否存在用户

检查 ArrayList 中是否存在用户 ID?

检查MySQL Java中是否存在用户名

如何检查用户是否已经登录?

Cloud Firestore安全规则:如何检查属性是否存在?

检查列表中是否存在用户名,如果不存在则仅创建用户

如何检查用户是否已经使用Google登录和Firebase Auth登录以检索其数据?

要检查用户是否已经存在MongoDB

解析检查用户是否已经存在

Flask 用户登录 - 检查用户是否存在

在Cloud Firestore规则中-如何检查键是否为空

如何检查文档是否在Cloud Firestore中包含属性?

流星检查用户集中是否存在用户名

如何检查Cloud Firestore中集合中所有文档中是否都存在字段值?

Firebase Facebook登录检查用户是否存在

在JavaScript中检查Firebase数据库中是否存在用户名

检查用户是否在Firestore中

如何检查用户是否已登录Swift中的Google帐户?

使用WCF RESTful服务检查服务器上是否存在用户登录名和密码