1

I'am try to register a user in my android app with using Google Firebase authentication option, but i am getting error and my Toast handle my error in this code line Toast.makeText(RegistrationActivity.this, "Sign Up Error!", Toast.LENGTH_SHORT).show();

I think my SHA-1 and firebase connection was linked right. Firebase console image attached below firebase console

I'm here add my source code and also xml file.

package com.example.anu.activityone;

import android.content.Intent;
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.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 RegistrationActivity extends AppCompatActivity {

private EditText txtEmail, txtPassword;
private Button btnRegister;

private FirebaseAuth mAuth;
/*
    public abstract class FirebaseAuth extends Object
    The entry point of the Firebase Authentication SDK.
 */
private FirebaseAuth.AuthStateListener FirebaseAuthStateListener;
/*  FirebaseAuth.AuthStateListener  is a interface,
    that Listener called when there is a change in the authentication state.
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_registration);

    // method called when user logged in or logged out
    mAuth = FirebaseAuth.getInstance();
    FirebaseAuthStateListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            if(user != null){
                Intent intent = new Intent(RegistrationActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
                return;
            }
        }
    };

    txtEmail = (EditText)findViewById(R.id.email);
    txtPassword = (EditText)findViewById(R.id.password);
    btnRegister = (Button)findViewById(R.id.register);

    // Register a new user
    btnRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final String email = txtEmail.getText().toString();
            final String password = txtPassword.getText().toString();
            /*
                Support User Registration
                With password-based authentication, new users must register themselves by providing
                a unique email address and a password. To add this functionality to your app,
                you can use the createUserWithEmailAndPassword() method of the FirebaseAuth class.
                As its name suggests, the method expects an email address and a password as its arguments.
             */
            mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(RegistrationActivity.this, new OnCompleteListener<AuthResult>() {
                /*
                    public interface OnCompleteListener
                    Listener called when a Task completes.
                 */
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if(!task.isSuccessful()){
                        Toast.makeText(RegistrationActivity.this, "Sign Up Error!", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            /*
                To be able to determine the result of the createUserWithEmailAndPassword() method,
                you must add an OnCompleteListener to it using the addOnCompleteListener() method.
             */
        }
    });
}
// To start Firebase authentication
@Override
protected void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(FirebaseAuthStateListener);
}
// To stop Firebase authentication when app pause
@Override
protected void onStop() {
    super.onStop();
    mAuth.removeAuthStateListener(FirebaseAuthStateListener);
}
}

XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.anu.activityone.RegistrationActivity"
android:orientation="vertical">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="email"
    android:id="@+id/email"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="password"
    android:id="@+id/password"/>

<Button
    android:id="@+id/register"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Registration" />
</LinearLayout>
2
  • 1
    Update your Toast to show the reason for the failure. Add: task.getException().getMessage() Commented Nov 17, 2017 at 4:09
  • that is worked nice. i found the error. tnx Commented Nov 17, 2017 at 5:29

2 Answers 2

1

Try this code:

Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
                        Toast.LENGTH_SHORT).show();

for getting complete information why login is failed and then update your question with Error occured

Sign up to request clarification or add additional context in comments.

Comments

0

I found the error and i also print it using Toast by using this code line

Toast.makeText(LoginActivity.this, "User Authentication Failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.