Skip to content

Commit f54311a

Browse files
added migration
1 parent 182338b commit f54311a

21 files changed

+11696
-111
lines changed

api/Controllers/Auth/v1/AuthController.cs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Security.Claims;
22
using api.DTOs.Auth.Requests;
3+
using api.DTOs.Users;
34
using api.Services.Auth.interfaces;
45
using api.Services.Users.interfaces;
56
using Asp.Versioning;
@@ -12,20 +13,29 @@ namespace api.Controllers.Auth.v1;
1213
[ApiController]
1314
[Route("api/auth")]
1415
[ApiVersion("1")]
15-
public class AuthController(IAuthService service,IHttpContextAccessor httpContextAccessor) : ControllerBase
16+
public class AuthController(IAuthService service) : ControllerBase
1617
{
1718
private readonly IAuthService _service = service ?? throw new ArgumentNullException(nameof(service));
18-
private readonly IHttpContextAccessor _httpContextAccessor=httpContextAccessor;
1919

2020

2121
[HttpGet("me")]
2222
[Authorize]
23-
public async Task<IActionResult> GetMe()
23+
public async Task<IActionResult> AuthMe()
2424
{
2525

26-
// _httpContextAccessor!.HttpContext.GetTokenAsync();
2726

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);
2939
}
3040

3141
[HttpPost("register")]
@@ -73,10 +83,22 @@ public async Task<IActionResult> Login([FromBody] LoginRequestDto requestDto)
7383

7484
[Authorize]
7585
[HttpPost("logout")]
76-
public IActionResult Logout()
86+
public async Task<IActionResult> Logout( )
87+
7788
{
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" });
80102
}
81103

82104

0 commit comments

Comments
 (0)