简体   繁体   中英

I keep getting authentication denied when i try to log into my firebase app

I have a app that will allow people to login and when they get logged in they a firebase DB reference should be made on with thier user id as the child. I my app is connected to a firebase console and on that console I have email and password login enabled. My rules are set to have read write and validate set to true. Below is a snippet of my code.


package com.example.movir;

import androidx.annotation.NonNull;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

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.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class RentersLoginActivity extends AppCompatActivity {

    private static final String TAG = "";

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener firebaseAuthStatelistener;




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_renters_login);

// ...
// Initialize Firebase Auth
        mAuth = FirebaseAuth.getInstance();



        final EditText usernameEditText =(EditText) findViewById(R.id.username);
        final EditText passwordEditText =(EditText) findViewById(R.id.password);
        final Button loginButton = (Button) findViewById(R.id.login);
        final Button registerButton =(Button) findViewById(R.id.register_button);

        final ProgressBar loadingProgressBar = findViewById(R.id.loading);

        firebaseAuthStatelistener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                if (user != null) {
                    Intent intent = new Intent(RentersLoginActivity.this, RentersMenuPageActivity.class);
                    startActivity(intent);
                    finish();
                    return;
                }

            }
        };


        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = usernameEditText.getText().toString();
                 String password = passwordEditText.getText().toString();
                mAuth.signInWithEmailAndPassword(email, password)
                        .addOnCompleteListener(RentersLoginActivity.this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (!task.isSuccessful()) {
                                    // Sign in success, update UI with the signed-in user's information
                                    Log.w(TAG, "signInWithEmail:failure", task.getException());
                                    Toast.makeText(RentersLoginActivity.this, "Authentication failed.",
                                            Toast.LENGTH_SHORT).show();

                                } else {
                                    // If sign in fails, display a message to the user.

                                    Log.d(TAG, "signInWithEmail:success");
                                    String user_id= mAuth.getCurrentUser().getUid();
                                    DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child("Renters").child(user_id);
                                    current_user_db.setValue(true);
                                    // ...
                                }


                                // ...
                            }
                        });

            }
        });
registerButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String email = usernameEditText.getText().toString();
        String password = passwordEditText.getText().toString();
        mAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(RentersLoginActivity.this, new OnCompleteListener< AuthResult >() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "createUserWithEmail:success");
                            String user_id= mAuth.getCurrentUser().getUid();
                            DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child("Renters").child(user_id);
                            current_user_db.setValue(true);
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "createUserWithEmail:failure", task.getException());
                            Toast.makeText(RentersLoginActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }

                        // ...
                    }
                });

    }
});



    }

}

When i click the register button i get the "Authentication Failed" error I even tried making a user from console and trying the login button instead with the credentials I made for the user and I still get the error. What should I do?

I fixed this. I had the wrong google-servies.json file. I had a file from an old firebase project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM