Account Kit
Account Kit lets people quickly register for and login to your app by using just their phone number or email address — no password needed. It’s reliable, easy to use and gives you a choice about how you sign up for apps.
To set up Account Kit in your Android app, if you don’t have a Facebook developer account create a Developer Account. Then Account Kit for Android requires a Facebook app ID. Follow the steps. Choose whether to allow email and SMS login, and choose security settings for your app.
Configure the SDK
Add your Facebook app ID and your Account Kit client token to the AndroidManifest.xml file. You’ll find the Account Kit client token in the Account Kit section of the App Dashboard. The application name will be used in the UI on the login screen.
Add the compile dependency with the latest version of the Account Kit SDK in the build.gradle file:
1 2 3 4 5 6 7 | repositories { jcenter() } dependencies { compile 'com.facebook.android:account-kit-sdk:4.+' } |
Add the following to the application tag of your AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 | <meta-data android:name="com.facebook.accountkit.ApplicationName" android:value="@string/app_name" /> <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/FACEBOOK_APP_ID" /> <meta-data android:name="com.facebook.accountkit.ClientToken" android:value="@string/ACCOUNT_KIT_CLIENT_TOKEN" /> <activity android:name="com.facebook.accountkit.ui.AccountKitActivity" android:theme="@style/AppLoginTheme" tools:replace="android:theme"/> |
Make sure @style/AppLoginTheme inherits from Theme.AccountKit.
Define the value for FACEBOOK_APP_ID as the Facebook app ID shown at the top of your application dashboard, and the value for ACCOUNT_KIT_CLIENT_TOKEN using the client token found in the Account Kit tab in the App Dashboard.
The AccountKitActivity must be defined here as well, to enable it to start in the app. Set the android:themeattribute here to customize the color scheme of the UI.
If you wish to disable App Events for your Account Kit application, add the following line toAndroidManifest.xml:
1 2 | <meta-data android:name="com.facebook.accountkit.FacebookAppEventsEnabled" android:value="false"/> |
Initiate a Login flow for SMS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import com.facebook.accountkit.AccountKit; public static int APP_REQUEST_CODE = 99; public void phoneLogin(final View view) { final Intent intent = new Intent(getActivity(), AccountKitActivity.class); AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder = new AccountKitConfiguration.AccountKitConfigurationBuilder( LoginType.PHONE, AccountKitActivity.ResponseType.CODE); // or .ResponseType.TOKEN // ... perform additional configuration ... intent.putExtra( AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION, configurationBuilder.build()); startActivityForResult(intent, APP_REQUEST_CODE); } |
The APP_REQUEST_CODE is your custom code to track your login flow. It can be any integer, but it needs to be set by your application.
When initializing your intent extras, be sure to specify the AccountKitActivity.
ResponseType that matches your application’s authorization setting in the Facebook developer portal dashboard: TOKEN if the Enable Client Access Token Flow switch in your app’s dashboard is ON, and CODE if it is OFF.
Initiate a Login flow for Email
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import com.facebook.accountkit.AccountKit; public static int APP_REQUEST_CODE = 99; public void emailLogin(final View view) { final Intent intent = new Intent(getActivity(), AccountKitActivity.class); AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder = new AccountKitConfiguration.AccountKitConfigurationBuilder( LoginType.EMAIL, AccountKitActivity.ResponseType.CODE); // or .ResponseType.TOKEN // ... perform additional configuration ... intent.putExtra( AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION, configurationBuilder.build()); startActivityForResult(intent, APP_REQUEST_CODE); } |
The APP_REQUEST_CODE is your custom code to track your login flow. It can be any integer, but it needs to be set by your application.
When initializing your intent extras, be sure to specify the AccountKitActivity.ResponseType that matches your application’s authorization setting in the Facebook developer portal dashboard: TOKEN if the Enable Client Access Token Flow switch in your app’s dashboard is ON, and CODE if it is OFF.
With Account Kit email login, people receive an email sent to their account. When they click on the link in the email on the same device that your app is installed on, they return to your app to finish the login activity.
To return people to your app, add the AccountKitEmailRedirectActivity activity with the following intent filter to your AndroidManifest.xml file:
1 2 3 4 5 6 7 8 | <activity android:name="com.facebook.accountkit.ui.AccountKitEmailRedirectActivity"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="@string/ak_login_protocol_scheme" /> </intent-filter> </activity> |
And the following in your strings.xml file:
// if your Facebook App ID is 1234567, you should use ak1234567
1 | <string name="ak_login_protocol_scheme">akFACEBOOK_APP_ID</string> |
Handle the Activity’s Result
Capture the Account Kit activity’s result and extract the AccountKitLoginResult from the Intent argument to determine the status of the login attempt.
If you began the login session with AccountKitActivity.ResponseType.TOKEN, a logout option is available to remove the stored AccessToken from the device.
1 | AccountKit.logOut(); |
Access Account Information on the Device
If your began the login session with AccountKitActivity.ResponseType.TOKEN, it’s possible to access the Account Kit ID, phone number and email of the current account via a call to getCurrentAccount().
Example Application : Dither