android - 如何确定用户之前是否使用 google firebase 登录
问题描述
我正在为我的android项目使用google登录,并在使用firebase auth成功进行身份验证后,我将带有其他默认字段的用户记录添加到firestore。
问题是当同一用户尝试使用同一电子邮件再次登录时,同一用户的新文档被添加到 firestore 数据库(两个具有相同数据的文档)
我需要一种方法来了解用户之前是否已登录,这样我就不会向 Firestore 添加新文档
我搜索了堆栈溢出,只找到了使用电子邮件和密码进行身份验证的解决方案。该解决方案使用:
authResultTask.getException() instanceof FirebaseAuthUserCollisionException
这不适用于谷歌登录
//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);
}
}
});
}
解决方案
如果您使用由 Firebase 身份验证分配的用户 UID 作为文档的 ID,而不是使用该方法创建的随机 ID,这对您来说会容易得多add()
。每次用户登录时,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);
您可能还想在尝试这样编写之前先检查文档是否已经存在,或者使用合并集覆盖其现有内容。
推荐阅读
- azure - 如何发送包含最近 Azure 基础架构部署详细信息的电子邮件
- c++ - MIP:在 CPLEX (C++) 中修复部分解向量
- angular - 单击浏览器上的返回 <-- 按钮从数组中删除项目
- javascript - Array.from 方法返回一个具有未定义值而不是实数的数组
- json - 来自嵌套数组(Apache NiFi)的 JOLT 连接值
- graphql - 苦艾酒联合类型解析器为对象字段解析器提供空映射
- react-native - 在组件外部使用 react-i18next 中的 t()
- java - LeetCode - Remove Duplicates from Sorted Array using Java
- angular - 角度错误 TS2341:属性“X”是私有的,只能在类内访问
- excel - 从 Excel 到 Sharepoint 的动态链接