Refactor authentication services and implement JWT validation logic

This commit is contained in:
Aaron Po
2026-02-26 23:44:52 -05:00
parent 0ab2eaaec9
commit 250e5f2c9c
7 changed files with 106 additions and 24 deletions

View File

@@ -10,12 +10,13 @@ public interface IConfirmationService
Task<ConfirmationServiceReturn> ConfirmUserAsync(string confirmationToken);
}
public class ConfirmationService(IAuthRepository authRepository) : IConfirmationService
public class ConfirmationService(IAuthRepository authRepository)
: IConfirmationService
{
public async Task<ConfirmationServiceReturn> ConfirmUserAsync(string confirmationToken)
public async Task<ConfirmationServiceReturn> ConfirmUserAsync(
string confirmationToken
)
{
return new ConfirmationServiceReturn(DateTime.Now, Guid.NewGuid());
}
}
}

View File

@@ -3,11 +3,21 @@ using Infrastructure.Jwt;
namespace Service.Auth;
public enum TokenType
{
AccessToken,
RefreshToken,
ConfirmationToken,
}
public interface ITokenService
{
public string GenerateAccessToken(UserAccount user);
public string GenerateRefreshToken(UserAccount user);
public string GenerateConfirmationToken(UserAccount user);
public string GenerateToken<T>(UserAccount user)
where T : struct, Enum;
}
public static class TokenServiceExpirationHours
@@ -76,4 +86,28 @@ public class TokenService(ITokenInfrastructure tokenInfrastructure)
_confirmationTokenSecret
);
}
public string GenerateToken<T>(UserAccount userAccount)
where T : struct, Enum
{
var tokenType = typeof(T);
if (tokenType == typeof(TokenType))
{
var tokenTypeValue = (TokenType)
Enum.Parse(tokenType, typeof(T).Name);
return tokenTypeValue switch
{
TokenType.AccessToken => GenerateAccessToken(userAccount),
TokenType.RefreshToken => GenerateRefreshToken(userAccount),
TokenType.ConfirmationToken => GenerateConfirmationToken(
userAccount
),
_ => throw new InvalidOperationException("Invalid token type"),
};
}
else
{
throw new InvalidOperationException("Invalid token type");
}
}
}