Skip to content

Facebook Standard authentication

namnh-0652 edited this page Apr 3, 2023 · 10 revisions

Features

Installation

Prerequisites

Create Facebook Application

  • Go to Facebook developer site and create your application.
    • Save your App Id.
    • Go to Setting > Basic > save your App secret.
    • Go to Setting > Advanced > save your Client token.
  • Go to Dashboard > Add products to your app, select Facebook Login.
  • Go to Facebook Login > QuickStart > Add settings for your Android application.

Notes: For additional settings please read Facebook documents for detail. Depending on the Facebook data you request from people using Facebook Login , you may need to submit your app for review prior to launch.

Add library

From project build.gradle (or settings.gradle), add Jitpack maven

repositories {
    maven { url 'https://jitpack.io' }
}

Add these dependencies to your app/build.gradle

dependencies {
    implementation "com.github.sun-asterisk.tech-standard-android-auth:core:${latest_version}"
    implementation "com.github.sun-asterisk.tech-standard-android-auth:facebookauth:${latest_version}"
}

Usage

SignIn Facebook

Setup your FacebookConfig

From Application class, call initFacebookAuth method

initFacebookAuth(
     appId = getString(R.string.facebook_app_id),
     clientToken = getString(R.string.facebook_client_token),
) {
     readPermissions = listOf("email", "public_profile")
     enableAppEvent = false
     enableLoginStatus = true
}

Add Facebook activity to your Manifest

<activity android:name="com.facebook.FacebookActivity"
    android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation" />

<activity android:name="com.facebook.CustomTabActivity"
    android:exported="true">    <!--To support Web login-->
    <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_login_protocol_scheme" /> <!--fbAPP-ID Ex: fb111000111000111--> 
    </intent-filter>
</activity>

Initialize Activity/Fragment with callbacks you want to get authentication result

From your FragmentActivity or Fragment

fun initFacebookSignIn(activity: FragmentActivity) {
    FacebookStandardAuth.initialize(
        activity,
        signInCallback = object : SignInCallback<AccessToken> {
             override fun onResult(data: AccessToken?, error: Throwable?) {
                 _signInState.value = SocialAuthResult(data = data, error = error)
             }
        }
    )
}

Important Note:

  • Because this client is designed follow LifecycleObserver, so call initialize method before Activity or Fragment falls in STARTED state.

Finally, call signIn Facebook

fun signIn() {
    FacebookStandardAuth.signIn()
}

SignIn via Facebook LoginButton

Sets LoginButton permissions

// via FacebookConfig
initFacebookAuth(...) {
    useFacebookLoginButton = true // required
    readPermissions = listOf("email", "public_profile")
}
// or directly
binding.facebookSignIn.permissions = listOf("email", "public_profile")

Sets LoginButton

FacebookStandardAuth.setLoginButton(binding.facebookSignIn)

Note: SignIn via Facebook LoginButton, you do not need to call SignIn() method.

Sequence diagram

sequenceDiagram
    autonumber
    participant Application
    participant FacebookStandardAuth
    note over FacebookStandardAuth: FacebookAuth Module
    participant Facebook
    Application->>FacebookStandardAuth: check SignIn status
    FacebookStandardAuth->>Facebook: check SignIn status
    Facebook-->>FacebookStandardAuth: response status
    alt is Signed In
        rect rgb(0, 0, 255, .1)
            break when is SignedIn
                FacebookStandardAuth-->>Application: 
            end
        end
    else is Not Signed In
        FacebookStandardAuth-->>Application: 
    end
    Application->>FacebookStandardAuth: send SignIn() request
    FacebookStandardAuth->>Facebook: request SignIn
    Facebook->>Facebook: Handle Request
    alt signIn process fails
        rect rgb(0, 0, 255, .1)
            break
                Facebook-->>FacebookStandardAuth: return error
                FacebookStandardAuth-->>Application: Callback error
            end
        end
    else signIn Success
        Facebook-->>FacebookStandardAuth: return AccessToken
        FacebookStandardAuth-->>Application: Callback AccessToken
    end
Loading

Check signed in status

fun isSignedIn(): Boolean {
    return FacebookStandardAuth.isSignedIn()
}

Gets Facebook Token or Profile

fun getFacebookToken(): AccessToken? {
    return FacebookStandardAuth.getAccessToken()
}

fun getFacebookProfile(): Profile? {
    return FacebookStandardAuth.getProfile()
}

SignOut current user

fun signOut() {
    FacebookStandardAuth.signOut(object : SignOutCallback {
        override fun onResult(error: Throwable?) {
            // error = null means success
            _signOutState.value = error
        }
    })
}

Documentation

Method/field Purpose
appId: String Facebook application id
clientToken: String Facebook client token
readPermissions: List List of read permissions
enableLoginStatus: Boolean Enable to check login status, default true
enableAppEvent: Boolean Sets auto logging events flag, default true
useFacebookLoginButton: Boolean Sets flag use Facebook LoginButton, default false
Method Purpose
fun signIn(): Unit Start SignIn process
fun setLoginButton(button: LoginButton): Unit Sets Facebook LoginButton
fun isSignedIn(): Boolean Check current SignIn status
fun signOut(): Unit SignOut current User
fun getProfile(): Profile? Gets current Facebook Profile
fun getAccessToken(): AccessToken? Gets current Facebook AccessToken