Does not authenticate on firebase

0

I had problems with a project in which I was authenticating with firebace I was using email and google as authentication, I started another project, I also created another project in firebase and I used the same codes as before if they worked for this new application and I also put the necessary compiles and it does not authenticate me, if I want to register an email it will not let me and if login with google send me a toast that I made error and in the firebase does not save that login.

---- build app -----

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
//Firebaase
compile 'com.google.android.gms:play-services-auth:11.2.2'
compile 'com.google.firebase:firebase-auth:11.2.2'
compile 'com.google.android.gms:play-services-auth:11.2.2'
//suport
compile 'com.android.support:appcompat-v7:26.0.2'
compile 'com.android.support:multidex:1.0.2'
testCompile 'junit:junit:4.12'}
apply plugin: 'com.google.gms.google-services'

---- Code to log in with google

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();


    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                    Toast.makeText(MainActivity.this, "Algo Salio Mal", Toast.LENGTH_SHORT).show();
                }
            }).addApi(Auth.GOOGLE_SIGN_IN_API,gso).build();



    btnCuenta.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(v.getContext(),CorreoElectronico.class);
            startActivity(intent);
        }
    });

    btnCorreo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(v.getContext(),CorreoElectronico.class);
            startActivity(i);
        }
    });


}

private void inicializarAutenticacion() {
    mAuth = FirebaseAuth.getInstance();
    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser usr = firebaseAuth.getCurrentUser();
            if(usr != null)
            {
                startActivity(new Intent(MainActivity.this,Main2Activity.class));

            }else{
                Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
            }
        }
    };
}



private void cerrarSesionFirebase()
{
    mAuth.signOut();
}




private void goMainScreen() {
    // Intent in = new Intent(MainActivity.this,Main2Activity.class);
    Intent in = new Intent();
    in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(in);
}


// GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
//     .requestIdToken(getString(R.string.default_web_client_id))
//   .requestEmail()
// .build();
private void signIn()
{
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent,RC_SIGN_IN);
}


public void onActivityResult(int requestCode, int resultCode, Intent data)
{

    super.onActivityResult(requestCode,resultCode,data);



    if(requestCode == RC_SIGN_IN)
    {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if(result.isSuccess())
        {
            GoogleSignInAccount account = result.getSignInAccount();
            firebaseAuthWithGoogle(account);
            startActivity(new Intent(MainActivity.this,Main2Activity.class));
        }else
        {
            Toast.makeText(MainActivity.this, "Autentificacion Fallida", Toast.LENGTH_SHORT).show();
        }
    }
}
private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
    AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(),null);
    mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>()  {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if(task.isSuccessful())
            {
                Log.d( "TAG", "signInWithCredential:success");
                FirebaseUser user = mAuth.getCurrentUser();
                // updateUI(user);

            }else{
                Log.w("TAG","signWithCredential:failure",task.getException());
                Toast.makeText(MainActivity.this, "Autentificacion fallida", Toast.LENGTH_SHORT).show();
                //updateUI(null);
            }
        }
    });
}

------------ Code to start with email and password

 createUser.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            userEmail = email.getText().toString().trim();
            userPass = password.getText().toString().trim();
            if(userEmail.equals("") || userPass.equals(""))
            {
                Toast.makeText(CorreoElectronico.this, "Se dejaron campos vacios", Toast.LENGTH_SHORT).show();
            }

            if(!TextUtils.isEmpty(userEmail) && !TextUtils.isEmpty(userPass))
            {
                mAuth.createUserWithEmailAndPassword(userEmail,userPass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        // startActivity(new Intent(CorreoElectronico.this,Main2Activity.class));
                        if(task.isSuccessful())
                            Toast.makeText(CorreoElectronico.this, "Usuario Creado", Toast.LENGTH_SHORT).show();
                        else
                        {
                            Toast.makeText(CorreoElectronico.this, "Error al Crear el Usuario", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }


        }
        });



    //move to login
    login.setOnClickListener((View v) -> {
        //startActivity(new Intent(CorreoElectronico.this,Main2Activity.class));

        userEmail = email.getText().toString().trim();
        userPass = password.getText().toString().trim();

        if(userEmail.equals("")||userPass.equals(""))
        {
            Toast.makeText(CorreoElectronico.this, "Existen Campos Vacios", Toast.LENGTH_SHORT).show();
        }


        if(!TextUtils.isEmpty(userEmail) && !TextUtils.isEmpty(userPass))
            mAuth.signInWithEmailAndPassword(userEmail, userPass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        startActivity(new Intent(CorreoElectronico.this, Main2Activity.class));
                        Toast.makeText(CorreoElectronico.this, "Bienvenido", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(CorreoElectronico.this, "Fallo al Ingresar", Toast.LENGTH_SHORT).show();
                    }
                }
            });

    });



}
    
asked by Aitor 11.09.2017 в 05:43
source

1 answer

0

In addition to the code, the new project should include the configuration file (google-services.json) that corresponds to the new project. This file has specific configurations that allow you to access the Firebase services for each project and application package id.

    
answered by 30.09.2017 в 16:31