Skip to content

Commit b496e7f

Browse files
committed
fix(auth): initialize demo user data on transition
Fixes a critical bug in the demo environment where authenticating as a new user (anonymous or via email) would fail. Previously, when a new user was created, the `AppInitializer` would immediately try to fetch their settings and preferences. In the demo environment, this data did not exist yet, causing a `NotFoundException` that would halt the login process and leave the UI unresponsive. This fix modifies `AppInitializer.handleUserTransition` to explicitly call `DemoDataInitializerService` for new users in the demo environment. This ensures that the necessary user data is created on-the-fly *before* any attempt is made to read it, resolving the race condition and allowing the authentication flow to complete successfully.
1 parent ef93f0d commit b496e7f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

lib/app/services/app_initializer.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,23 @@ class AppInitializer {
306306
_logger.fine(
307307
'[AppInitializer] Re-fetching user data for transitioned user ${newUser.id}...',
308308
);
309+
310+
// --- Demo-Specific Logic: Initialize Data on Transition ---
311+
// In demo mode, when a new user authenticates (e.g., anonymous sign-in
312+
// or email verification), their user-specific data (settings, preferences)
313+
// does not yet exist in the in-memory repositories. This block ensures
314+
// that the DemoDataInitializerService is called to create this data
315+
// *before* the subsequent code attempts to read it. This prevents a
316+
// NotFoundException that would otherwise cause a critical error and
317+
// stall the authentication flow.
318+
if (_environment == local_config.AppEnvironment.demo) {
319+
_logger.info(
320+
'[AppInitializer] Demo mode: Initializing data for new user '
321+
'${newUser.id} during transition.',
322+
);
323+
await demoDataInitializerService?.initializeUserSpecificData(newUser);
324+
}
325+
309326
try {
310327
final [
311328
userAppSettings as UserAppSettings?,

0 commit comments

Comments
 (0)