java - 我想将电子邮件验证发送到 firebase 中不用于注册帐户的另一个帐户
问题描述
我的注册活动中有 3 个字段。
用户名电子邮件和密码。
因此,将使用将显示在身份验证页面上的用户名创建帐户,但我想将电子邮件发送到电子邮件字段,但如何将其发送到 editTextEmail 中指定的电子邮件。这是我使用的代码。
package net.simplifiedlearning.firebaseauth;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.Patterns;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthUserCollisionException;
import com.google.firebase.auth.FirebaseUser;
import java.util.AbstractCollection;
public class ChildSignUpActivity extends AppCompatActivity implements View.OnClickListener{
EditText editTextEmail,editTextUsername, editTextPassword;
ProgressBar progressBar;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
editTextUsername = (EditText) findViewById(R.id.editTextUsername);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
mAuth = FirebaseAuth.getInstance();
findViewById(R.id.buttonSignUp).setOnClickListener(this);
findViewById(R.id.textViewLogin).setOnClickListener(this);
}
private void registerUser (){
String email = editTextEmail.getText().toString().trim();
String username = editTextUsername.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
if (email.isEmpty()) {
editTextEmail.setError("Email is required");
editTextEmail.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
editTextEmail.setError(("Please enter a valid email"));
editTextEmail.requestFocus();
return;
}
if (password.isEmpty()) {
editTextPassword.setError("Password is required");
editTextPassword.requestFocus();
return;
}
if (password.length() < 6) {
editTextPassword.setError("Your password must be at least 6 characters");
return;
}
progressBar.setVisibility(View.VISIBLE);
mAuth.createUserWithEmailAndPassword(username, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull final Task<AuthResult> task) {
if (task.isSuccessful()) {
// send user verification link using firebase
FirebaseUser user = mAuth.getCurrentUser();
user.sendEmailVerification().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(ChildSignUpActivity.this, "Verification Email Has Been Sent", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(), task.getException().getMessage(), Toast.LENGTH_SHORT).show();;
}
});
Toast.makeText(getApplicationContext(), "User Register Succesfull", Toast.LENGTH_SHORT).show();
} else {
if(task.getException() instanceof FirebaseAuthUserCollisionException) {
Toast.makeText(getApplicationContext(), "User already exists", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), task.getException().getMessage(), Toast.LENGTH_SHORT);
}
}
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.buttonSignUp:
registerUser();
break;
case R.id.textViewLogin:
startActivity(new Intent(this, MainActivity.class));
break;
}
}
}
所以我想要名为 email 的字段来获取验证电子邮件,任何人都可以提供解决方案吗?
解决方案
在 firestore 中创建一个文档,文档名称为username和 field email。
简介
- fatalcoder524 //用户名
- 电子邮件:test@test.com //电子邮件验证
- dob: 12/12/12 //其他细节
首先检查用户名是否存在于firestore中。
- 如果存在,请使用 firestore 中的电子邮件和用户密码进行验证。
- 如果没有,请让用户注册。
示例代码:(注册)
String email; //user input
String username; //user input
String password; //user input
Map<String, Object> user = new HashMap<>();
user.put("email", email); // data to add to firestore
db.collection("profiles").document(username) //username is set as firestore document name, profiles is the collection name
.set(user) //Store the email in above document.
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) // If data is added successfully
{
mAuth.createUserWithEmailAndPassword(email, password) // create account
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "createUserWithEmail:success");
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "createUserWithEmail:failure", task.getException());
}
}
});
}
});
示例代码:(登录)
String username; //get username from user
String password; //get password from user
DocumentReference docRef = db.collection("profiles").document(username); //get doc reference
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) // if username is present
{
String email=document.getData().email; //get email from document
mAuth.signInWithEmailAndPassword(email, password) //signin with email
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) // if password and email id matches
{
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.getException());
}
}
});
} else {
Log.d(TAG, "No such document");
//add code (user not found/registered)
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
修改您的代码:
FirebaseFirestore db = FirebaseFirestore.getInstance();
private void registerUser (){
String email = editTextEmail.getText().toString().trim();
String username = editTextUsername.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
if (email.isEmpty()) {
editTextEmail.setError("Email is required");
editTextEmail.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
editTextEmail.setError(("Please enter a valid email"));
editTextEmail.requestFocus();
return;
}
if (password.isEmpty()) {
editTextPassword.setError("Password is required");
editTextPassword.requestFocus();
return;
}
if (password.length() < 6) {
editTextPassword.setError("Your password must be at least 6 characters");
return;
}
progressBar.setVisibility(View.VISIBLE);
Map<String, Object> user = new HashMap<>();
user.put("email", email);
db.collection("profiles").document(username) //username is set as firestore document name, profiles is the collection name
.set(user) //Store the email in above document.
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) // If data is added successfully
{
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull final Task<AuthResult> task) {
if (task.isSuccessful()) {
// send user verification link using firebase
FirebaseUser user = mAuth.getCurrentUser();
user.sendEmailVerification().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(ChildSignUpActivity.this, "Verification Email Has Been Sent", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(), task.getException().getMessage(), Toast.LENGTH_SHORT).show();;
}
});
Toast.makeText(getApplicationContext(), "User Register Succesfull", Toast.LENGTH_SHORT).show();
} else {
if(task.getException() instanceof FirebaseAuthUserCollisionException) {
Toast.makeText(getApplicationContext(), "User already exists", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), task.getException().getMessage(), Toast.LENGTH_SHORT);
}
}
}
});
}
});
}
推荐阅读
- android - 检查 Chromecast 是否可以播放视频文件
- json - 在c#中将JSON数据反序列化到一个类中
- java - 字符串子字符串故意删除字符 0
- amazon-web-services - 将“cloud-provider=aws”与 kubeadm 一起使用时,kube-controller-manager 无法启动
- tkinter - 如何在 Tkinter 窗口上显示图像?
- java - 按下退出键时取消表格选择
- r - 将图像从帧数据保存到R中的文件
- neo4j - 将 csv 加载到 neo4j 的分步说明
- angular - Angular 4 - 格式化 SQL 时间
- html - 将高度和宽度调整为与 flexbox 相等