Skip to content

Commit

Permalink
Add protection against ready race-condition (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
itaihanski authored Dec 3, 2024
1 parent 4d3016d commit 84806b4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ written for Android. You can read more on the [Descope Website](https://descope.
Add the following to your `build.gradle` dependencies:

```groovy
implementation 'com.descope:descope-kotlin:0.12.1'
implementation 'com.descope:descope-kotlin:0.12.2'
```

## Quickstart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ internal class DescopeFlowCoordinator(private val webView: WebView) {
webView.settings.setSupportZoom(false)
webView.addJavascriptInterface(object {
@JavascriptInterface
fun onReady() {
fun onReady(tag: String) {
if (state != State.Started) {
logger?.log(Info, "Flow onReady called in state $state - ignoring")
return
}
state = State.Ready
logger?.log(Info, "Flow is ready")
logger?.log(Info, "Flow is ready ($tag)")
handler.post {
listener?.onReady()
}
Expand Down Expand Up @@ -276,7 +276,7 @@ private fun setupScript(
magicLinkRedirect: String,
isWebAuthnSupported: Boolean
) = """
function waitWebComponent() {
function flowBridgeWaitWebComponent() {
const styles = `
* {
user-select: none;
Expand All @@ -292,12 +292,21 @@ function waitWebComponent() {
wc = document.getElementsByTagName('descope-wc')[0]
if (wc) {
clearInterval(id)
prepareWebComponent(wc)
flowBridgePrepareWebComponent(wc)
}
}, 20)
}
function prepareWebComponent(wc) {
function flowBridgeIsReady(wc, tag) {
if (!wc.bridgeVersion) {
flow.onError('Hosted flow uses unsupported web-component SDK version');
return
}
wc.sdk.webauthn.helpers.isSupported = async () => $isWebAuthnSupported
flow.onReady(tag);
}
function flowBridgePrepareWebComponent(wc) {
wc.nativeOptions = {
bridgeVersion: 1,
platform: 'android',
Expand All @@ -308,14 +317,13 @@ function prepareWebComponent(wc) {
origin: '$origin',
}
wc.addEventListener('ready', () => {
if (!wc.bridgeVersion) {
flow.onError('Hosted flow uses unsupported web-component SDK version');
return
}
wc.sdk.webauthn.helpers.isSupported = async () => $isWebAuthnSupported
flow.onReady();
})
if (document.querySelector('descope-wc')?.shadowRoot?.querySelector('descope-container')) {
flowBridgeIsReady(wc, 'immediate')
} else {
wc.addEventListener('ready', () => {
flowBridgeIsReady(wc, 'listener')
})
}
wc.addEventListener('success', (e) => {
flow.onSuccess(JSON.stringify(e.detail), window.location.href);
Expand All @@ -330,7 +338,7 @@ function prepareWebComponent(wc) {
})
}
waitWebComponent();
flowBridgeWaitWebComponent();
""".trimIndent()

private fun String.escapeForBackticks() = replace("\\", "\\\\")
Expand Down
2 changes: 1 addition & 1 deletion descopesdk/src/main/java/com/descope/sdk/Sdk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ class DescopeSdk(context: Context, projectId: String, configure: DescopeConfig.(
const val name = "DescopeAndroid"

/** The Descope SDK version */
const val version = "0.12.1"
const val version = "0.12.2"
}
}

0 comments on commit 84806b4

Please sign in to comment.