在我的应用程序用户可以创建一个帐户,然后登录。如何正确使用Firebase登录用户?
一切正常,因为它应该,但我很好奇,如果我已经登录用户“正常”。
我的意思是什么“正确”是,我之前登录的用户我来检查该用户是否存在使用dataSnapshot获得用户的唯一ID,然后我带他们到首页之后他们已被认证。
但在火力地堡我已经看到了这一点:
“如果用户成功登录后,您可以在 与getCurrentUsermethod任何一点得到他们的帐户数据。”
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
// Name, email address, and profile photo Url
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();
}
正如你可以在上面的代码块看到在评论中说,使用FirebaseUser.Token()
我有点糊涂了,如果我做的方式是罚款,或者如果我必须做的它使用Firebase上提到的令牌的另一种方式。
我的逻辑:
之前,我在用户登录我检查,看看用户是否已经对火力地堡一笔账:
// Check if the user has already signed up and exists in the database
private void checkIfUserSignedUp() {
// Get the currently authenticated user
mUser = mAuth.getCurrentUser();
// If user exists
if (mUser != null) {
// Retrieve current user's unique id
final String user_id = mUser.getUid();
// Listen for changes in database
mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Data snapshot checks contents of the database at the reference. In this case
// Checking for registered user by their unique id
if (dataSnapshot.hasChild(user_id)) {
// Take user to the home/main page after successfully signing in
Intent mainIntent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
} else {
// User does not exist in the database. Has not signed up.
// Take user to register profile
Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(registerIntent);
finish();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// User cancelled task...
}
});
}// End if
// Hide progress dialog
mLoginProgress.dismiss();
}
正如你我所使用的图片中看到以上dataSnapshot.hasChild(user_id)
检查用户的存在。
这样好吗?或者,我应该做一个替代方式,我检查用户使用Firebase.getToken()
我只是不希望任何问题与应用程序后,如果我的方式是不正确的。
编辑:
所以我改变了我的方法在下列情况,用户能够登录就好了:
private void checkIfUserSignedUp() {
// Get the currently authenticated user
mUser = mAuth.getCurrentUser();
if (mUser != null) {
// Take user to the home/main page after successfully signing in
Intent mainIntent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
} else {
// User does not exist in the database. Has not signed up.
// Take user to register profile
Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(registerIntent);
finish();
}
mLoginProgress.dismiss();
}
所以基本上在最高层的代码块是即使它工作,也不正确地完成。
因此,由于有人评论,一个问题得到解决。接下来的事情是我之前问过的,Firebase.getToken()
什么时候使用?
请将代码作为文本分享。像这张照片一样娱乐,它不是可重复使用的。 –
我不明白你为什么试图通过在数据库中查找他/她的'uid'来检查用户是否已经注册了,如果'mAuth.getCurrentUser()'已经告诉你他/她是否已经登录因此已经签署)或不。 – hotrod
@FrankvanPuffelen我发布了实际的代码。 – Equivocal