-
Notifications
You must be signed in to change notification settings - Fork 20
Description
OTP Screen Error After Logout and Re-login with Different Account
Description
After logging out of one account and signing in with a different Google account, the OTP verification screen appears immediately (as intended), but shows an error: "No OTP session found and no auto-initiate flag" or "Grid sign-in did not create OTP session" before the Grid sign-in process has time to complete.
Type of Issue
- Bug
- Feature Request
- Documentation
- Question
Steps to Reproduce
- Sign in with Google account A
- Complete Grid wallet setup (OTP verification)
- Log out from account A
- Sign in with Google account B (different account)
- Expected: OTP screen appears with preloader, then OTP input after Grid sign-in completes
- Actual: OTP screen appears but shows error immediately before Grid sign-in can create the OTP session
Root Cause
A race condition occurs between:
AuthContextsetting the auto-initiate flag and redirecting to OTP screenGridContextstarting the Grid sign-in process (which has a 500ms delay)OTP Screenchecking for the OTP session before it's created
The OTP screen was checking for the session/flag too early and stopping polling prematurely, causing the error to appear before GridContext had time to:
- Process the auto-initiate flag (500ms delay)
- Call the Grid API to start sign-in
- Store the OTP session in secure storage
Error Messages Observed
❌ [OTP Screen] No OTP session found and no auto-initiate flag
or
❌ [OTP Screen] Grid sign-in did not create OTP session
Expected Behavior
- User signs in with Google → OTP screen appears immediately
- Preloader shows: "Setting up your wallet..." with spinner
- Grid sign-in starts in background (GridContext processes auto-initiate flag)
- OTP session is created and stored
- OTP input appears for user to enter code
- User enters OTP → Wallet loads → Navigate to chat screen
Actual Behavior
- User signs in with Google → OTP screen appears immediately
- Error message appears: "Session error. Please sign in again." or "Grid sign-in did not complete"
- User sees error before Grid sign-in has time to complete
- Grid sign-in may complete in background, but user already sees error
Technical Details
Files Involved
apps/client/app/(auth)/verify-otp.tsx- OTP verification screenapps/client/contexts/AuthContext.tsx- Sets auto-initiate flag after Google sign-inapps/client/contexts/GridContext.tsx- Handles Grid sign-in with 500ms delay
The Race Condition
// Timeline of events:
T+0ms: User completes Google sign-in
T+0ms: AuthContext sets GRID_AUTO_INITIATE flag
T+0ms: Navigation redirects to OTP screen
T+0ms: OTP screen checks for session → NOT FOUND
T+0ms: OTP screen checks for flag → FOUND
T+0ms: OTP screen starts polling
T+500ms: GridContext processes flag (setTimeout delay)
T+500ms: GridContext clears flag (immediately)
T+500ms: GridContext calls initiateGridSignIn()
T+1000ms: Grid API responds with OTP session
T+1000ms: GridContext stores OTP session
T+1000ms: OTP screen polling finds session ✅Problem: If OTP screen checks happen between T+500ms and T+1000ms, it sees:
- No session (not created yet)
- No flag (cleared by GridContext)
isSigningInToGridmight be false (not set yet)
This causes the early stop condition to trigger incorrectly.
Proposed Solution
- Increase minimum polling time: Wait at least 5 seconds before considering early stop
- Check authentication state: If user is authenticated, keep polling even if flag is cleared
- Longer timeout: Increase from 15s to 20s to handle slower network conditions
- Better indicator checking: Check multiple indicators (flag,
isSigningInToGrid, authenticated user) - Delayed polling start: If no indicators found initially, wait 2 seconds then start polling if indicators appear
Environment
- Platform: Android (emulator)
- Device: Android Emulator
- Flow: Google Sign-In → Grid Wallet Setup
Additional Context
This issue is related to the overall Grid wallet setup flow improvements where:
- OTP screen should appear immediately after Google sign-in
- Grid wallet should load in background during OTP flow
- User should navigate to chat after OTP completion
- Wallet should be ready when user clicks profile (no OTP again)
Related Issues/PRs
- Related to Grid wallet auto-loading improvements Error: Expo: Android Grid sign-in fails (crypto.getRandomValues / network) #89
- Part of unified authentication flow enhancements
PR to fix this issue is at #100
cc: @edgarpavlovsky