Android 如何使用Firebase发送验证电子邮件?
我正在使用Firebase的电子邮件和密码方法注册我的用户。像这样:Android 如何使用Firebase发送验证电子邮件?,android,firebase,firebase-authentication,Android,Firebase,Firebase Authentication,我正在使用Firebase的电子邮件和密码方法注册我的用户。像这样: mAuth.createUserWithEmailAndPassword(email, password) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) {
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);
}
}
});
mAuth.createUserWithEmailAndPassword(电子邮件,密码)
.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
FirebaseUser signed=task.getResult().getUser();
writeNewUser(signed.getUid());
新的android.os.Handler().postDelayed(
新的Runnable(){
公开募捐{
更新者(b);
}
}, 3000);
}否则{
新的android.os.Handler().postDelayed(
新的Runnable(){
公开募捐{
onSignupFailed();
}
}, 3000);
}
}
});
用户的电子邮件成功注册后,我希望Firebase发送验证电子邮件。我知道使用Firebase的
sendEmailVerification
可以做到这一点。除了发送这封电子邮件,我希望用户的帐户被禁用,直到他们验证电子邮件。这还需要使用Firebase的isEmailVerified
功能。但是,我无法让Firebase发送验证电子邮件,我无法确定如何让Firebase禁用和启用发送验证电子邮件的帐户,以及在验证之后。使用FirebaseAuth.getInstance().getCurrentUser().sendEmailVerification()
和FirebaseAuth.getInstance().getCurrentUser().isEmailVerified()
无法通过Firebase SDK禁用该帐户。您可以使用包含Firebase Auth ID令牌的
GetTokenResult
,并根据您的自定义后端对其进行验证,或者为对应于该用户的Firebase数据库设置一个标志。就我个人而言,我会选择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
}
// ...
}
});
现在发送验证电子邮件的方式如下:
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());
}
}
});
}
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();
}
// ...
}
});
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
}
}
因此,我在这里检查电子邮件是否经过验证。如果没有,则注销该用户
所以这就是我正确跟踪事情的方法。将验证发送到用户的电子邮件
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
user.sendEmailVerification();
检查用户是否已验证
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
boolean emailVerified = user.isEmailVerified();
要发送与Firebase的电子邮件链接,首先需要抓取FirebaseAuth实例 使用我们通过以下方式在Firebase上创建用户的实例:
firebaseauth.createUserWithEmailAndPassword(email,pass);
当方法返回成功时,我们使用Firebase用户实例向用户发送验证链接,如下所示:
final FirebaseUser user = mAuth.getCurrentUser();
user.sendEmailVerification()
请参阅此链接:.mAuth.createUserWithEmailAndPassword(电子邮件,密码)。添加OnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
mAuth.getCurrentUser().sendEmailVerification().addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
Toast.makeText(这是“请检查电子邮件以进行验证”,Toast.LENGTH_SHORT).show();
loadingBar.disclose();
}否则{
Toast.makeText(this,task.getException().getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
这可能是解决方案的一部分,但没有回答我的问题。谢谢。我想我将使用的解决方法是创建一个默认密码,然后发送一个密码重置。通知用户在首次登录时首先设置一个新密码。根据您的代码,我如何检查whet她电子邮件是否已验证?。它将成功登录。但你应该先验证电子邮件,然后才能登录,这是正确的方式。你能帮我找到正确的方式吗?@Simon我回答的最后一段代码正在做你想做的事。让用户登录,然后检查他/她的电子邮件是否已验证。如果电子邮件未验证已验证,然后注销用户。我已完成所有操作,已发送验证电子邮件,我已单击链接,但user.isemailverefied()不起作用。我已找到答案。它起作用了,非常感谢您的帮助…对于Web JS,访问值:var isemailverefied=user.emailVerified;
。感谢
mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
mAuth.getCurrentUser().sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(this, "please check email for verification.", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}else{
Toast.makeText(this, task.getException().getMessage() , Toast.LENGTH_SHORT).show();
}
}
});