1
1
using System . Security . Claims ;
2
2
using api . DTOs . Auth . Requests ;
3
+ using api . DTOs . Users ;
3
4
using api . Services . Auth . interfaces ;
4
5
using api . Services . Users . interfaces ;
5
6
using Asp . Versioning ;
@@ -12,20 +13,29 @@ namespace api.Controllers.Auth.v1;
12
13
[ ApiController ]
13
14
[ Route ( "api/auth" ) ]
14
15
[ ApiVersion ( "1" ) ]
15
- public class AuthController ( IAuthService service , IHttpContextAccessor httpContextAccessor ) : ControllerBase
16
+ public class AuthController ( IAuthService service ) : ControllerBase
16
17
{
17
18
private readonly IAuthService _service = service ?? throw new ArgumentNullException ( nameof ( service ) ) ;
18
- private readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor ;
19
19
20
20
21
21
[ HttpGet ( "me" ) ]
22
22
[ Authorize ]
23
- public async Task < IActionResult > GetMe ( )
23
+ public async Task < IActionResult > AuthMe ( )
24
24
{
25
25
26
- // _httpContextAccessor!.HttpContext.GetTokenAsync();
27
26
28
- return Ok ( ) ;
27
+ var userIdClaim = User . FindFirst ( ClaimTypes . NameIdentifier ) ? . Value ;
28
+ if ( string . IsNullOrEmpty ( userIdClaim ) || ! Guid . TryParse ( userIdClaim , out Guid userId ) )
29
+ {
30
+ return Unauthorized ( "Invalid token" ) ;
31
+ }
32
+
33
+ UserDto ? response = await _service . GetById ( userId ) ;
34
+
35
+ if ( response == null ) return NotFound ( "User not found" ) ;
36
+
37
+
38
+ return Ok ( response ) ;
29
39
}
30
40
31
41
[ HttpPost ( "register" ) ]
@@ -73,10 +83,22 @@ public async Task<IActionResult> Login([FromBody] LoginRequestDto requestDto)
73
83
74
84
[ Authorize ]
75
85
[ HttpPost ( "logout" ) ]
76
- public IActionResult Logout ( )
86
+ public async Task < IActionResult > Logout ( )
87
+
77
88
{
78
- Response . Cookies . Delete ( "ACCESS_TOKEN" ) ;
79
- return Ok ( ) ;
89
+ var userIdClaim = User . FindFirst ( ClaimTypes . NameIdentifier ) ? . Value ;
90
+ if ( string . IsNullOrEmpty ( userIdClaim ) || ! Guid . TryParse ( userIdClaim , out Guid userId ) )
91
+ {
92
+ return Unauthorized ( "Invalid token" ) ;
93
+ }
94
+
95
+ bool response = await _service . Logout ( userId ) ;
96
+ if ( ! response )
97
+ {
98
+ return StatusCode ( 500 , "Logout failed" ) ;
99
+ }
100
+
101
+ return Ok ( new { message = "Logged out successfully" } ) ;
80
102
}
81
103
82
104
0 commit comments