Skip to content

fix: redirect logged-in users to home screen on app launch#180

Open
hasanravda wants to merge 4 commits intoAOSSIE-Org:mainfrom
hasanravda:fix/splash-screen-session-check
Open

fix: redirect logged-in users to home screen on app launch#180
hasanravda wants to merge 4 commits intoAOSSIE-Org:mainfrom
hasanravda:fix/splash-screen-session-check

Conversation

@hasanravda
Copy link
Copy Markdown
Member

@hasanravda hasanravda commented Mar 6, 2026

Closes #179

📝 Description

Every time the Patient app was launched, users were shown the Login screen - even if they had already signed in. This happened because SplashScreen always navigated to AuthScreen unconditionally after the 3-second splash delay, without ever checking whether a valid Supabase session already existed.

This fix makes the splash screen session-aware. On launch, it now checks for an active session and routes the user directly to the right screen based on their onboarding progress - skipping the login flow entirely for returning users.

🔧 Changes Made

patient/lib/presentation/splash_screen.dart

  • Check Supabase.instance.client.auth.currentSession after the splash delay
  • If a valid session exists → call checkIfPatientExists() and route directly to the correct screen (HomeScreen, PersonalDetailsScreen, AssessmentsListScreen, or ConsultationRequestScreen)
  • If status.isError → show error snackbar before falling back to AuthScreen
  • If no session → navigate to AuthScreen as before (no change for logged-out users)

📷 Screenshots or Visual Changes (if applicable)

NeuroTrack.Splash.Auth.issue.mp4

✅ Checklist

  • I have read the contributing guidelines.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added necessary documentation (if applicable).
  • Any dependent changes have been merged and published in downstream modules.

Summary by CodeRabbit

  • New Features
    • Conditional app startup now directs users to the appropriate first screen (home with name, personal details, assessments list, or consultation request) based on their active session and onboarding progress.
  • Bug Fixes
    • Improved startup navigation to correctly route authenticated and unauthenticated users.
    • Added runtime safety checks and user-facing error notifications for initialization failures.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 6, 2026

Warning

Rate limit exceeded

@hasanravda has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 6 minutes and 18 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7b6db4e0-6369-47f1-a59a-d83104bd1c32

📥 Commits

Reviewing files that changed from the base of the PR and between 5ad761f and a55f114.

📒 Files selected for processing (1)
  • patient/lib/presentation/splash_screen.dart
📝 Walkthrough

Walkthrough

Splash screen navigation was changed to check Supabase for a current session, query onboarding status via AuthProvider, and then navigate to the correct next screen (Home, PersonalDetails, AssessmentsList, ConsultationRequest) or fallback to AuthScreen with error feedback.

Changes

Cohort / File(s) Summary
Splash Screen Session & Routing Logic
patient/lib/presentation/splash_screen.dart
Replaced unconditional navigation with _navigateToNextScreen() that checks Supabase.instance.client.auth.currentSession, queries onboarding status from AuthProvider, resets navigation status, chooses destination (HomeScreen, PersonalDetailsScreen, AssessmentsListScreen, ConsultationRequestScreen) or falls back to AuthScreen, uses SnackbarService for errors, and guards with mounted checks. Expanded imports accordingly.

Sequence Diagram(s)

sequenceDiagram
  participant Splash as SplashScreen
  participant Supa as Supabase (auth)
  participant AuthP as AuthProvider
  participant Nav as Navigator
  participant Snack as SnackbarService
  Splash->>Supa: check currentSession
  alt session exists
    Splash->>AuthP: fetchOnboardingStatus(userId)
    AuthP-->>Splash: onboardingStatus (home/personal/assess/consult) or error
    alt status resolved
      Splash->>AuthP: resetNavigationStatus()
      Splash->>Nav: pushReplacement(selected Screen)
    else error/unknown
      Splash->>Snack: show error message
      Splash->>Nav: pushReplacement(AuthScreen)
    end
  else no session
    Splash->>Nav: pushReplacement(AuthScreen)
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A hop from splash to session true,
I sniff the token, know what to do.
If onboarding waits, I'll guide the way,
Else to home I bound—no login delay.
Hooray for routes that remember you!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: redirecting logged-in users to the home screen on app launch, which is the core fix implemented in the splash screen.
Linked Issues check ✅ Passed The implementation fully addresses issue #179: it checks for existing Supabase sessions, routes authenticated users to appropriate screens based on onboarding status, and falls back to AuthScreen when needed.
Out of Scope Changes check ✅ Passed All changes are scoped to the splash screen session-awareness requirement; no unrelated modifications to other files or features are included.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

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

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@patient/lib/presentation/splash_screen.dart`:
- Around line 53-58: When handling the error/unknown branch (the block using
status.isError, SnackbarService.showError and setting nextScreen = const
AuthScreen()), clear the current Supabase session before navigating back to
AuthScreen by calling your auth client's sign-out/clear-session method (e.g.,
invoke signOut() on the Supabase auth client or your AuthRepository.signOut()).
Ensure this sign-out call happens before assigning nextScreen = const
AuthScreen() so the next launch doesn't see a stale session.
- Around line 37-38: Wrap the await
context.read<AuthProvider>().checkIfPatientExists() call in a try/catch so any
thrown exceptions are handled the same way as failed results: catch all errors,
ensure you still check mounted before routing, and set the provider/navigation
fallback to AuthNavigationStatus.error (or call the same fallback path used for
unsuccessful results) instead of letting the exception bubble and leave the app
stuck on Splash; reference checkIfPatientExists(), AuthProvider, mounted and
AuthNavigationStatus.error when making the change.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 58e02ed9-01ac-4b8f-9af9-de8cffd2b12d

📥 Commits

Reviewing files that changed from the base of the PR and between 421e7c2 and 9b1f731.

📒 Files selected for processing (1)
  • patient/lib/presentation/splash_screen.dart

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
patient/lib/presentation/splash_screen.dart (1)

39-44: ⚠️ Potential issue | 🟠 Major

Clear the Supabase session before redirecting to AuthScreen.

Both fallback paths inside the session != null branch route back to AuthScreen, but they leave currentSession intact. That leaves the app on a sign-in screen while Supabase still considers the user authenticated, and the next launch can re-enter the same failing splash flow.

Suggested fix
       try {
         await context.read<AuthProvider>().checkIfPatientExists();
       } catch (_) {
+        await Supabase.instance.client.auth.signOut();
+        if (!mounted) return;
         SnackbarService.showError('Something went wrong. Please sign in again.');
         Navigator.of(context).pushReplacement(
           MaterialPageRoute(builder: (_) => const AuthScreen()),
         );
         return;
@@
       } else {
         // error or unknown — fall back to sign-in with feedback
+        await Supabase.instance.client.auth.signOut();
+        if (!mounted) return;
         if (status.isError) {
           SnackbarService.showError('Something went wrong. Please sign in again.');
         }
         nextScreen = const AuthScreen();
       }
In supabase_flutter 2.8.4, does `Supabase.instance.client.auth.signOut()` clear the persisted local session so `Supabase.instance.client.auth.currentSession` is null on the next app launch?

Also applies to: 62-67

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

In `@patient/lib/presentation/splash_screen.dart` around lines 39 - 44, The
fallback/catch flows in splash_screen.dart navigate to AuthScreen but do not
clear the Supabase session, leaving Supabase.instance.client.auth.currentSession
populated; update the flows that call SnackbarService.showError(...) and
Navigator.of(context).pushReplacement(... -> const AuthScreen()) to first await
Supabase.instance.client.auth.signOut() (handle errors safely and check mounted
before navigating) so the persisted session is cleared before pushing
AuthScreen; apply the same change to both the catch block (where the catch
currently uses SnackbarService.showError and Navigator) and the other fallback
path around the 62-67 region to ensure currentSession is null on next launch.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@patient/lib/presentation/splash_screen.dart`:
- Around line 39-44: The fallback/catch flows in splash_screen.dart navigate to
AuthScreen but do not clear the Supabase session, leaving
Supabase.instance.client.auth.currentSession populated; update the flows that
call SnackbarService.showError(...) and
Navigator.of(context).pushReplacement(... -> const AuthScreen()) to first await
Supabase.instance.client.auth.signOut() (handle errors safely and check mounted
before navigating) so the persisted session is cleared before pushing
AuthScreen; apply the same change to both the catch block (where the catch
currently uses SnackbarService.showError and Navigator) and the other fallback
path around the 62-67 region to ensure currentSession is null on next launch.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a53033b9-8267-4eea-ad32-d3dfc863ea4c

📥 Commits

Reviewing files that changed from the base of the PR and between 9b1f731 and 5ad761f.

📒 Files selected for processing (1)
  • patient/lib/presentation/splash_screen.dart

@hasanravda
Copy link
Copy Markdown
Member Author

Can you review this @jddeep @SharkyBytes ?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Logged-in users are redirected to Login screen on every app launch

1 participant