上QQ阅读APP看书,第一时间看更新
Sign in existing users
Sign in also looks for two string parameters, which are valid email address and password. However, here, we will make use of another method called signInWithEmailAndPassword. We need to attach addOnCompleteListener and with its callbacks, as shown:
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Successful signin
Log.d(TAG, "signInWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// failed for some reason
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}
// ...
}
});
This code explains that when the task object is successful, we can retrieve the user information through the FirebaseUser object; for instance, consider the following code, which retrieves the information from the authorized user:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
// These are the information we will be able to retrieve
// Name, email address, and profile photo Url
String name = user.getDisplayName();
String email = user.getEmail();
Uri photoUrl = user.getPhotoUrl();
// Check if user's email is verified
boolean emailVerified = user.isEmailVerified();
// The user's ID, unique to the Firebase project.
// UID shall not be used to server purpose
// FirebaseUser.getToken() instead.
String uid = user.getUid();
}