Implement Refresh Token on startup
Co-authored-by: Reimar <mail@reim.ar>
This commit is contained in:
parent
ffb5e20f79
commit
a471a11015
@ -82,6 +82,8 @@ namespace API.Application.Users.Commands
|
|||||||
CreatedAt = DateTime.UtcNow.AddHours(2),
|
CreatedAt = DateTime.UtcNow.AddHours(2),
|
||||||
UpdatedAt = DateTime.UtcNow.AddHours(2),
|
UpdatedAt = DateTime.UtcNow.AddHours(2),
|
||||||
HashedPassword = hashedPassword,
|
HashedPassword = hashedPassword,
|
||||||
|
RefreshToken = System.Guid.NewGuid().ToString(),
|
||||||
|
RefreshTokenExpiresAt = DateTime.UtcNow.AddDays(7),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ namespace API.Application.Users.Commands
|
|||||||
}
|
}
|
||||||
var jwtToken = _tokenHelper.GenerateJwtToken(user);
|
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});
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,14 +87,12 @@ namespace API.Controllers
|
|||||||
public async Task<IActionResult> DeleteUser(string id)
|
public async Task<IActionResult> DeleteUser(string id)
|
||||||
{
|
{
|
||||||
return await _deleteUser.Handle(id);
|
return await _deleteUser.Handle(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Authorize]
|
|
||||||
[HttpPost("/RefreshToken")]
|
[HttpPost("/RefreshToken")]
|
||||||
public async Task<IActionResult> RefreshToken()
|
public async Task<IActionResult> RefreshToken(RefreshTokenDTO refreshTokenDTO)
|
||||||
{
|
{
|
||||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
User user = await _repository.QueryUserByRefreshTokenAsync(refreshTokenDTO.RefreshToken);
|
||||||
var user = await _repository.QueryUserByIdAsync(userId);
|
|
||||||
return new OkObjectResult(_tokenHelper.GenerateJwtToken(user));
|
return new OkObjectResult(_tokenHelper.GenerateJwtToken(user));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,3 +39,8 @@ public class UpdateUserDTO
|
|||||||
public string Password { get; set; }
|
public string Password { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class RefreshTokenDTO
|
||||||
|
{
|
||||||
|
public string RefreshToken { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -10,5 +10,6 @@ namespace API.Persistence.Repositories
|
|||||||
Task<User> QueryUserByIdAsync(string id);
|
Task<User> QueryUserByIdAsync(string id);
|
||||||
Task<User> QueryUserByEmailAsync(string email);
|
Task<User> QueryUserByEmailAsync(string email);
|
||||||
Task<bool> UpdateUserAsync(User user);
|
Task<bool> UpdateUserAsync(User user);
|
||||||
|
Task<User> QueryUserByRefreshTokenAsync(string refreshToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -7,6 +7,7 @@ namespace API.Persistence.Repositories
|
|||||||
public class UserRepository(AppDBContext context) : IUserRepository
|
public class UserRepository(AppDBContext context) : IUserRepository
|
||||||
{
|
{
|
||||||
private readonly AppDBContext _context = context;
|
private readonly AppDBContext _context = context;
|
||||||
|
|
||||||
public async Task<List<User>> QueryAllUsersAsync()
|
public async Task<List<User>> QueryAllUsersAsync()
|
||||||
{
|
{
|
||||||
return await _context.Users.ToListAsync();
|
return await _context.Users.ToListAsync();
|
||||||
@ -16,14 +17,12 @@ namespace API.Persistence.Repositories
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return await _context.Users
|
return await _context.Users.FirstOrDefaultAsync(user => user.Id == id);
|
||||||
.FirstOrDefaultAsync(user => user.Id == id);
|
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
return new User();
|
return new User();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string> CreateUserAsync(User user)
|
public async Task<string> CreateUserAsync(User user)
|
||||||
@ -73,7 +72,11 @@ namespace API.Persistence.Repositories
|
|||||||
public async Task<User> QueryUserByEmailAsync(string email)
|
public async Task<User> QueryUserByEmailAsync(string email)
|
||||||
{
|
{
|
||||||
return await _context.Users.SingleOrDefaultAsync(u => u.Email == email);
|
return await _context.Users.SingleOrDefaultAsync(u => u.Email == email);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<User> QueryUserByRefreshTokenAsync(string refreshToken)
|
||||||
|
{
|
||||||
|
return await _context.Users.SingleOrDefaultAsync(u => u.RefreshToken == refreshToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user