1
1
package {{ sdk .namespace | caseDot }}
2
2
3
+ import android.content.Context
3
4
import android.content.Intent
4
5
import android.net.Uri
5
- import androidx.activity.ComponentActivity
6
6
import androidx.browser.customtabs.CustomTabsIntent
7
7
import androidx.lifecycle.DefaultLifecycleObserver
8
8
import androidx.lifecycle.LifecycleOwner
@@ -17,7 +17,7 @@ import kotlin.collections.set
17
17
* Used to authenticate with external OAuth2 providers. Launches browser windows and handles
18
18
* suspension until the user completes the process or otherwise returns to the app.
19
19
*/
20
- actual class WebAuthComponent(private val activity: ComponentActivity ) {
20
+ actual class WebAuthComponent(private val context: Context ) {
21
21
22
22
companion object : DefaultLifecycleObserver {
23
23
private var suspended = false
@@ -27,59 +27,30 @@ actual class WebAuthComponent(private val activity: ComponentActivity) {
27
27
suspended = false
28
28
}
29
29
30
- /**
31
- * Authenticate Session with OAuth2
32
- *
33
- * Launches a chrome custom tab from the given activity and directs to the given url,
34
- * suspending until the user returns to the app, at which point the given [onComplete] callback
35
- * will run, passing the callback url from the intent used to launch the [CallbackActivity],
36
- * or an [IllegalStateException] in the case the user closed the window or returned to the
37
- * app without passing through the [CallbackActivity].
38
- *
39
- *
40
- * @param activity The activity to launch the browser from and observe the lifecycle of
41
- * @param url The url to launch
42
- * @param callbackUrlScheme The callback url scheme used to key the given callback
43
- * @param onComplete The callback to run when a result (success or failure) is received
44
- */
45
30
suspend fun authenticate(
46
- activity: ComponentActivity ,
31
+ context: Context ,
47
32
url: String,
48
33
callbackUrlScheme: String,
49
34
onComplete: ((Result<String >) -> Unit)?
50
35
) {
51
36
val intent = CustomTabsIntent.Builder().build()
52
- val keepAliveIntent = Intent(activity , KeepAliveService::class.java)
37
+ val keepAliveIntent = Intent(context , KeepAliveService::class.java)
53
38
54
39
callbacks[callbackUrlScheme] = onComplete
55
40
56
41
intent.intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
57
42
intent.intent.putExtra("android.support.customtabs.extra.KEEP_ALIVE", keepAliveIntent)
58
- intent.launchUrl(activity , Uri.parse(url))
43
+ intent.launchUrl(context , Uri.parse(url))
59
44
60
- activity.runOnUiThread {
61
- activity.lifecycle.addObserver(this)
62
- }
63
-
64
- // Need to dirty poll block so execution doesn't continue at the callsite of this function
45
+ // Dirty poll block so execution doesn't continue at the callsite of this function
65
46
suspended = true
66
47
while (suspended) {
67
48
delay(200)
68
49
}
69
50
cleanUp()
70
51
}
71
52
72
- /**
73
- * Trigger a web auth callback
74
- *
75
- * Attempts to find a callback for the given [scheme] and if found, invokes it, passing the
76
- * given [url]. Calling this method stops auth suspension, so any calls to [authenticate]
77
- * will continue execution from their suspension points immediately after this method
78
- * is called.
79
- *
80
- * @param scheme The scheme to match to a callback's key
81
- * @param url The url received through intent data from the [CallbackActivity]
82
- */
53
+
83
54
fun onCallback(scheme: String, url: String) {
84
55
callbacks.remove(scheme)?.invoke(
85
56
Result.success(url)
@@ -104,7 +75,7 @@ actual class WebAuthComponent(private val activity: ComponentActivity) {
104
75
onComplete: ((Result<String >) -> Unit)?
105
76
) {
106
77
authenticate(
107
- activity ,
78
+ context ,
108
79
url,
109
80
callbackUrlScheme,
110
81
onComplete,
0 commit comments