0

i am using firebase and i developed an android app(java) where users login through Phone. but when the user login and finished the app and restart the app it shows again the login activity and again OTP is to be sent. How to solve this. the code of the main activity is given.

'''
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class MainActivity extends AppCompatActivity {
    private EditText editTextMobile;
    public Button buttonContinue;
    private FirebaseAuth firebaseAuth;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);
        editTextMobile=findViewById(R.id.editTextMobile);
        buttonContinue=findViewById(R.id.buttonContinue);
        buttonContinue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            String mobile=editTextMobile.getText().toString().trim();
            if(mobile.isEmpty()|| mobile.length()<10){
                editTextMobile.setError("Enter Valid number");
                editTextMobile.requestFocus();
                return;
            }
                Intent intent=new Intent(MainActivity.this, VarifyPhoneNumber.class);
                intent.putExtra("mobile", mobile);
            startActivity(intent);
            }
        });

    }
    @Override
    public void onStart() {
        super.onStart();
        if(firebaseAuth.getCurrentUser()!=null){
            Intent intent=new Intent(MainActivity.this,ProfileActivity.class);
            startActivity(intent);
        }
      }




}
'''
  • I think you are looking for this **[approach](https://stackoverflow.com/questions/50885891/one-time-login-in-app-firebaseauth)**. – Alex Mamo Sep 15 '20 at 08:12

3 Answers3

0

To keep the user logged in, after the user successfully verified their phone number you should save the user state or token in SharedPreference. Later when the user restarts the app, you have to check the state from the value you saved in SharedPreference. If the user state is authenticated or the token is valid then you have to redirect the user to any activity you want, else redirect them to the login screen.

Lee Royld
  • 75
  • 9
0

Next First you check that if user is null or not. if user is null than direct it to sign otherwise redirect it

private boolean checkLoggedIn() {
    return FirebaseAuth.currentUser() != null;
}
Kiryl Tkach
  • 3,118
  • 5
  • 20
  • 36
0

When you are restarting your app your app wont store the login credential with it self. So you will be prompted to the login screen. For that you have to save the login credential inside your application. To save you can use SharedPreferences. if login was valid save your credential(token) inside the preferences. if you stored inside the SharedPreferences you can use it to validate the user each time when app starts.

       if (isValidLoogin) {
        SharedPreferences preferences = getSharedPreferences("login", MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("UserName",uname);
        editor.putString("SecretKey",pass);
        editor.apply();


        Intent intent=new Intent(MainActivity.this,ProfileActivity.class);
        startActivity(intent);
Sindujan Nirmalan
  • 470
  • 1
  • 8
  • 18
  • how can I do this for the phone sign-in method? I mean otp login – idrees khan Sep 15 '20 at 09:16
  • how can i do this for users who log-in through Phone number? – idrees khan Sep 15 '20 at 09:30
  • once you logged in successfully with your otp create some boolean value , make it true and save it inside the sharedPreferences. whenever app try to login first check with sharedPreferences whether the boolean is true or false and according that value you can change the next activity – Sindujan Nirmalan Sep 16 '20 at 03:07