[英]How to save a signed in FirebaseUser before creating a new one
我正在嘗試使用Firebase(身份驗證)制作一個應用程序,該應用程序允許教師創建學生的用戶,然后向該學生發送用戶名和密碼,然后讓他登錄。我的問題是,在老師之后,這樣做正在創建用戶,當前用戶是學生的用戶,當我嘗試執行以下代碼時:
private void signStu(String email,String password)
{
final FirebaseUser teacher=mAuth.getCurrentUser();
mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
FirebaseUser student=mAuth.getCurrentUser();
User newUser=new User(/*data i need to pass to the constructor, doesn't affect the result*/);
mAuth.updateCurrentUser(teacher); //Here it passes on the student user and not the teacher
}
});
}
因此,似乎mAuth.getCurrentUser()之前已更新為所有用法,因此教師變量在創建后變成了學生變量。 任何人都知道如何解決該問題或解決該問題的方法嗎? 謝謝 !
FirebaseAuth.getInstance().signOut();
注銷學生。 有關此后該怎么做的更多信息: 如何使用戶退出Firebase? 。 您可以嘗試注銷用戶並重新登錄。也許正式的Firebase文檔中的某些內容(如重新認證用戶)可能會有所幫助。 在此鏈接中,給出了以下示例代碼:
AuthCredential credential = EmailAuthProvider
.getCredential("user@example.com", "password1234"); //teacher info
// Prompt the user to re-provide their sign-in credentials
user.reauthenticate(credential)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Log.d(TAG, "User re-authenticated.");
}
});
顯然,如果您想采用這種方法,您也可以處理此服務器端。 Firebase踢出當前用戶
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.