Search code examples
javaandroidfirebasefailed-to-connect

FireBase android: User Registration is not working


I have created one Login Registration app in android and using Firebase for storing the data. So, after giving email and password, when I'm clicking on the Register button, it's showing Registration Failed Notification. What should I do?

The codes are given Below:

Registration Activity

package com.shankhadeep.firebasedemo;

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.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;
import com.google.firebase.auth.FirebaseUser;

public class Registration extends AppCompatActivity {


    EditText edt_email_reg, edt_password_reg;
    Button btn_register;
    private FirebaseAuth auth;

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

        edt_email_reg = findViewById(R.id.edt_email_reg);
        edt_password_reg = findViewById(R.id.edt_password_reg);
        btn_register = findViewById(R.id.btn_register);

        auth = FirebaseAuth.getInstance();



        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email= edt_email_reg.getText().toString().trim();
                String password = edt_password_reg.getText().toString().trim();

                if(TextUtils.isEmpty(email) || TextUtils.isEmpty(password)){
                    Toast.makeText(Registration.this,"Empty Credentials",Toast.LENGTH_LONG).show();


                }

                else if(password.length()<6){
                    Toast.makeText(Registration.this,"Password too short! Minimum 8 characters required",Toast.LENGTH_LONG).show();

                }

                else{
                    registerUser(email,password);
                }
            }
        });
    }

    private void registerUser(String email, String password) {

        auth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(Registration.this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()){
                    Toast.makeText(Registration.this,"Registartion failed", Toast.LENGTH_LONG).show();

                    FirebaseUser user = auth.getCurrentUser();

                }
                else {
                    Toast.makeText(Registration.this,"Registartion failed", Toast.LENGTH_LONG).show();
                }
            }
        });

    }
}

Build.gradle(app level)

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"
    defaultConfig {
        applicationId "com.shankhadeep.firebasedemo"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
    implementation 'com.google.firebase:firebase-database:17.0.0'
    implementation 'com.google.firebase:firebase-auth:17.0.0'
    implementation 'com.google.firebase:firebase-analytics:17.5.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

Build.gradle(project level)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
        classpath 'com.google.gms:google-services:4.3.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

**Here is the notification I'm getting: **

In the Log-cat it's showing

Log-cat:

Any suggestion would be fine for me.


Solution

  • Please, check the enable status this functionality into your Firebase account, where you can enable the option to login via email.

    Open this menu: enter image description here