@@ -125,10 +125,10 @@ class AuthService {
125
125
/// - **Guest Sign-In:** If an authenticated `guestUser` (from
126
126
/// [authenticatedUser] ) performs this action, the service checks if a
127
127
/// 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.
130
130
/// - 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 .
132
132
///
133
133
/// - **Dashboard Login:** If [isDashboardLogin] is true, it performs a
134
134
/// strict login for an existing user with dashboard permissions.
@@ -145,6 +145,7 @@ class AuthService {
145
145
String code, {
146
146
required bool isDashboardLogin,
147
147
User ? authenticatedUser,
148
+ String ? currentToken,
148
149
}) async {
149
150
// 1. Validate the verification code.
150
151
final isValidCode =
@@ -162,7 +163,24 @@ class AuthService {
162
163
);
163
164
}
164
165
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.
166
184
if (authenticatedUser != null &&
167
185
authenticatedUser.appRole == AppUserRole .guestUser) {
168
186
_log.info (
@@ -211,7 +229,7 @@ class AuthService {
211
229
}
212
230
}
213
231
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.
215
233
User user;
216
234
try {
217
235
// Attempt to find user by email
@@ -287,7 +305,7 @@ class AuthService {
287
305
throw const OperationFailedException ('Failed to process user account.' );
288
306
}
289
307
290
- // 3 . Generate authentication token
308
+ // 4 . Generate authentication token
291
309
try {
292
310
final token = await _authTokenService.generateToken (user);
293
311
_log.info ('Generated token for user ${user .id }' );
0 commit comments