Skip to content

Commit 2765017

Browse files
committed
feat(auth): Enhance sign-out process
- Added token invalidation to sign-out - Improved error handling
1 parent bac9d80 commit 2765017

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

routes/api/v1/auth/sign-out.dart

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,36 @@ Future<Response> onRequest(RequestContext context) async {
2424
throw const UnauthorizedException('Authentication required to sign out.');
2525
}
2626

27+
// Extract the current token from the Authorization header
28+
final authHeader = context.request.headers[HttpHeaders.authorizationHeader];
29+
String? token;
30+
if (authHeader != null && authHeader.startsWith('Bearer ')) {
31+
token = authHeader.substring(7);
32+
}
33+
34+
// Although authentication middleware should ensure a token is present,
35+
// this check acts as a safeguard.
36+
if (token == null || token.isEmpty) {
37+
print(
38+
'Error: Could not extract Bearer token for user ${user.id} in sign-out handler.',
39+
);
40+
throw const OperationFailedException(
41+
'Internal error: Unable to retrieve authentication token for sign-out.',
42+
);
43+
}
44+
2745
// Read the AuthService provided by middleware
2846
final authService = context.read<AuthService>();
2947

3048
try {
31-
// Call the AuthService to handle any server-side sign-out logic
32-
await authService.performSignOut(userId: user.id);
49+
// Call the AuthService to handle any server-side sign-out logic,
50+
// including token invalidation.
51+
await authService.performSignOut(userId: user.id, token: token);
3352

3453
// Return 204 No Content indicating successful sign-out action
3554
return Response(statusCode: HttpStatus.noContent);
3655
} on HtHttpException catch (_) {
3756
// Let the central errorHandler middleware handle known exceptions
38-
// (though performSignOut might not throw many specific ones)
3957
rethrow;
4058
} catch (e) {
4159
// Catch unexpected errors from the service layer

0 commit comments

Comments
 (0)