[英]Understanding createUser function of firebase(specifically android library)
所以我從firebase文檔中獲得了以下代碼(我已經在我的應用程序中實現了它並且工作正常):
Firebase ref = new Firebase("https://myapp.firebaseio.com");
ref.createUser("bobtony@firebase.com", "correcthorsebatterystaple", new Firebase.ValueResultHandler<Map<String, Object>>() {
@Override
public void onSuccess(Map<String, Object> result) {
System.out.println("Successfully created user account with uid: " + result.get("uid"));
}
@Override
public void onError(FirebaseError firebaseError) {
// there was an error
}
});
在我創建一個用戶之后,它會在控制台上打印它的uid。 但是,當我進入我的myapp.firebaseio.com時,那里什么都沒有..所以我有一些問題:
所以,我試圖做的是在onSuccess()中我使用ref.push()一些值到myapp.firebaseio.com然后..我怎么能檢查createUser()創建的用戶uid是否相同作為我推的人? (id是不同的!)
我希望我的文字清楚,如果沒有被問及我可以嘗試再解釋一下!
謝謝你!
用戶信息不會存儲在Firebase數據庫中。 對於匿名用戶和OAuth用戶,任何地方都不會存儲任何信息。 電子郵件+密碼用戶的信息保存在您無權訪問的單獨數據庫中。 電子郵件+密碼用戶當然可以在儀表板的“登錄和驗證”選項卡中看到,而不是在數據庫中。
如果要將用戶信息存儲在自己的Firebase數據庫中,則必須在創建或驗證用戶時自行存儲用戶信息。 Firebase文檔中有一個關於存儲用戶數據的部分,其中顯示了如何執行此操作。
必須自己存儲信息的一個好處是,您可以確切地確定什么是未存儲的內容。
正如弗蘭克所說; 在創建用戶時,沒有用戶信息自動放入firebase本身(請在儀表板側欄中查看Login&Auth)。 創建后,新用戶甚至都沒有登錄。 這是我用來登錄時使用的代碼,並在注冊時將新用戶放入firebase:
static void createUser(final String username, final String password) {
final Firebase rootRef = new Firebase("YOUR_FIREBASE_URL");
rootRef.createUser(
username,
password,
new Firebase.ResultHandler() {
@Override
public void onSuccess() {
// Great, we have a new user. Now log them in:
rootRef.authWithPassword(
username,
password,
new Firebase.AuthResultHandler() {
@Override
public void onAuthenticated(AuthData authData) {
// Great, the new user is logged in.
// Create a node under "/users/uid/" and store some initial information,
// where "uid" is the newly generated unique id for the user:
rootRef.child("users").child(authData.getUid()).child("status").setValue("New User");
}
@Override
public void onAuthenticationError(FirebaseError error) {
// Should hopefully not happen as we just created the user.
}
}
);
}
@Override
public void onError(FirebaseError firebaseError) {
// Couldn't create the user, probably invalid email.
// Show the error message and give them another chance.
}
}
);
}
到目前為止,這對我來說效果很好。 我想如果連接在所有內容中間中斷(可能最終沒有用戶的初始信息),可能會出現問題。 不要過分依賴它設置......
//create user
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(SignupActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(SignupActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(SignupActivity.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
Log.e("task",String.valueOf(task));
getUserDetailse(auth);
}
}
});
/ 獲取用戶詳細信息對FirebaseAuth身份驗證 /
public static void getUserDetailse(FirebaseAuth auth)
{
//
auth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull final FirebaseAuth firebaseAuth) {
final FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
Log.i("AuthStateChanged", "User is signed in with uid: " + user.getUid());
String name = user.getDisplayName();
String email = user.getEmail();
Uri photoUrl = user.getPhotoUrl();
// The user's ID, unique to the Firebase project. Do NOT use this value to
// authenticate with your backend server, if you have one. Use
// FirebaseUser.getToken() instead.
String uid = user.getUid();
Log.e("user",name+email+photoUrl);
} else {
Log.i("AuthStateChanged", "No user is signed in.");
}
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.