Skip to content

Fix auth flow: ensure Appwrite client initialization, await splash au…#813

Closed
psrsingh wants to merge 2 commits into
AOSSIE-Org:masterfrom
psrsingh:fix/auth-flow
Closed

Fix auth flow: ensure Appwrite client initialization, await splash au…#813
psrsingh wants to merge 2 commits into
AOSSIE-Org:masterfrom
psrsingh:fix/auth-flow

Conversation

@psrsingh

@psrsingh psrsingh commented May 19, 2026

Copy link
Copy Markdown

No description provided.

…th navigation, and make FCM token updates resilient
@psrsingh psrsingh requested a review from M4dhav as a code owner May 19, 2026 09:23
@github-actions

Copy link
Copy Markdown
Contributor

🎉 Welcome @psrsingh!
Thank you for your pull request! Our team will review it soon. 🔍

  • Please ensure your PR follows the contribution guidelines. ✅
  • All automated tests should pass before merging. 🔄
  • If this PR fixes an issue, link it in the description. 🔗

We appreciate your contribution! 🚀

@coderabbitai

coderabbitai Bot commented May 19, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The PR hardens the authentication and app startup flow by introducing null-safe data access patterns, strengthening FCM token handling with early guards and error wrapping, ensuring Appwrite client initialization, removing obsolete routes, and refactoring the splash screen update-check navigation logic to centralize authentication checks.

Changes

Authentication & App Startup Initialization

Layer / File(s) Summary
User profile data with null-safe access
lib/controllers/auth_state_controller.dart
setUserProfileData now uses null-safe access (?.) to read the profile completion flag and applies safer defaults (including numeric type guards for ratingTotal) when loading user document fields and mapping followers.
FCM token registration and removal with safety guards
lib/controllers/auth_state_controller.dart
login wraps token registration in try/catch to log and suppress failures. Registration and removal helpers add early returns when FCM token is null or empty, and all token array updates switch from forced non-null assertions (fcmToken!) to guarded values.
Appwrite service client initialization
lib/services/appwrite_service.dart
getAccount() ensures _client is lazily initialized via null-coalescing assignment before creating the Account instance.
Route configuration cleanup
lib/routes/app_pages.dart
Removes AppRoutes.resetPassword and AppRoutes.loginScreen entries from the pages routing table.
Splash screen update-check and navigation refactor
lib/views/screens/splash_screen.dart
Simplifies update-check callbacks to only return true and moves authentication state initialization and conditional navigation logic into post-check result handling in _setupTimers, which now exits early when update is unavailable or check fails.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Suggested labels

enhancement

Poem

🐰 Null checks hop with grace so fine,
FCM tokens safely align,
Routes trimmed clean, flows refined,
Startup dance more organized in mind!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title captures the main improvements: Appwrite client initialization, splash auth navigation awaiting, and FCM token resilience, matching the core changes across multiple files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
lib/controllers/auth_state_controller.dart (1)

298-325: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Await token document writes and avoid duplicate token inserts.

In these loops, writes are fire-and-forget (updateDocument is not awaited), so failures are dropped and completion ordering is unreliable. Also, add flows append fcmToken blindly, which can duplicate tokens after repeated logins.

Proposed fix
-      registrationTokens.add(fcmToken);
-      databases.updateDocument(
+      if (!registrationTokens.contains(fcmToken)) {
+        registrationTokens.add(fcmToken);
+      }
+      await databases.updateDocument(
         databaseId: upcomingRoomsDatabaseId,
         collectionId: subscribedUserCollectionId,
         documentId: subscription.$id,
         data: {"registrationTokens": registrationTokens},
       );
@@
-      creatorFcmTokens.add(fcmToken);
-      databases.updateDocument(
+      if (!creatorFcmTokens.contains(fcmToken)) {
+        creatorFcmTokens.add(fcmToken);
+      }
+      await databases.updateDocument(
         databaseId: upcomingRoomsDatabaseId,
         collectionId: upcomingRoomsCollectionId,
         documentId: upcomingRoom.$id,
         data: {"creator_fcm_tokens": creatorFcmTokens},
       );
@@
-      registrationTokens.remove(fcmToken);
-      databases.updateDocument(
+      registrationTokens.remove(fcmToken);
+      await databases.updateDocument(
         databaseId: upcomingRoomsDatabaseId,
         collectionId: subscribedUserCollectionId,
         documentId: subscription.$id,
         data: {"registrationTokens": registrationTokens},
       );
@@
-      creatorFcmTokens.remove(fcmToken);
-      databases.updateDocument(
+      creatorFcmTokens.remove(fcmToken);
+      await databases.updateDocument(
         databaseId: upcomingRoomsDatabaseId,
         collectionId: upcomingRoomsCollectionId,
         documentId: upcomingRoom.$id,
         data: {"creator_fcm_tokens": creatorFcmTokens},
       );

Also applies to: 349-376

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lib/controllers/auth_state_controller.dart` around lines 298 - 325, The
updateDocument calls inside the loops that handle registrationTokens (using
variables registrationTokens and method databases.updateDocument with
documentId: subscription.$id) and creator_fcm_tokens (upcomingRoom and
databases.updateDocument with documentId: upcomingRoom.$id) are currently
fire-and-forget and blindly append fcmToken, so make both updates await the
futures and deduplicate before appending: check if
registrationTokens.contains(fcmToken) and creatorFcmTokens.contains(fcmToken)
and only push when missing, then await databases.updateDocument(...) so failures
propagate; apply the identical fix to the repeated block referenced in the later
section (lines ~349-376) that uses the same list mutation and
databases.updateDocument calls.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@lib/controllers/auth_state_controller.dart`:
- Around line 204-205: The isUserProfileComplete boolean short-circuits
initialization and can leave late fields (notably reportsCount) unassigned,
causing a LateInitializationError when isUserLoggedIn later reads reportsCount;
before the early return/guard that checks
appwriteUser.prefs?.data?['isUserProfileComplete'] (and before the
isUserProfileComplete == true branch), ensure all late-initialized profile
fields used later (e.g., reportsCount and any other late vars referenced by
isUserLoggedIn) are assigned default values from appwriteUser (or safe defaults)
so they are always initialized regardless of the profile-complete gate; update
the initialization logic in the auth state setup where appwriteUser and
isUserProfileComplete are processed to assign reportsCount and other late fields
first, then perform the profile-complete check.

---

Outside diff comments:
In `@lib/controllers/auth_state_controller.dart`:
- Around line 298-325: The updateDocument calls inside the loops that handle
registrationTokens (using variables registrationTokens and method
databases.updateDocument with documentId: subscription.$id) and
creator_fcm_tokens (upcomingRoom and databases.updateDocument with documentId:
upcomingRoom.$id) are currently fire-and-forget and blindly append fcmToken, so
make both updates await the futures and deduplicate before appending: check if
registrationTokens.contains(fcmToken) and creatorFcmTokens.contains(fcmToken)
and only push when missing, then await databases.updateDocument(...) so failures
propagate; apply the identical fix to the repeated block referenced in the later
section (lines ~349-376) that uses the same list mutation and
databases.updateDocument calls.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 81775ba0-f397-4c70-bb79-baf1a69afb26

📥 Commits

Reviewing files that changed from the base of the PR and between bf1dbe2 and f12e852.

📒 Files selected for processing (4)
  • lib/controllers/auth_state_controller.dart
  • lib/routes/app_pages.dart
  • lib/services/appwrite_service.dart
  • lib/views/screens/splash_screen.dart
💤 Files with no reviewable changes (1)
  • lib/routes/app_pages.dart

Comment thread lib/controllers/auth_state_controller.dart
@psrsingh psrsingh closed this May 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

PR Closed - Thank You, @psrsingh!

  • If this PR was merged: Congratulations! Your contribution is now part of the project. 🚀
  • If this PR was closed without merging: Don’t worry! You can always improve it and submit again. 💪

We appreciate your effort and look forward to more contributions from you! 🤝

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.

1 participant