Câu hỏi này là về cách sử dụng Firebase để gửi email xác minh. OP không thể tìm ra cách vô hiệu hóa và kích hoạt tài khoản gửi email xác minh và sau khi nó đã được xác minh.
Ngoài ra, tài liệu này không được ghi lại chính xác trong tài liệu firebase. Vì vậy, tôi đang viết một quy trình từng bước mà một người nào đó có thể làm theo nếu anh ta/cô ấy đang đối mặt với vấn đề.
1) Người dùng có thể sử dụng phương thức createUserWithEmailAndPassword.
Ví dụ:
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("TAG", "createUserWithEmail:onComplete:" + task.isSuccessful());
// 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()) {
// Show the message task.getException()
}
else
{
// successfully account created
// now the AuthStateListener runs the onAuthStateChanged callback
}
// ...
}
});
Nếu tài khoản mới được tạo ra, người dùng cũng đã đăng nhập, và AuthStateListener chạy callback onAuthStateChanged. Trong cuộc gọi lại, bạn có thể quản lý công việc gửi email xác minh tới người dùng.
Ví dụ:
onCreate(...//
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
// NOTE: this Activity should get onpen only when the user is not signed in, otherwise
// the user will receive another verification email.
sendVerificationEmail();
} else {
// User is signed out
}
// ...
}
};
Bây giờ email gửi xác minh có thể được viết như sau:
private void sendVerificationEmail()
{
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
user.sendEmailVerification()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// email sent
// after email is sent just logout the user and finish this activity
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(SignupActivity.this, LoginActivity.class));
finish();
}
else
{
// email not sent, so display message and restart the activity or do whatever you wish to do
//restart this activity
overridePendingTransition(0, 0);
finish();
overridePendingTransition(0, 0);
startActivity(getIntent());
}
}
});
}
Bây giờ đến LoginActivity:
đây nếu người dùng là suc cessfully đăng nhập sau đó chúng tôi chỉ đơn giản là có thể gọi một phương pháp mà bạn đang viết logic để kiểm tra nếu email được xác minh hay không.
Ví dụ:
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
//Log.d("TAG", "signInWithEmail:onComplete:" + task.isSuccessful());
// 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()) {
//Log.w("TAG", "signInWithEmail:failed", task.getException());
} else {
checkIfEmailVerified();
}
// ...
}
});
Bây giờ xem xét các phương pháp checkIfEmailVerified:
private void checkIfEmailVerified()
{
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user.isEmailVerified())
{
// user is verified, so you can finish this activity or send user to activity which you want.
finish();
Toast.makeText(LoginActivity.this, "Successfully logged in", Toast.LENGTH_SHORT).show();
}
else
{
// email is not verified, so just prompt the message to the user and restart this activity.
// NOTE: don't forget to log out the user.
FirebaseAuth.getInstance().signOut();
//restart this activity
}
}
Vì vậy, ở đây tôi đang kiểm tra nếu email được xác minh hay không.Nếu không, sau đó đăng xuất người dùng.
Vì vậy, đây là cách tiếp cận của tôi để theo dõi mọi thứ đúng cách.
có thể trùng lặp của [Email xác nhận bằng căn cứ hỏa lực 3.0 trên Android] (http://stackoverflow.com/questions/37418518/email-verification-using-firebase-3-0-on-android) –