how to keep user sign in for app life?

情到浓时终转凉″ 提交于 2019-12-11 09:29:25

问题


i'm having problem to keep the user sign in for all my app life, the "sign in" is on the main activity, when start another activity i'm still loged in and i can upload data to quickblox, but after i start another activity and then start again the activy that i uploaded data to quickblox, i get error when try to upload data: "token is required"...

Edit:

 QBSettings.getInstance().fastConfigInit(String.valueOf(APP_ID), AUTH_KEY, AUTH_SECRET);
    QBUser user = new QBUser("login", "password");
    QBAuth.createSession(user, this, QBQueries.SIGN_IN);

回答1:


I think it's another issue

"token is required" means that you didn't create session and trying to perform other query

You have to properly create a session first

        QBAuth.createSession(new QBCallbackImpl() {
            @Override
            public void onComplete(Result result) {
                if (result.isSuccess()) {
                    // do other requests here 
                    //
                } else {
                    handleErrors(result);
                }
            }
        });

If it's not an issue for you - please provide more code in your question

UPD

1) Try to check token for null

try {
    String token = BaseService.getBaseService().getToken();
    if(token == null){
        // recreate session here
    }
} catch (BaseServiceException e) {
    e.printStackTrace();
}



回答2:


Im not sure what how to use this API you are using here but if you want an alternate suggestion, I have always used SharedPreferences to save user sessions.

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.putString("username", username); // Save the username with tag "username"
editor.putString("password", password); // Save the password with tag "password"
editor.commit();

And to get the user info back:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
u = settings.getString("username", null);
p = settings.getString("password", null);
if(u == null && p == null) {...} // No saved user session, have user sign in
else {...} // User already logged in, go to main screen 


来源:https://stackoverflow.com/questions/21048282/how-to-keep-user-sign-in-for-app-life

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!