-
Notifications
You must be signed in to change notification settings - Fork 0
/
FacebookStandardAuth.kt
126 lines (115 loc) · 3.69 KB
/
FacebookStandardAuth.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.sun.auth.facebook.standard
import android.content.Context
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.facebook.AccessToken
import com.facebook.Profile
import com.facebook.login.widget.LoginButton
import com.sun.auth.core.callback.SignInCallback
import com.sun.auth.core.callback.SignOutCallback
import com.sun.auth.core.weak
@Suppress("Unused")
object FacebookStandardAuth {
private var context: Context? by weak(null)
private var config: FacebookConfig? = null
private var authClient: AuthClient? = null
/**
* Init the context and Facebook config.
* @param context The current context.
* @param config The FacebookConfig settings.
*/
@JvmStatic
internal fun initialize(context: Context, config: FacebookConfig) {
this.context = context.applicationContext
this.config = config
}
/**
* Init the Facebook authentication with callbacks. Call it before Fragment STARTED state.
* @param fragment The current [Fragment].
* @param signInCallback The sign in callback.
*/
@JvmStatic
fun initialize(fragment: Fragment, signInCallback: SignInCallback<AccessToken>? = null) {
check(fragment.activity != null || fragment.activity?.isFinishing != true) {
"The FragmentActivity this fragment is currently associated with is unavailable!"
}
check(config != null) {
"You must call initFacebookAuth first!"
}
authClient = AuthClient(
boundActivity = fragment.requireActivity(),
config = config!!,
signInCallback = signInCallback,
).apply {
fragment.lifecycle.addObserver(this)
}
}
/**
* Init the Facebook authentication with callbacks. Call it before Activity STARTED state.
* @param activity The current [FragmentActivity].
* @param signInCallback The sign in callback.
*/
@JvmStatic
fun initialize(
activity: FragmentActivity,
signInCallback: SignInCallback<AccessToken>? = null,
) {
check(!activity.isFinishing) {
"The FragmentActivity is currently unavailable!"
}
check(config != null) {
"You must call initFacebookAuth first!"
}
authClient = AuthClient(
boundActivity = activity,
config = config!!,
signInCallback = signInCallback,
).apply {
activity.lifecycle.addObserver(this)
}
}
/**
* Start Facebook SignIn process.
*
* If use Facebook LoginButton, no need to call this function.
*/
fun signIn() {
authClient?.signIn()
}
/**
* Sets the Facebook login button.
*
* Make sure you set permission for button or FacebookConfig while init.
*/
fun setLoginButton(button: LoginButton) {
authClient?.setLoginButton(button)
}
/**
* Check current SignIn status.
* @return true if Facebook account is signed in, otherwise false.
*/
fun isSignedIn(): Boolean {
return authClient?.isSignedIn() ?: false
}
/**
* Sign out the current account.
* @param signOutCallback: The sign out callback.
*/
fun signOut(signOutCallback: SignOutCallback? = null) {
authClient?.signOut(false, signOutCallback)
}
/**
* Gets the signed in account profile.
* @return Signed in Profile or null.
*/
fun getProfile(): Profile? {
return authClient?.getProfile()
}
/**
* Gets the facebook accessToken.
* @return Facebook accessToken or null.
*/
fun getAccessToken(): AccessToken? {
return authClient?.getAccessToken()
}
}