I'm trying to add a new account (after a Facebook login + server validation) in AccountManager. The flow for this case is like this:
- User login with Facebook
- I got the details after the login is done and I validate them against the data I have on my server
- If everything is ok, the server send back an auth_token (JWT token)
- Having user's details and the auth_token I'm creating an account via AccountManager and once it is created, I set the authToken for it.
- On next login when the user will re-open the app I call getAuthToken which first try to get the cached authToken by calling peekAuthToken().
The problem
At point 5, peekAuthToken returns null but it shouldn't because I already set the autToken for that account.
Code
public static Bundle handleUserLogin(Context context, User user) {
SharedPreferences mPrefs = context.getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE);
AccountManager am = AccountManager.get(context);
Account account = new Account(user.getEmail(), ACCOUNT_TYPE);
Account[] accounts = am.getAccountsByType(ACCOUNT_TYPE);
boolean isNewAccount = true;
for (int i = 0; i < 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);
for (int i = 0; i < 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 I thought that maybe the reference to my new account is not the correct one (ex the one from AccountManager) so I search for account again.
Could you give me some indications about what I'm doing wrong or how should I make sure the authToken will be set for an account?
Thank you