1

I'm working on sign up of my project and for authentication I'm using Firebase. Now I want that my code only verify or authenticate only those email addresses which are already created by gmail or yahoo or hotmail. new email address can be authenticate. If user write email which is not created in gmail or yahoo or hotmail. It should not able to register. But my code is taking every random email address whether it is already exists or not. and in firebase authentication table it is showing that email address too. Now what mistake i'm making in my code which is authenticating every email address.

Code:

package com.example.animation;

import android.content.Intent;
import android.graphics.Paint;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
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;

public class Signup extends AppCompatActivity {
    RelativeLayout myLayout;
    AnimationDrawable animationDrawable;
    private EditText email_signup, password_signup, name_signup;
    private Button signup_btn;
    private FirebaseAuth firebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        timing();
        setValues();
        TextView txt=findViewById(R.id.txt_signup);
      txt.setPaintFlags(txt.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
      firebaseAuth= FirebaseAuth.getInstance();
        signup_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(validate()){
                    // database
                    String user_email=email_signup.getText().toString().trim();
                    String user_password=password_signup.getText().toString().trim();
                    String user_name=name_signup.getText().toString().trim();

                    firebaseAuth.createUserWithEmailAndPassword(user_email,user_password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if(task.isSuccessful())
                            {
                                Toast.makeText(Signup.this,"Registeration Successful",Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(Signup.this,MainActivity.class));
                            }
                            else
                            {
                                Toast.makeText(Signup.this,"Not Successful",Toast.LENGTH_SHORT).show();
                            }

                        }
                    });
                }

            }
        });
    }
    private void timing(){
        myLayout=findViewById(R.id.signup_layout);
        animationDrawable=(AnimationDrawable)myLayout.getBackground();
        animationDrawable.setEnterFadeDuration(2500);
        animationDrawable.setExitFadeDuration(2500);
        animationDrawable.start();

    }

    private void setValues(){
        email_signup=findViewById(R.id.email_reg);
        password_signup=findViewById(R.id.pass_reg);
        name_signup=findViewById(R.id.namebox);
        signup_btn=findViewById(R.id.btn_register);


    }
    private Boolean validate(){
    Boolean result=false;
    String name=name_signup.getText().toString();
    String password=password_signup.getText().toString();
    String  email=email_signup.getText().toString();

    if(name.isEmpty()&& password.isEmpty()&&email.isEmpty()){

        Toast.makeText(this,"Please enter all Details",Toast.LENGTH_SHORT).show();
    }
    else{
        result=true;
    }
    return result;
    }
}

This is firebase authenticate table: enter image description here

And this is where i registered: enter image description here

enter image description here

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Aj Styles
  • 143
  • 6
  • So you want to let users sign-up with any email address but login only with gmail, yahoo and hotmail, right? – Alex Mamo Jun 20 '19 at 13:58
  • No, i want user to signup with that email address which is already created on gmail, yahoo or hotmail. Like first i will create id on gmail (ajstyles25@gmail.com) and i want that when i signup in my app and this id it only register this email address. Right now if i'm writing (ajstyle0900@gmail.com) it will accept it, which i don't want – Aj Styles Jun 20 '19 at 14:05

1 Answers1

0

According to your comment:

i want user to signup with that email address which is already created on gmail, yahoo or hotmail. Like first i will create id on gmail (ajstyles25@gmail.com) and i want that when i signup in my app and this id it only register this email address. Right now if i'm writing (ajstyle0900@gmail.com) it will accept it, which i don't want

To solve this, you need to keep track of your users. This means that you'll have to save the email address of your users in a database. For that I recommend you the new Cloud Firestore database or Firebase realtime database. Now to check if a user exist in Cloud Firestore, please see my answer from the following post:

And to check if the user exist in Firebase realtime database, please see my answer from the following post:

In this way, you can call createUserWithEmailAndPassword() only if the user already exists in the database.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193