I am facing an issue with retrofit login. when i run the application in my cell phone , i logged in with a registered user... login successful... but when i logged out and want to re-login with another account the "application logged me in with the previous account" ... i am not understanding what is this issue please help. i am not understanding why this issue is occurring with my application.
Note: i am using retrofit (with Firebase) in the application for signup and login. Thanks
here is the code of my Login Class
private Button forgot ;
private TextView CreactAccount_text;
private EditText login_email , login_password;
private ProgressDialog mLoginProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
Toolbar toolbar = (Toolbar) findViewById(R.id.login_page_toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar()!= null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
FirebaseApp.initializeApp(this);
// Realm.init(this);
// User Session Manager
Window window = getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
window.setStatusBarColor(getColor(R.color.login_statusbar_color));
}
else {
window.setStatusBarColor(getResources().getColor(R.color.login_statusbar_color));
}
mLoginProgress = new ProgressDialog(this);
CreactAccount_text = (TextView) findViewById(R.id.ui_crateaccount_text);
forgot = (Button) findViewById(R.id.ui_btn_forgot);
forgot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent forgot_password = new Intent(Login_Activity.this , Forgot_Password_Activity.class);
startActivity(forgot_password);
}
});
CreactAccount_text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent move_signup = new Intent(Login_Activity.this , Signup_Activity.class);
startActivity(move_signup);
}
});
login_email = (EditText) findViewById(R.id.ui_login_email);
login_password = (EditText) findViewById(R.id.ui_login_password);
findViewById(R.id.ui_signin).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
usersignin();
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home)
finish();
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(
R.anim.no_anim, R.anim.slide_right_out);
AppUtils.hideSoftKeyboard(this);
}
public void usersignin(){
final String email = login_email.getText().toString().trim();
final String password = login_password.getText().toString().trim();
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
login_email.setError("Email is not correctly formated");
login_email.requestFocus();
return;
}
if (password.isEmpty()){
login_password.setError("password is required");
login_password.requestFocus();
return;
}
if (password.length() < 6){
login_password.setError("Password should be atleast 6 characters");
login_password.requestFocus();
return;
}
if(!TextUtils.isEmpty(email) || !TextUtils.isEmpty(password)){
mLoginProgress.setTitle("Logging In");
mLoginProgress.setMessage("Please wait while we check your credentials.");
mLoginProgress.setCanceledOnTouchOutside(false);
mLoginProgress.show();
SharedPreferenceUtil.storeStringValue(Login_Activity.this, Constants.USERNAME,email);
SharedPreferenceUtil.storeStringValue(Login_Activity.this,Constants.PASSWORD,password);
RetrofitUtil.createProviderAPI().userLogin(email , password).enqueue(loginUser(this));
}
}
@Override
public void onLoginUser(RetrofitClientLogin data) {
if(data.getType().equals(Constants.SUCCESS)){
FirebaseInstanceId.getInstance().getToken();
UtilFirebaseAnalytics.logEvent(Constants.EVENT_LOGIN,Constants.KEY_EMAIL,login_email.getText().toString());
SharedPreferenceUtil.storeBooleanValue(this,Constants.ISUSERLOGGEDIN,true);
if(getIntent() != null && getIntent().getBooleanExtra(Constants.IS_RESULT_ACTIVITY,false)){
setResult(RESULT_OK);
}else{
hideProgressDialog();
openAcitivty(Home_Activity.class);
}
loginOnFirebase();
}
}
private void loginOnFirebase(){
final FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser == null){
final String email = login_email.getText().toString();
final String password = login_password.getText().toString();
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(Login_Activity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
hideProgressDialog();
finish();
}
});
}else{
hideProgressDialog();
finish();
}
}
});
}
}
protected void openAcitivty(Class<?> cls) {
Intent intent = new Intent(this, cls);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
protected void showProgressDialog(String msg) {
try {
if (mLoginProgress != null && !mLoginProgress.isShowing()) {
mLoginProgress.setMessage(msg);
mLoginProgress.show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void hideProgressDialog() {
try {
if (mLoginProgress != null && mLoginProgress.isShowing()) {
mLoginProgress.dismiss();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onSignup(RetrofitClientLogin data) {
}
@Override
public void OnError(String error) {
hideProgressDialog();
Toast.makeText(this,error,Toast.LENGTH_LONG).show();
}
You're using SharedPreferences for storing login credentials. Once user enters valid login credentials (login ID and password) you store it in SharedPreferences in usersignin()
method.
SharedPreferenceUtil.storeStringValue(Login_Activity.this, Constants.USERNAME, email);
SharedPreferenceUtil.storeStringValue(Login_Activity.this, Constants.PASSWORD, password);
Now, I am assuming (you only posted LoginActivity and didn't mention other parts) that your app has Splash page. Which appears before Login page. In which you decide whether the user has already logged in or not (using SharedPreference values). If yes, redirect to Home page else Login page.
The fix for this issue is very simple. When user logs out from the app, you need to clear SharedPreferences as well.
Like this,
SharedPreferenceUtil.storeStringValue(Login_Activity.this, Constants.USERNAME, null);
SharedPreferenceUtil.storeStringValue(Login_Activity.this, Constants.PASSWORD, null);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.