Keep login active in the application android (firebase)

0

Good morning, I'm having a hard time, I made an application using firebase authentication and every time the application is opened it asks for login, how do I keep this login active once it has been done and open the "restricted" part of the direct application? p>

Here's how the login method works:

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.

    
asked by anonymous 11.05.2017 / 15:25

1 answer

0

In method onComplete replace with 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();
}
    
10.10.2017 / 21:32