gpt4 book ai didi

android - 如何使用 Firebase 发送验证邮件?

转载 作者:可可西里 更新时间:2023-11-01 18:49:48 26 4
gpt4 key购买 nike

我正在使用 Firebase 的电子邮件和密码方法注册我的用户。像这样:

mAuth.createUserWithEmailAndPassword(email, password)

.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {

if (task.isSuccessful()) {

FirebaseUser signed = task.getResult().getUser();

writeNewUser(signed.getUid());

new android.os.Handler().postDelayed(

new Runnable() {
public void run() {

updateUser(b);

}
}, 3000);

} else {

new android.os.Handler().postDelayed(

new Runnable() {
public void run() {

onSignupFailed();

}
}, 3000);

}

}
});

用户邮箱注册成功后,我希望Firebase发送一封验证邮件。我知道这可以使用 Firebase 的 sendEmailVerification。除了发送这封电子邮件之外,我还希望在用户验证电子邮件之前禁用用户的帐户。这还需要使用 Firebase 的 isEmailVerified 功能。但是,我未能成功让 Firebase 发送验证电子邮件,我无法弄清楚如何禁用和启用发送验证电子邮件的帐户以及在验证后。

最佳答案

这个问题是关于如何使用Firebase发送验证邮件。 OP 无法弄清楚如何禁用和启用发送验证电子邮件的帐户以及验证后的帐户。

此外,这在 firebase 文档中没有正确记录。所以我正在编写一个逐步的程序,如果有人遇到问题,他/她可能会遵循该程序。

1) 用户可以使用 createUserWithEmailAndPassword 方法。

例子:

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
}

// ...
}
});

如果创建了新帐户,则用户也已登录,并且 AuthStateListener 运行 onAuthStateChanged 回调。在回调中,您可以管理向用户发送验证邮件的工作。

示例:

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

}
// ...
}
};

现在发送验证邮件可以这样写:

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());

}
}
});
}

现在来到 LoginActivity:

在这里,如果用户成功登录,那么我们可以简单地调用一个方法,您在其中编写用于检查电子邮件是否已验证的逻辑。

示例:

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();
}
// ...
}
});

现在考虑 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

}
}

所以我在这里检查电子邮件是否经过验证。如果不是,则注销用户。

所以这是我正确跟踪事物的方法。

关于android - 如何使用 Firebase 发送验证邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40404567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com