我有点迷茫,我正在编写一个 android 应用程序,它可以让尚未登录的新用户从 firestore
读取一些数据,所以我设置了匿名登录,我想使用 firebase-auth
对匿名用户进行身份验证,一旦用户通过身份验证,我想使用他的 uid/cred
来访问 firestore database
。
Firebase 身份验证:
mAuth = FirebaseAuth.getInstance();
mAuth.signInAnonymously()
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
printWithDash("INSIDE onComplete");
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
printWithDash("SIGN IN SUCCESSFULL");
FirebaseUser user = mAuth.getCurrentUser();
printWithDash(user.toString()); // address
} else {
// If sign in fails, display a message to the user.
printWithDash("SIGN IN FAIL!!!");
}
}
});
这是我写信给 firestore 的方式:
db.collection("answers")
.add(jsonAnswers)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
System.out.println("-----------------------------------------------------------------------------------------");
System.out.println("DocumentSnapshot added with ID: " + documentReference.getId());
System.out.println("-----------------------------------------------------------------------------------------");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
System.out.println("Error adding document " + e);
}
});
这是我的firetore规则
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read: if request.auth.uid != null && request.auth.token.firebase.sign_in_provider == 'anonymous';
allow write: if request.auth.uid != null && request.auth.token.firebase.sign_in_provider == 'anonymous';
}
}
}
我是 Android 开发的新手,身份验证如何在 android 应用程序上工作?。我如何使用我的匿名用户写信给 firestore db
?
答案 0 :(得分:0)
仅将 request.auth.uid != null;
设置为能够读写已登录匿名用户:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.uid != null;
}
}
}
匿名登录的用户也经过身份验证。
答案 1 :(得分:-1)
我了解到匿名签名会将您的用户信用保存在手机上,请查看应用程序/您的应用程序下的设备文件浏览器。因此,一旦我们进行了匿名登录,用户就会被视为已通过身份验证,直到您执行 mAuth.signOut();