0

I have an application there is user login screen sends to an activity if logging action is OK. But everytime I closed the application, app asks for email and password, I want to stay logged in like instagram or facebook. Have can I do that? And also how can I do that, do I have to change the code in signin activity or create another class for saving the current user, I am so much confused. There is my login code for firebase:

SignInActivity;

public class SignInActivity extends AppCompatActivity  {

private EditText SignInMail, SignInPass;
private FirebaseAuth auth;
private Button SignInButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Get Firebase auth instance
    auth = FirebaseAuth.getInstance();



    // set the view now
    setContentView(R.layout.activity_signin);
    SignInMail = (EditText) findViewById(R.id.SignInMail);
    SignInPass = (EditText) findViewById(R.id.SignInPass);
    SignInButton = (Button) findViewById(R.id.SignInButton);

    //Get Firebase auth instance
    auth = FirebaseAuth.getInstance();

    SignInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String email = SignInMail.getText().toString();
            final String password = SignInPass.getText().toString();
            if (TextUtils.isEmpty(email)) {
                Toast.makeText(getApplicationContext(), "Mail", Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(password)) {
                Toast.makeText(getApplicationContext(), "Password", Toast.LENGTH_SHORT).show();
                return;
            }
            //authenticate user
            auth.signInWithEmailAndPassword(email, password)
                    .addOnCompleteListener(SignInActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            // If sign in fails, display a message to the user. If sign in succeeds
                            // the auth state listener will be notified and logic to handle the
                            // signed in user can be handled in the listener.
                           // progressBar.setVisibility(View.GONE);
                            if (!task.isSuccessful()) {
                                // there was an error
                                if (password.length() < 8) {
                                    Toast.makeText(getApplicationContext(),"pass min 8",Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                Intent intent = new Intent(SignInActivity.this, CampaignActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        }
                    });
        }
    });

   }


public void NavigateSignUp(View v) {
    Intent inent = new Intent(this, SignupActivity.class);
    startActivity(inent);
}
public void NavigateForgetMyPassword(View v) {
    Intent inent = new Intent(this, ResetPasswordActivity.class);
    startActivity(inent);
}
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

4 Answers4

0

When a user logged in successfully store the LOGIN TYPE of User SharedPreferenceence and check that flag again when user restart app. If Shared Preference contains value then just take him to Main Screen.

Like this on each login update this value and check

PreferencesManager.getInstance().getString(ANNONYMOUS_SIGNUP_DATE, "")) && (PreferencesManager.getInstance().getInt(LOGIN_TYPE, 0) == LOGIN_TYPE_ANNONYMOUS)
Ankit Tale
  • 1,924
  • 4
  • 17
  • 30
0

In the onCreate function, you need to add this piece of code

FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();

This code will fetch you the currently logged in user if the user has previously signed in, else will return null.

Check this link for further understanding.

Get the currently signed-in user - Firebase Docs

I hope this solved your problem. If you feel this answer is correct, please accept the answer.

BennyHawk
  • 172
  • 2
  • 11
0

Since you authenticated the user, then you can create a splash screen before your sign in activity, and write the following code:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null){
   Intent i = new Intent(SplashActivity.this, HomeActivity.class);
   } else{
   Intent i = new Intent(SplashActivity.this, SignInActivity.class);
   }

Here, you check if currently there is a logged in user and then navigate to the right activity according to the condition.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
0

I would recommend you to use splash screen first and check it user is logged in by the following

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // User is signed in
    // go to main page
} else {
    // No user is signed in
    // go to loging page
}
Abdulkadir Ugas
  • 415
  • 5
  • 13