繁体   English   中英

在 Firebase 中创建用户之前发送验证电子邮件需要进行哪些修改

[英]what modifications needed to send verification email before creating user in firebase

我是 android 应用程序开发的新手。 我想在将用户的电子邮件添加到 firebase 之前发送验证电子邮件。 需要在下面的代码中进行哪些修改,以将验证电子邮件发送到用户的电子邮件。

package com.threemusketeers.healthmaster;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

//defining view objects
private EditText editTextEmail;
private EditText editTextPassword;
private Button buttonSignup;
private ProgressDialog progressDialog;

private TextView textViewSignin;
;

//defining firebaseauth object
private FirebaseAuth firebaseAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //initializing firebase auth object
    firebaseAuth = FirebaseAuth.getInstance();

    //if getCurrentUser does not returns null
    if (firebaseAuth.getCurrentUser() != null) {
        //that means user is already logged in
        //so close this activity
        finish();

        //and open profile activity
        startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
    }

        //initializing views
        editTextEmail = (EditText) findViewById(R.id.editTextEmail);
        editTextPassword = (EditText) findViewById(R.id.editTextPassword);
        textViewSignin = (TextView) findViewById(R.id.textViewSignin);

        buttonSignup = (Button) findViewById(R.id.buttonSignup);

        progressDialog = new ProgressDialog(this);

        //attaching listener to button
        buttonSignup.setOnClickListener(this);
        textViewSignin.setOnClickListener(this);
    }

private void registerUser() {

    //getting email and password from edit texts
    String email = editTextEmail.getText().toString().trim();
    String password = editTextPassword.getText().toString().trim();

    //checking if email and passwords are empty
    if (TextUtils.isEmpty(email)) {
        Toast.makeText(this, "Please enter email", Toast.LENGTH_LONG).show();
        return;
    }

    if (TextUtils.isEmpty(password)) {
        Toast.makeText(this, "Please enter password", Toast.LENGTH_LONG).show();
        return;
    }

    //if the email and password are not empty
    //displaying a progress dialog

    progressDialog.setMessage("Registering Please Wait...");
    progressDialog.show();

    //creating a new user
    firebaseAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {

                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    //checking if success
                    if (task.isSuccessful()) {
                        finish();
                        startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
                        //Toast.makeText(MainActivity.this, "Successfully registered", Toast.LENGTH_LONG).show();
                    } else {
                        //display some message here
                        Toast.makeText(MainActivity.this, "Registration Error", Toast.LENGTH_LONG).show();
                    }
                    progressDialog.dismiss();
                }
            });

}

@Override
public void onClick(View view) {
    if(view == buttonSignup){
        registerUser();
    }

    if(view == textViewSignin){
        //open login activity when user taps on the already registered textview
        startActivity(new Intent(this, LoginActivity.class));
    }
}
}

使用新 API ( 'com.google.firebase:firebase-auth:9.6.1' ),您可以使用:

私人无效 verifyEmail() {

firebaseAuth.getCurrentUser().sendEmailVerification()
        .addOnCompleteListener(getActivity(), new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Toast.makeText(getActivity(), "Email sent", Toast.LENGTH_LONG).show();
                }
            }
        });

}

                firebaseAuth.createUserWithEmailAndPassword(email, password)
                        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (task.isSuccessful()) {
                                    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                                    user.sendEmailVerification()
                                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                                @Override
                                                public void onComplete(@NonNull Task<Void> task) {
                                                    user.sendEmailVerification();
                                                    if (task.isSuccessful()) {
                                                        finish();
                                                        Toast.makeText(Register.this, "Registered Successfully. Check your email", Toast.LENGTH_SHORT).show();
                                                        startActivity(new Intent(getApplicationContext(), yourActivity.class));
                                                    }
                                                }
                                            });

                                    if (firebaseAuth.getCurrentUser() != null) {
                                        finish();
                                        startActivity(new Intent(getApplicationContext(), yourActivity.class)); 
                                    }
                                } else {
                                    Toast.makeText(Register.this, "Could not Register User. Try Again", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });

https://firebase.google.com/docs/reference/android/com/google/firebase/auth/UserInfo

在登录活动...

if(user.isEmailVerified){
//allow to login app
} else{
//show toast, help, inform that account is not registered or no  stable internet connection
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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