@@ -24,18 +24,36 @@ Future<Response> onRequest(RequestContext context) async {
24
24
throw const UnauthorizedException ('Authentication required to sign out.' );
25
25
}
26
26
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
+
27
45
// Read the AuthService provided by middleware
28
46
final authService = context.read <AuthService >();
29
47
30
48
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);
33
52
34
53
// Return 204 No Content indicating successful sign-out action
35
54
return Response (statusCode: HttpStatus .noContent);
36
55
} on HtHttpException catch (_) {
37
56
// Let the central errorHandler middleware handle known exceptions
38
- // (though performSignOut might not throw many specific ones)
39
57
rethrow ;
40
58
} catch (e) {
41
59
// Catch unexpected errors from the service layer
0 commit comments