Migrate TokenService.RefreshTokenAsync to UserManager

Last remaining IAuthRepository call site; RefreshTokenResult now
carries a plain (Guid, string) instead of UserAccount.
This commit is contained in:
Aaron Po
2026-08-22 01:36:27 -04:00
parent ceadf125d8
commit 96c83842d3
6 changed files with 37 additions and 37 deletions

View File

@@ -22,8 +22,8 @@ public class RefreshTokenHandler(ITokenService tokenService)
{
RefreshTokenResult result = await tokenService.RefreshTokenAsync(request.RefreshToken);
return new LoginPayload(
result.UserAccount.UserAccountId,
result.UserAccount.Username,
result.UserId,
result.Username,
result.RefreshToken,
result.AccessToken
);

View File

@@ -1,5 +1,4 @@
using System.Security.Claims;
using Domain.Entities;
namespace Features.Auth.Services;
@@ -22,7 +21,7 @@ public enum TokenType
public record ValidatedToken(Guid UserId, string Username, ClaimsPrincipal Principal);
/// <summary>Represents the result of refreshing a user's session.</summary>
public record RefreshTokenResult(UserAccount UserAccount, string RefreshToken, string AccessToken);
public record RefreshTokenResult(Guid UserId, string Username, string RefreshToken, string AccessToken);
/// <summary>
/// Defines the expiration windows, in hours, for each type of token issued by <see cref="ITokenService" />.

View File

@@ -1,9 +1,9 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Domain.Entities;
using Domain.Exceptions;
using Features.Auth.Repository;
using Features.Auth.Identity;
using Infrastructure.Jwt;
using Microsoft.AspNetCore.Identity;
namespace Features.Auth.Services;
@@ -14,20 +14,23 @@ namespace Features.Auth.Services;
public class TokenService : ITokenService
{
private readonly string _accessTokenSecret;
private readonly IAuthRepository _authRepository;
private readonly string _confirmationTokenSecret;
private readonly string _refreshTokenSecret;
private readonly ITokenInfrastructure _tokenInfrastructure;
private readonly UserManager<ApplicationUser> _userManager;
/// <summary>Initializes a new instance of the <see cref="TokenService" /> class.</summary>
/// <exception cref="InvalidOperationException">
/// Thrown when any of the <c>ACCESS_TOKEN_SECRET</c>, <c>REFRESH_TOKEN_SECRET</c>, or
/// <c>CONFIRMATION_TOKEN_SECRET</c> environment variables are not set.
/// </exception>
public TokenService(ITokenInfrastructure tokenInfrastructure, IAuthRepository authRepository)
public TokenService(
ITokenInfrastructure tokenInfrastructure,
UserManager<ApplicationUser> userManager
)
{
_tokenInfrastructure = tokenInfrastructure;
_authRepository = authRepository;
_userManager = userManager;
_accessTokenSecret =
Environment.GetEnvironmentVariable("ACCESS_TOKEN_SECRET")
@@ -100,14 +103,14 @@ public class TokenService : ITokenService
public async Task<RefreshTokenResult> RefreshTokenAsync(string refreshTokenString)
{
ValidatedToken validated = await ValidateRefreshTokenAsync(refreshTokenString);
UserAccount? user = await _authRepository.GetUserByIdAsync(validated.UserId);
ApplicationUser? user = await _userManager.FindByIdAsync(validated.UserId.ToString());
if (user == null)
throw new UnauthorizedException("User account not found");
string newAccess = GenerateAccessToken(user.UserAccountId, user.Username);
string newRefresh = GenerateRefreshToken(user.UserAccountId, user.Username);
string newAccess = GenerateAccessToken(user.Id, user.UserName);
string newRefresh = GenerateRefreshToken(user.Id, user.UserName);
return new RefreshTokenResult(user, newRefresh, newAccess);
return new RefreshTokenResult(user.Id, user.UserName, newRefresh, newAccess);
}
/// <summary>