AccountManager - authToken is set, but peekAuthToken returns null

I am trying to add a new account (after logging in to Facebook + server validation) in the AccountManager. The flow for this case is as follows:

  • Facebook user login
  • I received the information after the login was completed, and I check them for the data that I have on my server.
  • If everything is in order, the server sends back auth_token (JWT token)
  • Availability of user information and auth_token I create an account through AccountManager, and after creating it, I set authToken for it.
  • At the next login, when the user opens the application again, I call getAuthToken, which will first try to get the cached authToken by calling peekAuthToken ().

Problem

At point 5, peekAuthToken returns null , but this should not be because I already set autotoken for this 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; } 

At first I thought that maybe the link to my new account is incorrect (for example, from the AccountManager), so I'm looking for an account again.

Could you give me some guidance on what I am doing wrong, or how should I make sure that authToken will be set for the account?

thanks

+6
source share
1 answer

I would not rely on the explicit definition of authToken in place, other than the AbstractThreadedAccountAuthenticator getAuthToken () method. I assume authToken is not cached yet when you request it.

I suggest simply creating an account through AccountManager.addAccountExplicitly () and defer access to authToken when you really need it.

In AbstractThreadedAccountAuthenticator getAuthToken (), I suggest applying logic, as in a project that I created some time ago.

  • AuthToken request through AccountManager.peekAuthToken ()
  • If authToken is set to cache, return it.
  • If not, then request it from your server, then cache through AccountManager.setAuthToken () and finally return.

Link: https://github.com/dawidgdanski/AccountAuthenticatorExample/blob/master/app/src/main/java/com/authenticator/account/auth/SimpleAuthenticator.java

Hope this helps you:

https://github.com/dawidgdanski/AccountAuthenticatorExample

0
source

Source: https://habr.com/ru/post/987021/


All Articles