使用Firebase身份验证成功创建新用户帐户后,我尝试在Firebase数据库中创建第二个请求以创建其个人资料(姓名,电子邮件和电话),但如果第二个请求失败,我将有一个用户没有个人资料,这种情况不可能,因为他登录时无法编辑他的个人资料,所以目前我正在Firebase Auth中删除这个最近创建的用户,允许他重试创建,但是它不能解决问题,因为删除请求也可能失败。在Firebase中处理交易的最佳方法是什么,既成功创建了用户及其个人资料,又没有成功创建?
private void attemptToCreateAccount(final Credentials cred) {
getCompositeDisposable().add(
getAuthService().createAccount(cred)
.subscribeOn(getSchedulerProvider().io())
.observeOn(getSchedulerProvider().ui())
.subscribeWith(new DisposableSingleObserver<User>() {
@Override
public void onSuccess(User user) {
addUserProfileToDatabase(user.getEmail(), user.getUserUid(), cred.getUsername(), cred.getPassword());
}
@Override
public void onError(Throwable e) {
getView().showMessage(e.toString());
}
})
);
}
--------------------------
private void addUserProfileToDatabase(final String email, final String userUid, final String name, final String password) {
final Profile profile = new Profile("", "", "", email, "", name);
getCompositeDisposable().add(
getDatabaseSource().createNewProfileToUser(userUid, profile)
.subscribeOn(getSchedulerProvider().io())
.observeOn(getSchedulerProvider().ui())
.subscribeWith(new DisposableCompletableObserver(){
@Override
public void onComplete() {
getView().saveUserData(userUid, email, name);
getView().goToProfilePageActivity();
}
@Override
public void onError(Throwable e) {
// TODO RETRY OPERATION OR DELETE THE CURRENT CREATED USER
getView().showMessage(e.getLocalizedMessage());
deleteUserCreatedWithoutProfile(email, password);
}
})
);
}
--------------------------
private void deleteUserCreatedWithoutProfile(String email, String password){
getCompositeDisposable().add(
getDatabaseSource().deleteUser(email, password)
.observeOn(getSchedulerProvider().io())
.observeOn(getSchedulerProvider().ui())
.subscribeWith(new DisposableCompletableObserver(){
@Override
public void onComplete() {
Log.e("FIREBASE", "user deleted because profile could not be created successfully");
}
@Override
public void onError(Throwable e) {
Log.e("FIREBASE", "Houston We have a problem. User was created without a profile.");
}
})
);
}
答案 0 :(得分:0)
你不需要FirebaseDatabase就可以存储用户信息firebaseAuth了解你如何做到
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName("Jane Q. User")
.setPhotoUri(Uri.parse("https://example.com/jane-q-user/profile.jpg"))
.build();
user.updateProfile(profileUpdates)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "User profile updated.");
}
}
});
了解更多内容link