mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-04-06 02:19:05 +00:00
Refactor authentication services and implement JWT validation logic
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user