Question

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);
Was it helpful?

Solution

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();
}

OTHER TIPS

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 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top