我在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"
您可以使用以下代码行uid
从FirebaseUser
对象获取:
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] 删除。
我来说两句