Skip to content

Commit 950ea2c

Browse files
committed
Change flow to optional
1 parent f2b3282 commit 950ea2c

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

descopesdk/src/main/java/com/descope/Descope.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ object Descope {
118118
// The underlying `DescopeSdk` object used by the `Descope` singleton.
119119
internal lateinit var sdk: DescopeSdk
120120

121-
// Initialization check
121+
// Used internally to check if the singleton object has an actual sdk value.
122122
internal val isInitialized
123123
get() = this::sdk.isInitialized
124124
}

descopesdk/src/main/java/com/descope/android/DescopeFlowCoordinator.kt

+15-14
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,12 @@ class DescopeFlowCoordinator(val webView: WebView) {
5757
internal var listener: DescopeFlowView.Listener? = null
5858
internal var state: DescopeFlowView.State = Initial
5959

60-
private lateinit var flow: DescopeFlow
60+
private var flow: DescopeFlow? = null
6161
private val handler: Handler = Handler(Looper.getMainLooper())
62-
private val globalSdk: DescopeSdk?
63-
get() = if (Descope.isInitialized) Descope.sdk else null
6462
private val sdk: DescopeSdk?
65-
get() = if (this::flow.isInitialized) flow.sdk ?: globalSdk else globalSdk
63+
get() = flow?.sdk ?: if (Descope.isInitialized) Descope.sdk else null
6664
private val logger: DescopeLogger?
67-
get() = sdk?.client?.config?.logger
65+
get() = sdk?.client.config.logger
6866
private var currentFlowUrl: Uri? = null
6967
private var alreadySetUp = false
7068

@@ -154,13 +152,13 @@ class DescopeFlowCoordinator(val webView: WebView) {
154152

155153
is NativePayload.OAuthWeb -> {
156154
logger?.log(Info, "Launching custom tab for web-based oauth")
157-
launchCustomTab(webView.context, nativePayload.startUrl, flow.presentation?.createCustomTabsIntent(webView.context))
155+
launchCustomTab(webView.context, nativePayload.startUrl, flow?.presentation?.createCustomTabsIntent(webView.context))
158156
return@launch
159157
}
160158

161159
is NativePayload.Sso -> {
162160
logger?.log(Info, "Launching custom tab for sso")
163-
launchCustomTab(webView.context, nativePayload.startUrl, flow.presentation?.createCustomTabsIntent(webView.context))
161+
launchCustomTab(webView.context, nativePayload.startUrl, flow?.presentation?.createCustomTabsIntent(webView.context))
164162
return@launch
165163
}
166164

@@ -232,7 +230,7 @@ class DescopeFlowCoordinator(val webView: WebView) {
232230
DoNothing -> true
233231
OpenBrowser -> {
234232
try {
235-
launchCustomTab(webView.context, uri, flow.presentation?.createCustomTabsIntent(webView.context))
233+
launchCustomTab(webView.context, uri, flow?.presentation?.createCustomTabsIntent(webView.context))
236234
} catch (e: DescopeException) {
237235
logger?.log(Error, "Failed to open URL in browser", e)
238236
}
@@ -260,10 +258,10 @@ class DescopeFlowCoordinator(val webView: WebView) {
260258
evaluateJavascript(
261259
setupScript(
262260
origin = origin,
263-
oauthNativeProvider = flow.oauthNativeProvider?.name ?: "",
264-
oauthRedirect = pickRedirectUrl(flow.oauthRedirect, flow.oauthRedirectCustomScheme, useCustomSchemeFallback),
265-
ssoRedirect = pickRedirectUrl(flow.ssoRedirect, flow.ssoRedirectCustomScheme, useCustomSchemeFallback),
266-
magicLinkRedirect = flow.magicLinkRedirect ?: "",
261+
oauthNativeProvider = flow?.oauthNativeProvider?.name ?: "",
262+
oauthRedirect = pickRedirectUrl(flow?.oauthRedirect, flow?.oauthRedirectCustomScheme, useCustomSchemeFallback),
263+
ssoRedirect = pickRedirectUrl(flow?.ssoRedirect, flow?.ssoRedirectCustomScheme, useCustomSchemeFallback),
264+
magicLinkRedirect = flow?.magicLinkRedirect ?: "",
267265
isWebAuthnSupported = isWebAuthnSupported,
268266
)
269267
) {}
@@ -333,7 +331,10 @@ document.head.appendChild(element)
333331
}
334332

335333
internal fun resumeFromDeepLink(deepLink: Uri) {
336-
if (!this::flow.isInitialized) throw DescopeException.flowFailed.with(desc = "`resumeFromDeepLink` cannot be called before `startFlow`")
334+
if (flow == null) {
335+
logger?.log(Error, "resumeFromDeepLink cannot be called before startFlow")
336+
return
337+
}
337338
activityHelper.closeCustomTab(webView.context)
338339
val response = JSONObject().apply { put("url", deepLink.toString()) }
339340
val type = if (deepLink.queryParameterNames.contains("t")) "magicLink" else "oauthWeb"
@@ -345,7 +346,7 @@ document.head.appendChild(element)
345346
private fun executeHooks(event: Event) {
346347
val hooks = mutableListOf<DescopeFlowHook>().apply {
347348
addAll(DescopeFlowHook.defaults)
348-
if (this@DescopeFlowCoordinator::flow.isInitialized) addAll(flow.hooks)
349+
flow?.let { addAll(it.hooks) }
349350
}
350351
hooks.filter { it.events.contains(event) }
351352
.forEach { it.execute(event, this) }

0 commit comments

Comments
 (0)