简体   繁体   English

面临有关Android中的改装登录的问题

[英]facing issue regarding retrofit login in android

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. 注意:我在应用程序中使用翻新版(带有Firebase)进行注册和登录。 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. 您正在使用SharedPreferences来存储登录凭据。 Once user enters valid login credentials (login ID and password) you store it in SharedPreferences in usersignin() method. 用户输入有效的登录凭据(登录ID和密码)后,就可以将其存储在usersignin()方法的SharedPreferences中。

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. 现在,我假设 (您只发布了LoginActivity而未提及其他部分)您的应用程序具有启动页面。 Which appears before Login page. 出现在登录页面之前。 In which you decide whether the user has already logged in or not (using SharedPreference values). 您可以在其中确定用户是否已经登录(使用SharedPreference值)。 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. 当用户从应用程序注销时,您还需要清除SharedPreferences。

Like this, 像这样,

SharedPreferenceUtil.storeStringValue(Login_Activity.this, Constants.USERNAME, null);
SharedPreferenceUtil.storeStringValue(Login_Activity.this, Constants.PASSWORD, null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM