Uh oh!
There was an error while loading. Please reload this page.
Add updateDeviceToken API and refresh flow for device tokens - #564
Conversation
Add a public Clerk.updateDeviceToken API that persists a new device token and forces a client/environment refresh after initialization. The refresh path now supports manual token-sync reloads without changing reinitialize semantics, and the client fetch can omit the stale in-memory client id header on a per-request basis to avoid mismatched anonymous client state. Also add regression tests for the new API and the request-scoped header behavior.
Break the device-token refresh path in ConfigurationManager into smaller validation, fetch, success, and failure helpers. This keeps the new post-initialization token sync behavior intact while satisfying the repo's Detekt thresholds for method size, complexity, and return count.
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughAdds device token update support: a public 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In
`@source/api/src/main/kotlin/com/clerk/api/configuration/ConfigurationManager.kt`:
- Line 486: Replace the force-unwrapping of client.id in handleSuccessfulRefresh
to avoid an NPE: don't use client.id!!—instead read val clientId = client.id ?:
run { ClerkLog.w("Received client with null ID, skipping post-refresh tasks");
return ClerkResult.success(Unit) } (or otherwise handle the null case and
abort/postpone post-refresh tasks) and then use clientId for subsequent
operations.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: e9d034da-49fb-450e-ae80-7617853e0aa4
📒 Files selected for processing (7)
source/api/src/main/kotlin/com/clerk/api/Clerk.ktsource/api/src/main/kotlin/com/clerk/api/configuration/ConfigurationManager.ktsource/api/src/main/kotlin/com/clerk/api/network/api/ClientApi.ktsource/api/src/main/kotlin/com/clerk/api/network/middleware/outgoing/VersioningUserAgentMiddleware.ktsource/api/src/main/kotlin/com/clerk/api/network/model/client/Client.ktsource/api/src/test/java/com/clerk/api/network/middleware/outgoing/VersioningUserAgentMiddlewareTest.ktsource/api/src/test/java/com/clerk/api/sdk/ClerkDeviceTokenUpdateTest.kt
| applicationContext = applicationContext, | ||
| cloudProjectNumber = options?.deviceAttestationOptions?.cloudProjectNumber, | ||
| applicationId = options?.deviceAttestationOptions?.applicationId, | ||
| clientId = client.id!!, |
There was a problem hiding this comment.
Potential NPE: Force-unwrapping nullable client.id
client.id is declared as String? in the Client data class. While a successful API response typically includes a client ID, force-unwrapping with !! can cause a crash if the server ever returns a null ID.
Proposed fix
- clientId = client.id!!,+ clientId = client.id ?: return,Or guard earlier in handleSuccessfulRefresh:
val clientId = client.id ?:run {
ClerkLog.w("Received client with null ID, skipping post-refresh tasks")
returnClerkResult.success(Unit)
}📝 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.
| clientId = client.id!!, | |
| clientId = client.id?:return, |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In
`@source/api/src/main/kotlin/com/clerk/api/configuration/ConfigurationManager.kt`
at line 486, Replace the force-unwrapping of client.id in
handleSuccessfulRefresh to avoid an NPE: don't use client.id!!—instead read val
clientId = client.id ?: run { ClerkLog.w("Received client with null ID, skipping
post-refresh tasks"); return ClerkResult.success(Unit) } (or otherwise handle
the null case and abort/postpone post-refresh tasks) and then use clientId for
subsequent operations.
chriscanin
left a comment
There was a problem hiding this comment.
This is working well, and is exactly what was needed!
Generate a fresh fictional Clerk test phone number for each auth integration run. This avoids CI failures when the shared test instance still has the previous hard-coded phone identifier attached to an account.
Uh oh!
There was an error while loading. Please reload this page.
Summary
Clerk.updateDeviceTokenas the supported way to write a new device token and reload client/environment state without reinitializingConfigurationManagerplus request-scoped client-id skipping so the refreshed client fetch does not reuse stale headersTesting
Summary by CodeRabbit