Firebase email authentication android

Firebase email authentication android is a process by which we can validate email address of the user. By this we can add a layer of the authentic user. Now a days its is quite useful. Manually if we need to validate the user we need to create a whole system to do so.

Like we need to deploy the database and api and send the email to authenticate user. Instead of doing all this we can just use the firebase email authentication method which saves a lot of time and dependency on the backend technologies.

So lets get started Firebase email authentication android

1- I hope you have already created the account on the firebase console if not you can go thorught this link.

2- Go to firebase console.

3- Go to Authentication and Sign-in Methods Enable email authentication then click save button.

Firebase email authentication

4- To send verification link from the android app create a email password field and submit button added in EmailVerificationActivity.

package com.example.sampleproject;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

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 EmailVerificationActivity extends AppCompatActivity {

private FirebaseAuth mAuth;
private Helper helper;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_email_verification);
mAuth= FirebaseAuth.getInstance();
helper = new Helper(this);

((Button)findViewById(R.id.bt_submit)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText email = (EditText)findViewById(R.id.et_email);
String email_string = email.getText().toString();
EditText et_password = (EditText)findViewById(R.id.et_password);
String pasword = et_password.getText().toString();
createNewUser(email_string,pasword);

}
});
}
void createNewUser(String email,String password){
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(EmailVerificationActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
// Toast.makeText(SignupMaterial.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
// If sign in fails, helper.display a message to the admin. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in admin can be handled in the listener.

if (!task.isSuccessful()) {
// admin alreeady exists
// VerifyEmail();
helper.display("Email already exists");
helper.hideDialog();
} else {
VerifyEmail();
// startActivity(new Intent(SignupActivity.this, ChangeMailActivity.class));
// finish();
}
}


});
}
private void VerifyEmail() {
final FirebaseUser user = mAuth.getCurrentUser();
if (user != null){
user.sendEmailVerification()
.addOnCompleteListener(EmailVerificationActivity.this, (OnCompleteListener) task -> {
// Re-enable button
if (task.isSuccessful()) {
FirebaseUser firebase_user = mAuth.getCurrentUser();
String firebase_uid=firebase_user.getUid();
helper.display("Email verification send");

} else {
Log.e("", "sendEmailVerification", task.getException());
helper.display("Failed to send verification email.");
}
});
}else {
// Toast.makeText(SignupMaterial.this, "null", Toast.LENGTH_SHORT).show();
}

}
}

on submit button email will be send.

Note: email should be unique.

email from firebase

Tagged : /