Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save guptasanchit90/f95cf4ca90ca8221d14bdfff044bece2 to your computer and use it in GitHub Desktop.
Save guptasanchit90/f95cf4ca90ca8221d14bdfff044bece2 to your computer and use it in GitHub Desktop.
Basic integration for Facebook Account kit SDK for Phone number Validation and login
Facebook Account Kit Android Basic integration
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mobyta.authhelper">
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true">
<meta-data
android:name="com.facebook.accountkit.ApplicationName"
android:value="@string/app_name" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/fb_app_id" />
<meta-data
android:name="com.facebook.accountkit.ClientToken"
android:value="@string/fb_ak_client_key" />
<activity android:name="com.facebook.accountkit.ui.AccountKitActivity">
<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/fb_ak_login_protocol_scheme" />
</intent-filter>
</activity>
</application>
</manifest>
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile group: 'com.facebook.android', name: 'account-kit-sdk', version: '4.17.0'
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="fb_app_id">FB_APP_ID</string>
<string name="fb_ak_client_key">FB_ACCOUNT_KIT_CLIENT_ID</string>
<string name="fb_ak_login_protocol_scheme">akFB_APP_ID</string>
</resources>
package com.mobyta.authhelper.FbAccountKit;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import com.facebook.accountkit.AccessToken;
import com.facebook.accountkit.AccountKit;
import com.facebook.accountkit.AccountKitLoginResult;
import com.facebook.accountkit.ui.AccountKitActivity;
import com.facebook.accountkit.ui.AccountKitConfiguration;
import com.facebook.accountkit.ui.LoginType;
public class FbAccountKitHelper {
private static int APP_REQUEST_CODE = 99;
private static FbAccountKitHelper _instance;
private FbAccountKitHelper(Context context) {
AccountKit.initialize(context);
}
public static FbAccountKitHelper getInstance(Context context) {
if (null == _instance) {
_instance = new FbAccountKitHelper(context);
}
return _instance;
}
/**
* Check if user is logged in
*
* @return
*/
public boolean validateSession() {
AccessToken accessToken = AccountKit.getCurrentAccessToken();
if (accessToken != null) {
return true;
}
return false;
}
/**
* Log user out
*/
public void inValidateSession() {
AccountKit.logOut();
}
/**
* Opens a Facebook account kit activity for login
*
* @param context
*/
public void onLoginPhone(Activity context) {
final Intent intent = new Intent(context, AccountKitActivity.class);
AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
new AccountKitConfiguration.AccountKitConfigurationBuilder(
LoginType.PHONE,
AccountKitActivity.ResponseType.TOKEN); // or .ResponseType.TOKEN
// ... perform additional configuration ...
intent.putExtra(
AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION,
configurationBuilder.build());
context.startActivityForResult(intent, APP_REQUEST_CODE);
}
/**
* Returns true if this is handled for Account kit callback
*
* @param reqCode
* @param data
* @return
*/
public boolean onResponse(int reqCode, Intent data, IFbAccountKitCallback iFbAccountKitCallback) {
if (reqCode == APP_REQUEST_CODE) {
AccountKitLoginResult loginResult = data.getParcelableExtra(AccountKitLoginResult.RESULT_KEY);
if (loginResult.getError() != null) {
iFbAccountKitCallback.onError();
} else if (loginResult.wasCancelled()) {
iFbAccountKitCallback.onCancel();
} else {
iFbAccountKitCallback.onSuccess();
}
return true;
} else {
return false;
}
}
}
package com.mobyta.authhelper.FbAccountKit;
public interface IFbAccountKitCallback {
void onCancel();
void onSuccess();
void onError();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment