Skip to content

fix(expo): prevent session loss on Expo JS reload#8437

Open
souyahia wants to merge 1 commit intoclerk:mainfrom
souyahia:fix/expo-native-session-sync-reload
Open

fix(expo): prevent session loss on Expo JS reload#8437
souyahia wants to merge 1 commit intoclerk:mainfrom
souyahia:fix/expo-native-session-sync-reload

Conversation

@souyahia
Copy link
Copy Markdown

Summary

Fixes a bug where pressing R (JS reload) in Expo during development causes the user's session to be lost, forcing them to log in again. Killing and reopening the app preserves the session correctly.

Root cause

NativeSessionSync calls ClerkExpo.signOut() whenever isSignedIn is falsy, without checking whether Clerk has finished loading:

const { isSignedIn } = useAuth();

useEffect(() => {
  if (!isSignedIn) {           // true when isSignedIn is `undefined` (loading)
    ClerkExpo?.signOut();       // revokes the session!
  }
}, [isSignedIn, ...]);

On a JS reload (pressing R in Expo):

  1. All JS module state is reset, but native modules persist
  2. NativeSessionSync mounts, useAuth() returns isSignedIn: undefined (loading)
  3. !undefined === true, so it enters the signed-out branch
  4. ClerkExpo.signOut() is called on the native module, which is still configured from the previous session
  5. The native signOut() calls Clerk.shared.auth.signOut(sessionId:) (revoking the session server-side) and Clerk.clearAllKeychainItems() (deleting the JWT from the keychain)
  6. When Clerk JS finishes loading, the token is gone and the session is invalid

On a full app restart (kill + reopen), the native module's factory is uninitialized (the process died), so signOut() rejects with E_NOT_INITIALIZED and the session is preserved correctly.

Fix

Added an isLoaded guard so ClerkExpo.signOut() is only called when Clerk has fully loaded and confirmed the user is actually signed out, not during the initial loading phase.

const { isSignedIn, isLoaded } = useAuth();

useEffect(() => {
  if (!isSignedIn) {
    if (isLoaded) {             // only sign out when we're sure
      ClerkExpo?.signOut();
    }
  }
}, [isSignedIn, isLoaded, ...]);

NativeSessionSync was calling native signOut() during the loading phase
when isSignedIn is undefined (!undefined === true). On a JS reload
(pressing R in Expo), the native module persists from the previous
session, so signOut() goes through and revokes the session server-side
via Clerk.shared.auth.signOut() and clears all keychain items via
Clerk.clearAllKeychainItems(), forcing the user to log in again.

On a full app restart (kill + reopen), the native module factory is
uninitialized (process died), so signOut() rejects with
E_NOT_INITIALIZED and the session is preserved.

This adds an isLoaded guard so native signOut() is only called when
Clerk has fully loaded and confirmed the user is actually signed out,
not during the initial loading phase.
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 30, 2026

@souyahia is attempting to deploy a commit to the Clerk Production Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 30, 2026

🦋 Changeset detected

Latest commit: adda444

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@clerk/expo Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot added the expo label Apr 30, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 30, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 558b1c8e-7ad1-47d7-a8b5-ccf7a83ded2f

📥 Commits

Reviewing files that changed from the base of the PR and between 7680859 and adda444.

📒 Files selected for processing (2)
  • .changeset/fix-native-session-sync-reload.md
  • packages/expo/src/provider/ClerkProvider.tsx

📝 Walkthrough

Walkthrough

This PR introduces a changeset and updates the ClerkProvider component in the Expo package to fix premature sign-out calls during JavaScript reloads. The NativeSessionSync now reads the isLoaded flag from useAuth and only executes native signOut() after Clerk has completed loading its auth state, preventing sign-out invocations during initial renders where isSignedIn may still be undefined.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: fixing session loss on Expo JS reload, which directly corresponds to the primary objective of the changeset.
Description check ✅ Passed The description is comprehensive and directly related to the changeset, explaining the root cause, reproduction steps, and the implemented fix in detail.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ 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.

❤️ Share
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants