Issue
What actually i am doing here, When a user login to their account, Then i am checking that whatever they had verified their email address or not, If not then i am starting the EmailVerificationActivity
.
From where when user click on SEND VERIFICATION EMAIL Button an Email Verification code will be sent to user's email address.
after that when user successfully verified their email address, when they click SEND VERIFICATION LINK Button again.
Instead of showing toast message
Toast.makeText(this, "Your email has been verified, Now you can login.", Toast.LENGTH_LONG).show();
,
sending the Email Verification link again.
Why
isEmailVerified()
returning thefalse
condition.
This is my EmailVerificationActivity
package com.socialcodia.sherewatan;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class EmailVerificationActivity extends AppCompatActivity {
private TextView tvEmailAddress;
private Button btnSendVerificationEmail, btnSignOut;
//Firebase
FirebaseAuth mAuth;
FirebaseUser mUser;
String email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_email_verification);
//Init
tvEmailAddress = findViewById(R.id.tvEmailAddress);
btnSendVerificationEmail = findViewById(R.id.btnSendVerificationEmail);
btnSignOut = findViewById(R.id.btnSignOut);
//Firebase Init
mAuth = FirebaseAuth.getInstance();
mUser = mAuth.getCurrentUser();
// get and set email address
email = mUser.getEmail();
tvEmailAddress.setText(email);
btnSignOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signOut();
}
});
btnSendVerificationEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isEmailVerified();
}
});
}
private void isEmailVerified()
{
if (mAuth.getCurrentUser()!=null)
{
boolean isEmailVerified = mAuth.getCurrentUser().isEmailVerified();
if (isEmailVerified)
{
Toast.makeText(this, "Your email has been verified, Now you can login.", Toast.LENGTH_LONG).show();
}
else
{
sendVerificationEmail();
}
}
}
private void sendVerificationEmail()
{
mAuth.getCurrentUser().sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful())
{
Toast.makeText(EmailVerificationActivity.this, "Email verification link has been sent", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(EmailVerificationActivity.this, "Oops! Failed to send email verification link", Toast.LENGTH_SHORT).show();
}
}
});
}
private void sendToLoginWithEmail()
{
Intent intent= new Intent(getApplicationContext(),LoginActivity.class);
intent.putExtra("email",email);
startActivity(intent);
finish();
}
private void signOut()
{
mAuth.signOut();
sendToLoginWithEmail();
}
}
Solution
How i solved the error.
private void isEmailVerified()
{
mAuth.getCurrentUser().reload().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
if (mAuth.getCurrentUser()!=null)
{
boolean isEmailVerified = mAuth.getCurrentUser().isEmailVerified();
if (isEmailVerified)
{
Toast.makeText(getApplicationContext(), "Your email has been verified, Now you can login.", Toast.LENGTH_LONG).show();
}
else
{
sendVerificationEmail();
}
}
}
});
}
Answered By - mufazmi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.