diff --git a/API/Application/Users/Commands/CreateUser.cs b/API/Application/Users/Commands/CreateUser.cs index 419ee62..e271b8d 100644 --- a/API/Application/Users/Commands/CreateUser.cs +++ b/API/Application/Users/Commands/CreateUser.cs @@ -82,6 +82,8 @@ namespace API.Application.Users.Commands CreatedAt = DateTime.UtcNow.AddHours(2), UpdatedAt = DateTime.UtcNow.AddHours(2), HashedPassword = hashedPassword, + RefreshToken = System.Guid.NewGuid().ToString(), + RefreshTokenExpiresAt = DateTime.UtcNow.AddDays(7), }; } } diff --git a/API/Application/Users/Commands/LoginUser.cs b/API/Application/Users/Commands/LoginUser.cs index 2e01ca5..de90f3d 100644 --- a/API/Application/Users/Commands/LoginUser.cs +++ b/API/Application/Users/Commands/LoginUser.cs @@ -34,7 +34,7 @@ namespace API.Application.Users.Commands } var jwtToken = _tokenHelper.GenerateJwtToken(user); - return new OkObjectResult(new { token = jwtToken, id = user.Id}); + return new OkObjectResult(new { token = jwtToken, id = user.Id, refreshToken = user.RefreshToken}); } } diff --git a/API/Controllers/UsersController.cs b/API/Controllers/UsersController.cs index d58d0f1..07c3c2d 100644 --- a/API/Controllers/UsersController.cs +++ b/API/Controllers/UsersController.cs @@ -87,14 +87,12 @@ namespace API.Controllers public async Task DeleteUser(string id) { return await _deleteUser.Handle(id); - } - - [Authorize] + } + [HttpPost("/RefreshToken")] - public async Task RefreshToken() + public async Task RefreshToken(RefreshTokenDTO refreshTokenDTO) { - var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); - var user = await _repository.QueryUserByIdAsync(userId); + User user = await _repository.QueryUserByRefreshTokenAsync(refreshTokenDTO.RefreshToken); return new OkObjectResult(_tokenHelper.GenerateJwtToken(user)); } diff --git a/API/Models/User.cs b/API/Models/User.cs index fcaaa30..8bdab07 100644 --- a/API/Models/User.cs +++ b/API/Models/User.cs @@ -39,3 +39,8 @@ public class UpdateUserDTO public string Password { get; set; } } +public class RefreshTokenDTO +{ + public string RefreshToken { get; set; } +} + diff --git a/API/Persistence/Repositories/IUserRepository.cs b/API/Persistence/Repositories/IUserRepository.cs index 690210f..3678a6c 100644 --- a/API/Persistence/Repositories/IUserRepository.cs +++ b/API/Persistence/Repositories/IUserRepository.cs @@ -10,5 +10,6 @@ namespace API.Persistence.Repositories Task QueryUserByIdAsync(string id); Task QueryUserByEmailAsync(string email); Task UpdateUserAsync(User user); + Task QueryUserByRefreshTokenAsync(string refreshToken); } } \ No newline at end of file diff --git a/API/Persistence/Repositories/UserRepository.cs b/API/Persistence/Repositories/UserRepository.cs index da8f833..3509bd9 100644 --- a/API/Persistence/Repositories/UserRepository.cs +++ b/API/Persistence/Repositories/UserRepository.cs @@ -7,6 +7,7 @@ namespace API.Persistence.Repositories public class UserRepository(AppDBContext context) : IUserRepository { private readonly AppDBContext _context = context; + public async Task> QueryAllUsersAsync() { return await _context.Users.ToListAsync(); @@ -16,14 +17,12 @@ namespace API.Persistence.Repositories { try { - return await _context.Users - .FirstOrDefaultAsync(user => user.Id == id); + return await _context.Users.FirstOrDefaultAsync(user => user.Id == id); } catch (Exception) { return new User(); } - } public async Task CreateUserAsync(User user) @@ -73,7 +72,11 @@ namespace API.Persistence.Repositories public async Task QueryUserByEmailAsync(string email) { return await _context.Users.SingleOrDefaultAsync(u => u.Email == email); - + } + + public async Task QueryUserByRefreshTokenAsync(string refreshToken) + { + return await _context.Users.SingleOrDefaultAsync(u => u.RefreshToken == refreshToken); } } }