Skip to content

Commit 3af92e3

Browse files
committed
refactor(auth): upgrade anonymous user linking process
- Update user creation flow for both registered and anonymous users - Enhance user object with additional properties and default values - Improve error handling and validation for account linking process - Replace role checks with more specific appRole comparisons
1 parent 7f64912 commit 3af92e3

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

lib/src/services/auth_service.dart

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,21 @@ class AuthService {
169169

170170
// All new users created via the public API get the standard role.
171171
// Admin users must be provisioned out-of-band (e.g., via fixtures).
172-
final roles = [UserRoles.standardUser];
173-
174-
user = User(id: _uuid.v4(), email: email, roles: roles);
172+
user = User(
173+
id: _uuid.v4(),
174+
email: email,
175+
appRole: AppUserRole.standardUser,
176+
dashboardRole: DashboardUserRole.none,
177+
createdAt: DateTime.now(),
178+
feedActionStatus: Map.fromEntries(
179+
FeedActionType.values.map(
180+
(type) => MapEntry(
181+
type,
182+
const UserFeedActionStatus(isCompleted: false),
183+
),
184+
),
185+
),
186+
);
175187
user = await _userRepository.create(item: user);
176188
print('Created new user: ${user.id} with roles: ${user.roles}');
177189

@@ -224,9 +236,21 @@ class AuthService {
224236
User user;
225237
try {
226238
user = User(
227-
id: _uuid.v4(), // Generate new ID
228-
roles: const [UserRoles.guestUser], // Anonymous users are guest users
229-
email: null, // Anonymous users don't have an email initially
239+
id: _uuid.v4(),
240+
// Use a unique placeholder email for anonymous users to satisfy the
241+
// non-nullable email constraint.
242+
email: '${_uuid.v4()}@anonymous.com',
243+
appRole: AppUserRole.guestUser,
244+
dashboardRole: DashboardUserRole.none,
245+
createdAt: DateTime.now(),
246+
feedActionStatus: Map.fromEntries(
247+
FeedActionType.values.map(
248+
(type) => MapEntry(
249+
type,
250+
const UserFeedActionStatus(isCompleted: false),
251+
),
252+
),
253+
),
230254
);
231255
user = await _userRepository.create(item: user);
232256
print('Created anonymous user: ${user.id}');
@@ -335,7 +359,7 @@ class AuthService {
335359
required User anonymousUser,
336360
required String emailToLink,
337361
}) async {
338-
if (!anonymousUser.roles.contains(UserRoles.guestUser)) {
362+
if (anonymousUser.appRole != AppUserRole.guestUser) {
339363
throw const BadRequestException(
340364
'Account is already permanent. Cannot link email.',
341365
);
@@ -348,8 +372,7 @@ class AuthService {
348372

349373
// Filter for permanent users (not guests) that are not the current user.
350374
final conflictingPermanentUsers = existingUsersResponse.items.where(
351-
(u) =>
352-
!u.roles.contains(UserRoles.guestUser) && u.id != anonymousUser.id,
375+
(u) => u.appRole != AppUserRole.guestUser && u.id != anonymousUser.id,
353376
);
354377

355378
if (conflictingPermanentUsers.isNotEmpty) {
@@ -399,7 +422,7 @@ class AuthService {
399422
required String codeFromUser,
400423
required String oldAnonymousToken, // Needed to invalidate it
401424
}) async {
402-
if (!anonymousUser.roles.contains(UserRoles.guestUser)) {
425+
if (anonymousUser.appRole != AppUserRole.guestUser) {
403426
// Should ideally not happen if flow is correct, but good safeguard.
404427
throw const BadRequestException(
405428
'Account is already permanent. Cannot complete email linking.',
@@ -421,10 +444,9 @@ class AuthService {
421444
}
422445

423446
// 2. Update the user to be permanent.
424-
final updatedUser = User(
425-
id: anonymousUser.id, // Preserve original ID
447+
final updatedUser = anonymousUser.copyWith(
426448
email: linkedEmail,
427-
roles: const [UserRoles.standardUser], // Now a permanent standard user
449+
appRole: AppUserRole.standardUser,
428450
);
429451
final permanentUser = await _userRepository.update(
430452
id: updatedUser.id,

0 commit comments

Comments
 (0)