-
Notifications
You must be signed in to change notification settings - Fork 0
Facebook Standard authentication
namnh-0652 edited this page Apr 3, 2023
·
10 revisions
- SignIn Facebook
- SingIn via Facebook LoginButton
- Check signed in status
- Gets Facebook Token or Profile
- SignOut current user
- Android 23+
- Before you start using Facebook Authentication with this helper, you must Create Facebook Application
- A Facebook application from Facebook developer site
- 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.
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}"
}
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
}
<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>
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 callinitialize
method beforeActivity or Fragment
falls inSTARTED
state.
fun signIn() {
FacebookStandardAuth.signIn()
}
// via FacebookConfig
initFacebookAuth(...) {
useFacebookLoginButton = true // required
readPermissions = listOf("email", "public_profile")
}
// or directly
binding.facebookSignIn.permissions = listOf("email", "public_profile")
FacebookStandardAuth.setLoginButton(binding.facebookSignIn)
Note: SignIn via Facebook LoginButton, you do not need to call SignIn()
method.
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
fun isSignedIn(): Boolean {
return FacebookStandardAuth.isSignedIn()
}
fun getFacebookToken(): AccessToken? {
return FacebookStandardAuth.getAccessToken()
}
fun getFacebookProfile(): Profile? {
return FacebookStandardAuth.getProfile()
}
fun signOut() {
FacebookStandardAuth.signOut(object : SignOutCallback {
override fun onResult(error: Throwable?) {
// error = null means success
_signOutState.value = error
}
})
}
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 |