Skip to content

Commit 28b1185

Browse files
committed
feat(auth): improve guest to standard user conversion
- Invalidate old guest token during account conversion - Update comments and step numbers in the login process - Add error logging for token invalidation
1 parent 01da8ab commit 28b1185

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

lib/src/services/auth_service.dart

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ class AuthService {
125125
/// - **Guest Sign-In:** If an authenticated `guestUser` (from
126126
/// [authenticatedUser]) performs this action, the service checks if a
127127
/// permanent account with the verified [email] already exists.
128-
/// - If it exists, the user is signed into that account, and the temporary
129-
/// guest account is deleted.
128+
/// - If it exists, the user is signed into that account, the old guest
129+
/// token is invalidated, and the temporary guest account is deleted.
130130
/// - If it does not exist, the guest account is converted into a new
131-
/// permanent `standardUser` with the verified [email].
131+
/// permanent `standardUser`, and the old guest token is invalidated.
132132
///
133133
/// - **Dashboard Login:** If [isDashboardLogin] is true, it performs a
134134
/// strict login for an existing user with dashboard permissions.
@@ -145,6 +145,7 @@ class AuthService {
145145
String code, {
146146
required bool isDashboardLogin,
147147
User? authenticatedUser,
148+
String? currentToken,
148149
}) async {
149150
// 1. Validate the verification code.
150151
final isValidCode =
@@ -162,7 +163,24 @@ class AuthService {
162163
);
163164
}
164165

165-
// 2. Check if the sign-in is initiated from an authenticated guest session.
166+
// 2. If this is a guest flow, invalidate the old anonymous token.
167+
// This is a fire-and-forget operation; we don't want to block the
168+
// login if invalidation fails, but we should log any errors.
169+
if (authenticatedUser != null &&
170+
authenticatedUser.appRole == AppUserRole.guestUser &&
171+
currentToken != null) {
172+
unawaited(
173+
_authTokenService.invalidateToken(currentToken).catchError((e, s) {
174+
_log.warning(
175+
'Failed to invalidate old anonymous token for user ${authenticatedUser.id}.',
176+
e,
177+
s is StackTrace ? s : null,
178+
);
179+
}),
180+
);
181+
}
182+
183+
// 3. Check if the sign-in is initiated from an authenticated guest session.
166184
if (authenticatedUser != null &&
167185
authenticatedUser.appRole == AppUserRole.guestUser) {
168186
_log.info(
@@ -211,7 +229,7 @@ class AuthService {
211229
}
212230
}
213231

214-
// 3. If not a guest flow, proceed with standard or dashboard login.
232+
// 4. If not a guest flow, proceed with standard or dashboard login.
215233
User user;
216234
try {
217235
// Attempt to find user by email
@@ -287,7 +305,7 @@ class AuthService {
287305
throw const OperationFailedException('Failed to process user account.');
288306
}
289307

290-
// 3. Generate authentication token
308+
// 4. Generate authentication token
291309
try {
292310
final token = await _authTokenService.generateToken(user);
293311
_log.info('Generated token for user ${user.id}');

0 commit comments

Comments
 (0)