Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/native-session-sync-v2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/expo": patch
---

Re-introduce two-way JS/native session sync for expo native components
2 changes: 1 addition & 1 deletion packages/expo/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ext {
credentialsVersion = "1.3.0"
googleIdVersion = "1.1.1"
kotlinxCoroutinesVersion = "1.7.3"
clerkAndroidApiVersion = "1.0.6"
clerkAndroidApiVersion = "1.0.9"
clerkAndroidUiVersion = "1.0.9"
composeVersion = "1.7.0"
activityComposeVersion = "1.9.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent
import android.util.Log
import com.clerk.api.Clerk
import com.clerk.api.network.serialization.ClerkResult
import com.facebook.react.bridge.ActivityEventListener
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
Expand Down Expand Up @@ -67,41 +68,63 @@ class ClerkExpoModule(reactContext: ReactApplicationContext) :
try {
publishableKey = pubKey

// If the JS SDK has a bearer token, write it to the native SDK's
// SharedPreferences so both SDKs share the same Clerk API client.
if (!bearerToken.isNullOrEmpty()) {
reactApplicationContext.getSharedPreferences("clerk_preferences", Context.MODE_PRIVATE)
.edit()
.putString("DEVICE_TOKEN", bearerToken)
.apply()
debugLog(TAG, "configure - wrote JS bearer token to native SharedPreferences")
}

Clerk.initialize(reactApplicationContext, pubKey)
if (!Clerk.isInitialized.value) {
// First-time initialization — write the bearer token to SharedPreferences
// before initializing so the SDK boots with the correct client.
if (!bearerToken.isNullOrEmpty()) {
reactApplicationContext.getSharedPreferences("clerk_preferences", Context.MODE_PRIVATE)
.edit()
.putString("DEVICE_TOKEN", bearerToken)
.apply()
}

// Wait for initialization to complete with timeout
try {
withTimeout(10_000L) {
Clerk.isInitialized.first { it }
Clerk.initialize(reactApplicationContext, pubKey)

// Wait for initialization to complete with timeout
try {
withTimeout(10_000L) {
Clerk.isInitialized.first { it }
}
} catch (e: TimeoutCancellationException) {
val initError = Clerk.initializationError.value
val message = if (initError != null) {
"Clerk initialization timed out: ${initError.message}"
} else {
"Clerk initialization timed out after 10 seconds"
}
promise.reject("E_TIMEOUT", message)
return@launch
}
} catch (e: TimeoutCancellationException) {
val initError = Clerk.initializationError.value
val message = if (initError != null) {
"Clerk initialization timed out: ${initError.message}"

// Check for initialization errors
val error = Clerk.initializationError.value
if (error != null) {
promise.reject("E_INIT_FAILED", "Failed to initialize Clerk SDK: ${error.message}")
} else {
"Clerk initialization timed out after 10 seconds"
promise.resolve(null)
}
promise.reject("E_TIMEOUT", message)
return@launch
Comment on lines +71 to 106
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Wait for the first token-backed session before resolving configure().

This cold-start branch resolves as soon as Clerk.isInitialized flips. Both packages/expo/src/hooks/useUserProfileModal.ts and packages/expo/src/native/UserButton.tsx immediately call getSession() after configure(), so a first launch with a JS bearer token can still observe null and treat native as unsigned-in. That breaks the sync/sign-out reconciliation this PR is reintroducing.

Suggested fix
                     try {
                         withTimeout(10_000L) {
                             Clerk.isInitialized.first { it }
                         }
+                        if (!bearerToken.isNullOrEmpty()) {
+                            withTimeout(5_000L) {
+                                Clerk.sessionFlow.first { it != null }
+                            }
+                        }
                     } catch (e: TimeoutCancellationException) {

}

// Check for initialization errors
val error = Clerk.initializationError.value
if (error != null) {
promise.reject("E_INIT_FAILED", "Failed to initialize Clerk SDK: ${error.message}")
} else {
promise.resolve(null)
// Already initialized — use the public SDK API to update
// the device token and trigger a client/environment refresh.
if (!bearerToken.isNullOrEmpty()) {
val result = Clerk.updateDeviceToken(bearerToken)
if (result is ClerkResult.Failure) {
debugLog(TAG, "configure - updateDeviceToken failed: ${result.error}")
}

// Wait for session to appear with the new token (up to 5s)
try {
withTimeout(5_000L) {
Clerk.sessionFlow.first { it != null }
}
} catch (_: TimeoutCancellationException) {
debugLog(TAG, "configure - session did not appear after token update")
}
}

promise.resolve(null)
} catch (e: Exception) {
promise.reject("E_INIT_FAILED", "Failed to initialize Clerk SDK: ${e.message}", e)
}
Expand Down Expand Up @@ -174,15 +197,15 @@ class ClerkExpoModule(reactContext: ReactApplicationContext) :
@ReactMethod
override fun getSession(promise: Promise) {
if (!Clerk.isInitialized.value) {
promise.reject("E_NOT_INITIALIZED", "Clerk SDK is not initialized. Call configure() first.")
// Return null when not initialized (matches iOS behavior)
// so callers can proceed to call configure() with a bearer token.
promise.resolve(null)
return
}

val session = Clerk.session
val user = Clerk.user

debugLog(TAG, "getSession - hasSession: ${session != null}, hasUser: ${user != null}")

val result = WritableNativeMap()

session?.let {
Expand Down Expand Up @@ -217,7 +240,6 @@ class ClerkExpoModule(reactContext: ReactApplicationContext) :
try {
val prefs = reactApplicationContext.getSharedPreferences("clerk_preferences", Context.MODE_PRIVATE)
val deviceToken = prefs.getString("DEVICE_TOKEN", null)
debugLog(TAG, "getClientToken - deviceToken: ${if (deviceToken != null) "found" else "null"}")
promise.resolve(deviceToken)
} catch (e: Exception) {
debugLog(TAG, "getClientToken failed: ${e.message}")
Expand All @@ -230,7 +252,8 @@ class ClerkExpoModule(reactContext: ReactApplicationContext) :
@ReactMethod
override fun signOut(promise: Promise) {
if (!Clerk.isInitialized.value) {
promise.reject("E_NOT_INITIALIZED", "Clerk SDK is not initialized. Call configure() first.")
// Resolve gracefully when not initialized (matches iOS behavior)
promise.resolve(null)
return
Comment on lines 253 to 257
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Clear DEVICE_TOKEN even when native was never initialized.

ClerkProvider calls ClerkExpo.signOut() on JS-side sign-out. In this branch we resolve without removing the SharedPreferences token that configure() wrote, so the next native init can still boot back into the old client/session.

Suggested fix
   override fun signOut(promise: Promise) {
+        val prefs = reactApplicationContext.getSharedPreferences("clerk_preferences", Context.MODE_PRIVATE)
         if (!Clerk.isInitialized.value) {
-            // Resolve gracefully when not initialized (matches iOS behavior)
+            prefs.edit().remove("DEVICE_TOKEN").apply()
             promise.resolve(null)
             return
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
override fun signOut(promise: Promise) {
if (!Clerk.isInitialized.value) {
promise.reject("E_NOT_INITIALIZED", "Clerk SDK is not initialized. Call configure() first.")
// Resolve gracefully when not initialized (matches iOS behavior)
promise.resolve(null)
return
override fun signOut(promise: Promise) {
val prefs = reactApplicationContext.getSharedPreferences("clerk_preferences", Context.MODE_PRIVATE)
if (!Clerk.isInitialized.value) {
prefs.edit().remove("DEVICE_TOKEN").apply()
promise.resolve(null)
return
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/expo/android/src/main/java/expo/modules/clerk/ClerkExpoModule.kt`
around lines 253 - 257, The signOut(promise: Promise) branch that returns when
!Clerk.isInitialized currently resolves without clearing the stored device
token; update signOut to always remove the stored DEVICE_TOKEN from
SharedPreferences (the same key written by configure()) before resolving, even
when Clerk.isInitialized is false. Locate the signOut method in
ClerkExpoModule.kt and add logic to obtain the module's SharedPreferences and
remove the "DEVICE_TOKEN" entry (or the constant used for that key), then
proceed to promise.resolve(null).

}

Expand Down Expand Up @@ -258,17 +281,13 @@ class ClerkExpoModule(reactContext: ReactApplicationContext) :
}

private fun handleAuthResult(resultCode: Int, data: Intent?) {
debugLog(TAG, "handleAuthResult - resultCode: $resultCode")

val promise = pendingAuthPromise ?: return
pendingAuthPromise = null

if (resultCode == Activity.RESULT_OK) {
val session = Clerk.session
val user = Clerk.user

debugLog(TAG, "handleAuthResult - hasSession: ${session != null}, hasUser: ${user != null}")

val result = WritableNativeMap()

// Top-level sessionId for JS SDK compatibility (matches iOS response format)
Expand Down Expand Up @@ -296,7 +315,6 @@ class ClerkExpoModule(reactContext: ReactApplicationContext) :

promise.resolve(result)
} else {
debugLog(TAG, "handleAuthResult - user cancelled")
val result = WritableNativeMap()
result.putBoolean("cancelled", true)
promise.resolve(result)
Expand Down
53 changes: 30 additions & 23 deletions packages/expo/ios/ClerkExpoModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public protocol ClerkViewFactoryProtocol {
// SDK operations
func configure(publishableKey: String, bearerToken: String?) async throws
func getSession() async -> [String: Any]?
func getClientToken() -> String?
func signOut() async throws
Comment on lines 23 to 26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Protocol requirements:"
sed -n '13,27p' packages/expo/ios/ClerkExpoModule.swift

echo
echo "Runtime factory methods:"
rg -n 'func (getClientToken|getSession|signOut)\b' packages/expo/ios/ClerkViewFactory.swift

echo
echo "Template factory methods:"
rg -n 'func (getClientToken|getSession|signOut)\b' packages/expo/ios/templates/ClerkViewFactory.swift

Repository: clerk/javascript

Length of output: 1287


The injected template is missing a required protocol method.

ClerkViewFactoryProtocol requires getClientToken(), but packages/expo/ios/templates/ClerkViewFactory.swift does not implement it. Any app built with this template will fail Swift protocol conformance at compile time.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/expo/ios/ClerkExpoModule.swift` around lines 23 - 26, The template
is missing the required protocol method getClientToken() which breaks
conformance to ClerkViewFactoryProtocol; add a concrete implementation of
getClientToken() inside the ClerkViewFactory class (the type that implements
ClerkViewFactoryProtocol) that returns the correct client token string or nil as
appropriate, ensuring the method signature exactly matches getClientToken() ->
String? and is public/internal consistent with other protocol methods; update
any backing storage or token retrieval logic used by configure/getSession to
return the same token value.

}

Expand All @@ -31,9 +32,11 @@ public protocol ClerkViewFactoryProtocol {
class ClerkExpoModule: RCTEventEmitter {

private static var _hasListeners = false
private static weak var sharedInstance: ClerkExpoModule?

override init() {
super.init()
ClerkExpoModule.sharedInstance = self
}

@objc override static func requiresMainQueueSetup() -> Bool {
Expand All @@ -52,6 +55,17 @@ class ClerkExpoModule: RCTEventEmitter {
ClerkExpoModule._hasListeners = false
}

/// Emits an onAuthStateChange event to JS from anywhere in the native layer.
/// Used by inline views (AuthView, UserProfileView) to notify ClerkProvider
/// of auth state changes in addition to the view-level onAuthEvent callback.
static func emitAuthStateChange(type: String, sessionId: String?) {
guard _hasListeners, let instance = sharedInstance else { return }
instance.sendEvent(withName: "onAuthStateChange", body: [
"type": type,
"sessionId": sessionId as Any,
])
}

/// Returns the topmost presented view controller, avoiding deprecated `keyWindow`.
private static func topViewController() -> UIViewController? {
guard let scene = UIApplication.shared.connectedScenes
Expand Down Expand Up @@ -174,31 +188,12 @@ class ClerkExpoModule: RCTEventEmitter {

@objc func getClientToken(_ resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock) {
// Use a custom keychain service if configured in Info.plist (for extension apps
// sharing a keychain group). Falls back to the main bundle identifier.
let keychainService: String = {
if let custom = Bundle.main.object(forInfoDictionaryKey: "ClerkKeychainService") as? String, !custom.isEmpty {
return custom
}
return Bundle.main.bundleIdentifier ?? ""
}()

let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: keychainService,
kSecAttrAccount as String: "clerkDeviceToken",
kSecReturnData as String: true,
kSecMatchLimit as String: kSecMatchLimitOne
]

var result: AnyObject?
let status = SecItemCopyMatching(query as CFDictionary, &result)

if status == errSecSuccess, let data = result as? Data {
resolve(String(data: data, encoding: .utf8))
} else {
guard let factory = clerkViewFactory else {
resolve(nil)
return
}

resolve(factory.getClientToken())
}

// MARK: - signOut
Expand Down Expand Up @@ -277,6 +272,12 @@ public class ClerkAuthNativeView: UIView {
let jsonData = (try? JSONSerialization.data(withJSONObject: data)) ?? Data()
let jsonString = String(data: jsonData, encoding: .utf8) ?? "{}"
self?.onAuthEvent?(["type": eventName, "data": jsonString])

// Also emit module-level event so ClerkProvider's useNativeAuthEvents picks it up
if eventName == "signInCompleted" || eventName == "signUpCompleted" {
let sessionId = data["sessionId"] as? String
ClerkExpoModule.emitAuthStateChange(type: "signedIn", sessionId: sessionId)
}
}
) else { return }

Expand Down Expand Up @@ -359,6 +360,12 @@ public class ClerkUserProfileNativeView: UIView {
let jsonData = (try? JSONSerialization.data(withJSONObject: data)) ?? Data()
let jsonString = String(data: jsonData, encoding: .utf8) ?? "{}"
self?.onProfileEvent?(["type": eventName, "data": jsonString])

// Also emit module-level event for sign-out detection
if eventName == "signedOut" {
let sessionId = data["sessionId"] as? String
ClerkExpoModule.emitAuthStateChange(type: "signedOut", sessionId: sessionId)
}
}
) else { return }

Expand Down
Loading
Loading