Skip to content

Commit e9e3b6f

Browse files
committed
Fix OAuth2 login handling
1 parent 876e363 commit e9e3b6f

File tree

8 files changed

+161
-108
lines changed

8 files changed

+161
-108
lines changed

composer.lock

Lines changed: 48 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/kmp/shared/build.gradle.kts.twig

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,30 @@ kotlin {
6464
}
6565
}
6666

67-
listOf(
68-
iosX64(),
69-
iosArm64(),
70-
iosSimulatorArm64()
71-
).forEach {
72-
it.binaries.framework {
67+
iosX64 {
68+
// Create a framework binary with additional options
69+
binaries.framework {
7370
baseName = "shared"
7471
isStatic = true
7572
binaryOption("bundleId", "io.appwrite.shared")
7673
}
77-
it.withSourcesJar(publish = false)
74+
withSourcesJar(publish = false)
75+
}
76+
iosArm64 {
77+
binaries.framework {
78+
baseName = "shared"
79+
isStatic = true
80+
binaryOption("bundleId", "io.appwrite.shared")
81+
}
82+
withSourcesJar(publish = false)
83+
}
84+
iosSimulatorArm64 {
85+
binaries.framework {
86+
baseName = "shared"
87+
isStatic = true
88+
binaryOption("bundleId", "io.appwrite.shared")
89+
}
90+
withSourcesJar(publish = false)
7891
}
7992

8093
sourceSets {
@@ -137,9 +150,11 @@ kotlin {
137150

138151
publishing {
139152
publications.withType<MavenPublication> {
140-
when (name) {
141-
"jvm" -> artifactId = "sdk-for-kmp-jvm"
142-
else -> artifactId = project.ext["PUBLISH_ARTIFACT_ID"].toString()
153+
artifactId = if (name != "kotlinMultiplatform") {
154+
// Append the target name to the artifactId so each publication is unique.
155+
"${project.ext["PUBLISH_ARTIFACT_ID"].toString()}-$name"
156+
} else {
157+
project.ext["PUBLISH_ARTIFACT_ID"].toString()
143158
}
144159

145160
pom {
@@ -211,4 +226,4 @@ dependencies {
211226
implementation(libs.androidx.browser)
212227
implementation(libs.androidx.espresso.core)
213228
implementation(libs.firebase.crashlytics.buildtools)
214-
}
229+
}

templates/kmp/shared/src/androidMain/kotlin/io/package/WebAuthComponent.android.kt.twig

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package {{ sdk.namespace | caseDot }}
22

3+
import android.content.Context
34
import android.content.Intent
45
import android.net.Uri
5-
import androidx.activity.ComponentActivity
66
import androidx.browser.customtabs.CustomTabsIntent
77
import androidx.lifecycle.DefaultLifecycleObserver
88
import androidx.lifecycle.LifecycleOwner
@@ -17,7 +17,7 @@ import kotlin.collections.set
1717
* Used to authenticate with external OAuth2 providers. Launches browser windows and handles
1818
* suspension until the user completes the process or otherwise returns to the app.
1919
*/
20-
actual class WebAuthComponent(private val activity: ComponentActivity) {
20+
actual class WebAuthComponent(private val context: Context) {
2121

2222
companion object : DefaultLifecycleObserver {
2323
private var suspended = false
@@ -27,59 +27,30 @@ actual class WebAuthComponent(private val activity: ComponentActivity) {
2727
suspended = false
2828
}
2929

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-
*/
4530
suspend fun authenticate(
46-
activity: ComponentActivity,
31+
context: Context,
4732
url: String,
4833
callbackUrlScheme: String,
4934
onComplete: ((Result<String>) -> Unit)?
5035
) {
5136
val intent = CustomTabsIntent.Builder().build()
52-
val keepAliveIntent = Intent(activity, KeepAliveService::class.java)
37+
val keepAliveIntent = Intent(context, KeepAliveService::class.java)
5338

5439
callbacks[callbackUrlScheme] = onComplete
5540

5641
intent.intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
5742
intent.intent.putExtra("android.support.customtabs.extra.KEEP_ALIVE", keepAliveIntent)
58-
intent.launchUrl(activity, Uri.parse(url))
43+
intent.launchUrl(context, Uri.parse(url))
5944

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
6546
suspended = true
6647
while (suspended) {
6748
delay(200)
6849
}
6950
cleanUp()
7051
}
7152

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+
8354
fun onCallback(scheme: String, url: String) {
8455
callbacks.remove(scheme)?.invoke(
8556
Result.success(url)
@@ -104,7 +75,7 @@ actual class WebAuthComponent(private val activity: ComponentActivity) {
10475
onComplete: ((Result<String>) -> Unit)?
10576
) {
10677
authenticate(
107-
activity,
78+
context,
10879
url,
10980
callbackUrlScheme,
11081
onComplete,

0 commit comments

Comments
 (0)