android - AccountManager - authToken is set but peekAuthToken return null -


i'm trying add new account (after facebook login + server validation) in accountmanager. flow case this:

  1. user login facebook
  2. i got details after login done , validate them against data have on server
  3. if ok, server send auth_token (jwt token)
  4. having user's details , auth_token i'm creating account via accountmanager , once created, set authtoken it.
  5. on next login when user re-open app call getauthtoken first try cached authtoken calling peekauthtoken().

the problem

at point 5, peekauthtoken returns null shouldn't because set auttoken account.

code

 public static bundle handleuserlogin(context context, user user) {     sharedpreferences mprefs = context.getsharedpreferences(constants.prefs_name, context.mode_private);     accountmanager = accountmanager.get(context);     account account = new account(user.getemail(), account_type);     account[] accounts = am.getaccountsbytype(account_type);     boolean isnewaccount = true;       (int = 0; < accounts.length; i++) {         if (user.getemail().equalsignorecase(accounts[i].name) && account_type.equalsignorecase(accounts[i].type)) {             isnewaccount = false;             account = accounts[i];             break;         }     }      if (isnewaccount) {         am.addaccountexplicitly(account, user.getpassword(), null);         accounts = am.getaccountsbytype(account_type);          (int = 0; < accounts.length; i++) {             if (user.getemail().equalsignorecase(accounts[i].name) && account_type.equalsignorecase(accounts[i].type)) {                 account = accounts[i];                 break;             }         }     }      if (null != user.getpassword()) {         am.setpassword(account, user.getpassword());     }      cs.error(tag, "account " + account + " token " + user.gettoken());     am.setauthtoken(account, user.gettoken(), authenticator.authtoken_type_full_access);     setuserdata(user, account, am);      bundle result = new bundle();      result.putstring(accountmanager.key_account_name, account.name);     result.putstring(accountmanager.key_account_type, account.type);     result.putstring(accountmanager.key_authtoken, user.gettoken());     mprefs.edit().putstring(constants.key_current_user, account.name).commit();      return result; } 

first thought maybe reference new account not correct 1 (ex 1 accountmanager) search account again.

could give me indications i'm doing wrong or how should make sure authtoken set account?

thank you

i not rely on explicit setting authtoken in place different in abstractthreadedaccountauthenticator's getauthtoken() method. guess authtoken not cached yet when request it.

i suggest creating account via accountmanager.addaccountexplicitly() , deferring accessing authtoken once need it.

in abstractthreadedaccountauthenticator's getauthtoken() suggest applying logic in project created while ago.

  1. request authtoken via accountmanager.peekauthtoken()
  2. if authtoken set in cache return it.
  3. if not request server cache via accountmanager.setauthtoken() , return.

link: https://github.com/dawidgdanski/accountauthenticatorexample/blob/master/app/src/main/java/com/authenticator/account/auth/simpleauthenticator.java

hope out:

https://github.com/dawidgdanski/accountauthenticatorexample


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -