Fix auth flow: ensure Appwrite client initialization, await splash au…#813
Fix auth flow: ensure Appwrite client initialization, await splash au…#813psrsingh wants to merge 2 commits into
Conversation
…th navigation, and make FCM token updates resilient
|
🎉 Welcome @psrsingh!
We appreciate your contribution! 🚀 |
📝 WalkthroughWalkthroughThe 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. ChangesAuthentication & App Startup Initialization
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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
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 winAwait token document writes and avoid duplicate token inserts.
In these loops, writes are fire-and-forget (
updateDocumentis not awaited), so failures are dropped and completion ordering is unreliable. Also, add flows appendfcmTokenblindly, 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
📒 Files selected for processing (4)
lib/controllers/auth_state_controller.dartlib/routes/app_pages.dartlib/services/appwrite_service.dartlib/views/screens/splash_screen.dart
💤 Files with no reviewable changes (1)
- lib/routes/app_pages.dart
|
✅ PR Closed - Thank You, @psrsingh!
We appreciate your effort and look forward to more contributions from you! 🤝 |
No description provided.