简体   繁体   中英

Firebase User Verification Email

I am using Firebase as the Backend for an Mobile Android Application. When a User registers, I would like to have a verification email sent to that user.

When the User Clicks the "SignUp" button, the following logic is run through: First of all a number of variables are set from the filled in boxes. Then a few checks are performed on validity of the filled in parameters. If the checks are passed, a user is created in the Database. This is all functioning..

Then I would like to send a verification email to the user to check if the email is valid. I have also put this in the onClick method of the Button but this is not functioning yet.

I do not receive the verification email. What is the reason behind this and the fix?

My onCreate Method

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_sign_up_page);
        setFontType();

        screen = (EditText)findViewById(R.id.SchermNaam);
        mail = (EditText)findViewById(R.id.EmailAdres);
        knop = (Button)findViewById(R.id.SignUp_Button_SignUp);

        firebaseAuth = FirebaseAuth.getInstance();
    }

The onClick method for the "SignUp" Button:

public void onClickSignUpPage(View view){

    String schermnaam = screen.getText().toString().trim();
    String emailadres = mail.getText().toString().trim();
    String paswoord = pass.getText().toString().trim();

    if(TextUtils.isEmpty(schermnaam)){
        Toast.makeText(this,"Schermnaam invullen", Toast.LENGTH_SHORT).show();
        return;
    }

    if(TextUtils.isEmpty(emailadres)){
        Toast.makeText(this,"Email invullen",Toast.LENGTH_SHORT).show();
        return;
    }

    if(!schermnaam_ok(schermnaam)){
        Toast.makeText(this,"schermnaam minstens 5 en maximum 15 tekens", Toast.LENGTH_SHORT).show();
        return;
    }

    if(!paswoord_ok(paswoord)){
        Toast.makeText(this,"paswoord tussen 6-12 karakters en minstens 1 cijfer", Toast.LENGTH_SHORT).show();
        return;
    }

    firebaseAuth.createUserWithEmailAndPassword(emailadres.trim(),paswoord)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if(task.isSuccessful()){
                        Toast.makeText(SignUpPage.this, "Nieuwe Speler Geregistreerd", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(SignUpPage.this, SignInPage.class);
                        startActivity(intent);
                    }
                    else {
                        FirebaseAuthException e = (FirebaseAuthException) task.getException();
                        Toast.makeText(SignUpPage.this,"Fout in de SignUp"+e.getMessage(), Toast.LENGTH_SHORT).show();
                        Log.d("LoginActivity", "Failed Registration", e);
                        return;
                    }
                }
            });

    FirebaseUser user = firebaseAuth.getCurrentUser();

    user.sendEmailVerification()
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "Email sent.");
                    }
                }
            });
}

Changed it up to a sequential procedure, as suggested by the post quoted by @cramopy. Thank you for this!

Now you first need to "Sign Up" as a user, and then verify in another step, through another button. Then I receive the confirmation email.

Here my code for the onClick method for the 2 Buttons.. From a UX point of view, need to look at how to position the buttons. This is a functional viewpoint.

public void onClickSignUpPage(View view){

    String schermnaam = screen.getText().toString().trim();
    String emailadres = mail.getText().toString().trim();
    String paswoord = pass.getText().toString().trim();

    if(TextUtils.isEmpty(schermnaam)){
        Toast.makeText(this,"Schermnaam invullen", Toast.LENGTH_SHORT).show();
        return;
    }

    if(TextUtils.isEmpty(emailadres)){
        Toast.makeText(this,"Email invullen",Toast.LENGTH_SHORT).show();
        return;
    }

    if(!schermnaam_ok(schermnaam)){
        Toast.makeText(this,"schermnaam minstens 5 en maximum 15 tekens", Toast.LENGTH_SHORT).show();
        return;
    }

    if(!paswoord_ok(paswoord)){
        Toast.makeText(this,"paswoord tussen 6-12 karakters en minstens 1 cijfer", Toast.LENGTH_SHORT).show();
        return;
    }

    firebaseAuth.createUserWithEmailAndPassword(emailadres.trim(),paswoord)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if(task.isSuccessful()){
                        Toast.makeText(SignUpPage.this, "Nieuwe Speler Geregistreerd", Toast.LENGTH_SHORT).show();

                    }
                    else {
                        FirebaseAuthException e = (FirebaseAuthException) task.getException();
                        Toast.makeText(SignUpPage.this,"Fout in de SignUp"+e.getMessage(), Toast.LENGTH_SHORT).show();
                        Log.d("LoginActivity", "Failed Registration", e);
                        return;
                    }
                }
            });

    AddDataFireBase();


}

public void onClickVerify(View view){

    FirebaseUser user = firebaseAuth.getCurrentUser();
    assert user != null;
    user.sendEmailVerification()
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "Email sent.");

                    }
                    else {
                        Log.e(TAG, "sendEmailVerification", task.getException());
                        Toast.makeText(SignUpPage.this,
                            "Failed to send verification email.",
                            Toast.LENGTH_SHORT).show();
                }}
            });
    Intent intent = new Intent(SignUpPage.this, SignInPage.class);
    startActivity(intent);
}

Your code should work. But I suggest you to follow the next steps to verify the email:

  • Create account
  • Check if creation was successful
  • Check if the email is verified with user.isEmailVerified()
  • Reload your user to update the instance with user.reload() . You should do this because some methods on Firebase ask you to reload or reauthenticate the user before realize some operations. Reload in this case will update the cache and data of your user.
  • Send the email using sendEmailVerification()

Something like:

  authInstance.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener {
                if (it.isSuccessful) {
                    //Create accountData in the database
                    if (it.result!!.user.isEmailVerified) {
                        it.result!!.user.reload()
                                .addOnCompleteListener {
                                    if (it.isSuccessful) {
                                        authInstance.currentUser!!.sendEmailVerification()
                                                .addOnCompleteListener {
                                                    Log.i("TAG", "Yay! verificationSent")
                                                }
                                    } else {
                                        //Manage error
                                    }
                                }
                    }
                } else {
                    //Manage error
                }
            }

Also, you should know that sometimes the email verification takes some time in Debug environments(at least from my experience I have saw delays of 1-3 minutes, but never on release versions).

Finally, before call user.verify() you should need to call again user.reload() to update the data of your user in the Firebase Cache, if not, even if you have click on Verify my email on the email sent to your account, the FirebaseAuthor.currentUser().isEmailVerified() will continue returning you false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM