One time login for multiple apps

When we login into a single google app, we are able to access all google apps with same account. We need not separately login into each and every google app.

For many applications, the answer is the AccountManager APIs. With the user’s permission, you can use Account Manager to fetch the account names that the user has stored on their device.

Integration with the user’s accounts allows you to do a variety of things such as:

  • Auto-fill forms with the user’s email address.
  • Retrieve an ID that is tied to a user, not the device.

Applications typically try to remember the user using one of three techniques:

  1. Ask the user to type in a username
  2. Retrieve a unique device ID to remember the device
  3. Retrieve a built-in account from AccountManager

Option (a) is problematic. First, asking the user to type something before entering your app will automatically make your app less appealing. Second, there’s no guarantee that the username chosen will be unique.

Option (b) is less difficult for the user, but it’s tricky to get right. More importantly, it only allows you to remember the user on one device. Imagine the frustration of someone who upgrades to a shiny new device, only to find that your app no longer remembers them.

Option (c) is the preferred technique. Account Manager allows you to get information about the accounts that are stored on the user’s device. Account Manager lets you remember your user, no matter how many devices the user may own, by adding just a couple of extra taps to your UI.


Decide what type of account to use

Android devices can store multiple accounts from many different providers. When you query AccountManager for account names, you can choose to filter by account type. The account type is a string that uniquely identifies the entity that issued the account. For instance, Google accounts have type “com.google,” while Twitter uses “com.twitter.android.auth.login.”


Steps:

1. In order to get a list of accounts on the device, your app needs the GET_ACCOUNTS permission. Add a <uses-permission> tag in your manifest file to request this permission:

<manifest ... >
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    ...
</manifest>

2. Once you decide what account type you’re interested in, you need to query for accounts of that type. Get an instance of AccountManager by calling AccountManager.get(). Then use that instance to call getAccountsByType().

AccountManager am = AccountManager.get(this); // "this" references the current Context

Account[] accounts = am.getAccountsByType("com.google");

This returns an array of Account objects. If there’s more than one Account in the array, you should present a dialog asking the user to select one.


Use the account object to personalize your app

The Account object contains an account name, which for Google accounts is an email address. You can use this information in several different ways, such as:

  • As suggestions in forms, so the user doesn’t need to input account information by hand.
  • As a key into your own online database of usage and personalization information.


References:
https://developer.android.com/training/id-auth/identify
https://developer.android.com/training/id-auth/authenticate
https://stackoverflow.com/questions/48662017/same-one-time-login-for-multiple-apps

One thought on “One time login for multiple apps

Leave a comment