Keep login active on android app (firebase)

Asked

Viewed 858 times

0

Good morning, I’m having a difficulty, I made an application using firebase Authentication and every time the application opens asks login, as I keep this login active after done and open the "restricted" part of the application direct?

See how this the login method:

private void startLogin() {
    final ProgressDialog progressDialog;
    progressDialog = new ProgressDialog(this, R.style.AppCompatAlertDialogStyle);

    String email = mEmailField.getText().toString();
    String password = mPasswordField.getText().toString();

    if(TextUtils.isEmpty(email) || TextUtils.isEmpty(password)){
        Toast.makeText(LoginActivity.this, "Campo vazio", Toast.LENGTH_LONG).show();
    }else{
        progressDialog.setMessage("Aguarde, entrando no aplicativo!");
        progressDialog.show();

        mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(!task.isSuccessful()){
                    Toast.makeText(LoginActivity.this, "Erro no login", Toast.LENGTH_LONG).show();
                }else{
                    startActivity(new Intent(LoginActivity.this, DashActivity.class));
                }

                progressDialog.dismiss();
            }
        });
    }
}

Thank you.

  • 1

    Look at this reply of the neighboring stackoverflow :)

  • opa, just an if!!! thanks!!!

1 answer

0

In the method onComplete substitute for this:

if (task.isSuccessful()) {
    FirebaseUser user = mAuth.getCurrentUser();
    Toast.makeText(SignInActivity.this, "Seja bem vindo: " + user.getDisplayName(), Toast.LENGTH_SHORT).show();
    if (user != null) {// Verifica se o usuario está logado
        startActivity(new Intent(SignInActivity.this, MainActivity.class));
    }
} else {
    // Se não estiver logado
    Log.w(TAG, "signInWithCredential:failure", task.getException());
    Toast.makeText(SignInActivity.this, "Authentication failed.",
            Toast.LENGTH_SHORT).show();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.