acct.getId()与mFirebaseUser.getUid()有什么区别?

问题描述 投票:0回答:1

我正在尝试将用户数据保存到firebase实时数据库中,我使用谷歌登录身份验证。因此,在创建用户节点树时,我想存储“Users”>“uid”> userdata等数据。如何获得经过身份验证的用户的Uid? by acct.getId()[acct is GoogleSignInAccount object] OR user.getUid()[user is FirebaseUser object]?代码从这里开始:

 private void firebaseAuthWithGoogle (final GoogleSignInAccount acct) {
    Log.d("TAG", "firebaseAuthWithGoogle:" + acct.getId());


    final DatabaseReference rootRef;
    rootRef = FirebaseDatabase.getInstance().getReference();

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, 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");
                        FirebaseUser user = mAuth.getCurrentUser();

                        if (acct != null) {
                            personName = acct.getDisplayName();
                            uid=user.getUid();
                            uid1=acct.getId();
                            HashMap<String,Object> userdatamap = new HashMap<>();
                            userdatamap.put("firstName",personName);
                            userdatamap.put("email",acct.getEmail());
                            rootRef.child("Users").child(/* uid */).updateChildren(userdatamap)
                                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {
                                            if(task.isSuccessful()){
                                                Toast.makeText(login.this,"DONE",Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    });

                            pref = getApplicationContext().getSharedPreferences("userinfo",0);
                            editor = pref.edit();
                            editor.putString("namekey",personName);
                            editor.commit();
                            Toast.makeText(login.this,"Welcome "+personName,Toast.LENGTH_SHORT).show();

                        }
                        else {
                            Toast.makeText(login.this,"Profile Name Not Found",Toast.LENGTH_SHORT).show();
                        }
                        //updateUI(user);

                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w("TAG", "signInWithCredential:failure", task.getException());
                        Toast.makeText(login.this,"Auth Failed",Toast.LENGTH_SHORT).show();
                        //updateUI(null);
                    }

                    // ...
                }
            });
}
android firebase firebase-realtime-database firebase-authentication google-signin
1个回答
0
投票

GoogleSignInAccount的getId()

如果您从“新版GoogleSignInOptions.Builder”(GoogleSignInOptions.DEFAULT_SIGN_IN)或配置了requestId()开始构建配置,则返回Google帐户的唯一ID;否则为null。

这是用于用户记录的首选唯一键。

而FirebaseUser的getUid()

返回用于在Firebase项目的用户数据库中唯一标识用户的字符串。在Firebase数据库或存储中存储信息时,甚至在您自己的后端中使用它。

此标识符不透明,不一定与用户的电子邮件地址或任何其他字段相对应。

使用Firebase服务时,最好使用来自Firebase身份验证的uid。从官方文档:

用户首次登录后,将创建一个新用户帐户并将其链接到用户登录的凭据(即用户名和密码,电话号码或身份验证提供商信息)。此新帐户存储为Firebase项目的一部分,可用于识别项目中每个应用程序的用户,无论用户如何登录。

© www.soinside.com 2019 - 2024. All rights reserved.