Tutorial 5 - Email Verification

 



This is Sample Project Which you can improve UI design & Coding skills .

*Once user is Registered Automatically send Verification Email

If you get any trouble. Please Contact me on WhatsApp!!

RegisterActivity

activity_home

HomeActivity

package com.rrmchathura.myfirstapp;

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

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
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.FirebaseUser;
import com.google.firebase.database.annotations.NotNull;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;

import java.util.HashMap;
import java.util.Map;

public class RegisterActivity extends AppCompatActivity {

public static final String TAG = "TAG";
private EditText mFullName,mEmail,mPassword,mPhone;
private Button mRegisterBtn;
ProgressBar progressBar;

private FirebaseAuth fAuth;
FirebaseFirestore fStore;

String userID;

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

mFullName = findViewById(R.id.fullName);
mEmail = findViewById(R.id.email);
mPassword = findViewById(R.id.password);
mPhone = findViewById(R.id.phone);
mRegisterBtn = findViewById(R.id.registerBtn);

fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
progressBar = findViewById(R.id.progressBar);

if (fAuth.getCurrentUser() != null){
startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
finish();
}

mRegisterBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
String fullName = mFullName.getText().toString();
String phone = mPhone.getText().toString();


if (TextUtils.isEmpty(fullName)){
mFullName.setError("Full Name is Required");
return;
}
if (TextUtils.isEmpty(email)){
mEmail.setError("Email is Required");
return;
}
if (TextUtils.isEmpty(phone)){
mPhone.setError("Phone is Required ");
return;
}
if (phone.length() < 10){
mPhone.setError("Invalid Number");
}
if (TextUtils.isEmpty(password)){
mPassword.setError("Password is Required");
return;
}
if (password.length() < 6){
mPassword.setError("Password Must be >= 6 Characters");
}

progressBar.setVisibility(View.VISIBLE);

//register the user
fAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull @NotNull Task<AuthResult> task) {
if (task.isSuccessful()){

////send verification link onece user Registerd

FirebaseUser fuser = fAuth.getCurrentUser();
fuser.sendEmailVerification().
addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Toast.makeText(RegisterActivity.
this,"Verification Email Has been Sent",
Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG,"onFailure Email sent " + e.getMessage());
}
});


Toast.makeText(RegisterActivity.
this,"User is Created",Toast.LENGTH_SHORT).show();
userID = fAuth.getCurrentUser().getUid();
DocumentReference documentReference = fStore.
collection("users").document(userID);
Map<String,Object> user = new HashMap<>();
user.put("fName",fullName);
user.put("email",email);
user.put("phone",phone);
documentReference.set(user).
addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Log.d(TAG,"onSuccess: user Profile is created for "+ userID);
}

});
startActivity(new Intent(RegisterActivity.
this,LoginActivity.class));

}else {
Toast.makeText(RegisterActivity.this,
"Error !" + task.getException().getMessage(),
Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);

}
}
});

}
});

}

public void Registertext(View view) {
startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
finish();
}

}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".HomeActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/top_gradient" />

<TextView

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Covid Tracker"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:height="30dp"
android:fontFamily="sans-serif-smallcaps"
android:textAlignment="center"
android:textSize="25dp"
android:textColor="@color/white"
android:gravity="center_horizontal" />

<ScrollView

android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="40dp"
android:layout_marginRight="10dp"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Your Profile"
android:fontFamily="@font/baloo"
android:textAlignment="center"
android:textSize="20dp"
android:gravity="center_horizontal" />

<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:src="@drawable/ic_user"
android:scaleType="centerCrop"
/>

<EditText
android:id="@+id/profileName"
android:layout_width="350dp"
android:layout_height="45dp"
android:editable="false"
android:textColor="@color/textcolor"
android:background="@drawable/input_style"
android:drawableRight="@drawable/ic_user"
android:hint="Your Name"
android:layout_gravity="center"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:layout_marginTop="10dp"
android:fontFamily="@font/baloo"
/>

<EditText
android:id="@+id/profileEmail"
android:layout_width="350dp"
android:layout_height="45dp"
android:editable="false"
android:background="@drawable/input_style"
android:drawableRight="@drawable/ic_email"
android:hint="Your Email Address"
android:textColor="@color/textcolor"
android:layout_gravity="center"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:layout_marginTop="10dp"
android:fontFamily="@font/baloo"
/>

<EditText
android:id="@+id/profilePhone"
android:layout_width="350dp"
android:layout_height="45dp"
android:editable="false"
android:background="@drawable/input_style"
android:drawableRight="@drawable/ic_phone"
android:hint="Your Phone"
android:textColor="@color/textcolor"
android:layout_gravity="center"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:layout_marginTop="10dp"
android:fontFamily="@font/baloo"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/verifyMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email Not Verified !"
android:visibility="invisible"
android:textAlignment="center"
android:gravity="center_horizontal"
android:layout_marginTop="30dp"
android:fontFamily="@font/brandon_grotesque_black"
android:textSize="20dp"
android:textColor="@color/col_red"
/>
<Button
android:id="@+id/resendCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="invisible"
android:text="Verify Now"
android:background="@drawable/button_style"
android:textAllCaps="true"
android:layout_marginTop="5dp"
android:textStyle="bold"
android:textColor="#FFF"
/>

</LinearLayout>



</LinearLayout>
</ScrollView>
<Button
android:id="@+id/btn_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_style"
android:layout_alignParentEnd="true"
android:text="Log Out"
android:textSize="18dp"
android:textStyle="bold"
android:textColor="#FFF"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>

</RelativeLayout>

package com.rrmchathura.myfirstapp;

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

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;

public class HomeActivity extends AppCompatActivity {

private EditText fullName,email,phone;
private TextView verifyMsg;
FirebaseAuth fAuth;
FirebaseFirestore fStore;
String userID;
Button resendCode;

private Button logout;


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

fullName = findViewById(R.id.profileName);
email = findViewById(R.id.profileEmail);
phone = findViewById(R.id.profilePhone);

logout = findViewById(R.id.btn_logout);

resendCode = findViewById(R.id.resendCode);
verifyMsg = findViewById(R.id.verifyMsg);

verifyMsg.startAnimation((Animation) AnimationUtils.
loadAnimation(HomeActivity.this,R.anim.animation_logo));



fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();



userID = fAuth.getCurrentUser().getUid();
FirebaseUser user = fAuth.getCurrentUser();

if (!user.isEmailVerified()){
resendCode.setVisibility(View.VISIBLE);
verifyMsg.setVisibility(View.VISIBLE);


//verify Button Ckicked


resendCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {

user.sendEmailVerification().
addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Toast.makeText(HomeActivity.
this,"Verification Email Has been Sent",
Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("TAG","onFailure Email sent " +
e.getMessage());
}
});
}
});
}





DocumentReference documentReference = fStore.
collection("users").document(userID);
documentReference.
addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot,
@Nullable FirebaseFirestoreException error) {
phone.setText(documentSnapshot.getString("phone"));
fullName.setText(documentSnapshot.getString("fName"));
email.setText(documentSnapshot.getString("email"));
}
});

logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
Toast.makeText(HomeActivity.
this,"Logeed Out!",Toast.LENGTH_SHORT).show();
startActivity(new Intent(HomeActivity.
this,LoginActivity.class));
finish();
}
});
}
}

Post a Comment

0 Comments

Youtube Channel Image
Coding With Chathura Subscribe To watch more Project Tutorials
Subscribe
Do you have any doubts? chat with us on WhatsApp
Hello, How can I help you? ...
Click me to start the chat...

Welcome to freesourcecodelk

X