From 9aa92a4a03af5193415ea5dc8368ab4cd654a553 Mon Sep 17 00:00:00 2001 From: emrecoskun705 Date: Mon, 18 Sep 2023 21:10:35 +0300 Subject: [PATCH] add new exceptions --- .../Exceptions/AccountLockoutException.cs | 4 ---- .../EmailAlreadyConfirmedException.cs | 4 ---- ...eption.cs => EmailOtpNotFoundException.cs} | 2 +- .../InvalidAccountCredentialsException.cs | 4 ---- .../Exceptions/JwtTokenException.cs | 4 ---- .../Exceptions/UserNotFoundException.cs | 9 --------- Unitagram.Identity/Services/AuthService.cs | 20 +++++++++---------- .../Services/EmailVerificationService.cs | 2 +- 8 files changed, 12 insertions(+), 37 deletions(-) rename Unitagram.Application/Exceptions/EmailVerification/{CreateNewConfirmationCodeException.cs => EmailOtpNotFoundException.cs} (52%) diff --git a/Unitagram.Application/Exceptions/AccountLockoutException.cs b/Unitagram.Application/Exceptions/AccountLockoutException.cs index e25065e..294a373 100644 --- a/Unitagram.Application/Exceptions/AccountLockoutException.cs +++ b/Unitagram.Application/Exceptions/AccountLockoutException.cs @@ -2,8 +2,4 @@ namespace Unitagram.Application.Exceptions; public class AccountLockoutException : Exception { - public AccountLockoutException(string message) : base(message) - { - - } } \ No newline at end of file diff --git a/Unitagram.Application/Exceptions/EmailAlreadyConfirmedException.cs b/Unitagram.Application/Exceptions/EmailAlreadyConfirmedException.cs index 6beb058..2bf2441 100644 --- a/Unitagram.Application/Exceptions/EmailAlreadyConfirmedException.cs +++ b/Unitagram.Application/Exceptions/EmailAlreadyConfirmedException.cs @@ -2,8 +2,4 @@ namespace Unitagram.Application.Exceptions; public class EmailAlreadyConfirmedException : Exception { - public EmailAlreadyConfirmedException() : base("Email is already confirmed.") - { - - } } \ No newline at end of file diff --git a/Unitagram.Application/Exceptions/EmailVerification/CreateNewConfirmationCodeException.cs b/Unitagram.Application/Exceptions/EmailVerification/EmailOtpNotFoundException.cs similarity index 52% rename from Unitagram.Application/Exceptions/EmailVerification/CreateNewConfirmationCodeException.cs rename to Unitagram.Application/Exceptions/EmailVerification/EmailOtpNotFoundException.cs index fe691c3..5c19eec 100644 --- a/Unitagram.Application/Exceptions/EmailVerification/CreateNewConfirmationCodeException.cs +++ b/Unitagram.Application/Exceptions/EmailVerification/EmailOtpNotFoundException.cs @@ -1,5 +1,5 @@ namespace Unitagram.Application.Exceptions.EmailVerification; -public class CreateNewConfirmationCodeException : Exception +public class EmailOtpNotFoundException : Exception { } \ No newline at end of file diff --git a/Unitagram.Application/Exceptions/InvalidAccountCredentialsException.cs b/Unitagram.Application/Exceptions/InvalidAccountCredentialsException.cs index 086715b..ef3ef23 100644 --- a/Unitagram.Application/Exceptions/InvalidAccountCredentialsException.cs +++ b/Unitagram.Application/Exceptions/InvalidAccountCredentialsException.cs @@ -2,8 +2,4 @@ namespace Unitagram.Application.Exceptions; public class InvalidAccountCredentialsException : Exception { - public InvalidAccountCredentialsException(string message) : base(message) - { - - } } \ No newline at end of file diff --git a/Unitagram.Application/Exceptions/JwtTokenException.cs b/Unitagram.Application/Exceptions/JwtTokenException.cs index 7ea7949..0de4ae8 100644 --- a/Unitagram.Application/Exceptions/JwtTokenException.cs +++ b/Unitagram.Application/Exceptions/JwtTokenException.cs @@ -2,8 +2,4 @@ namespace Unitagram.Application.Exceptions; public class JwtTokenException : Exception { - public JwtTokenException(string message) : base(message) - { - - } } \ No newline at end of file diff --git a/Unitagram.Application/Exceptions/UserNotFoundException.cs b/Unitagram.Application/Exceptions/UserNotFoundException.cs index 5bdbc9c..530c0c6 100644 --- a/Unitagram.Application/Exceptions/UserNotFoundException.cs +++ b/Unitagram.Application/Exceptions/UserNotFoundException.cs @@ -2,13 +2,4 @@ namespace Unitagram.Application.Exceptions; public class UserNotFoundException : Exception { - public UserNotFoundException() : base("User not found") - { - - } - - public UserNotFoundException(string value) : base($"User ({value}) was not found") - { - - } } \ No newline at end of file diff --git a/Unitagram.Identity/Services/AuthService.cs b/Unitagram.Identity/Services/AuthService.cs index 041c745..18927b3 100644 --- a/Unitagram.Identity/Services/AuthService.cs +++ b/Unitagram.Identity/Services/AuthService.cs @@ -62,7 +62,7 @@ public async Task> Login(AuthRequest request) if (user == null) { - var notFoundException = new UserNotFoundException(request.UserName); + var notFoundException = new UserNotFoundException(); return new Result(notFoundException); } @@ -71,7 +71,7 @@ public async Task> Login(AuthRequest request) var lockoutEndDate = await _userManager.GetLockoutEndDateAsync(user); if (lockoutEndDate >= DateTimeOffset.UtcNow) { - var lockoutException = new AccountLockoutException("Account locked out. Try again later."); + var lockoutException = new AccountLockoutException(); return new Result(lockoutException); } @@ -90,11 +90,11 @@ public async Task> Login(AuthRequest request) { await _userManager.SetLockoutEndDateAsync(user, DateTimeOffset.UtcNow.Add(_userManager.Options.Lockout.DefaultLockoutTimeSpan)); - var lockoutException = new AccountLockoutException("Account locked out. Try again later."); + var lockoutException = new AccountLockoutException(); return new Result(lockoutException); } - var badRequestException = new InvalidAccountCredentialsException($"Credentials for '{request.UserName} aren't valid'."); + var badRequestException = new InvalidAccountCredentialsException(); return new Result(badRequestException); } @@ -189,7 +189,7 @@ public async Task> ConfirmEmail(EmailVerificat if (username == null) { - var exception = new UserNotFoundException("token"); + var exception = new UserNotFoundException(); return new Result(exception); } @@ -217,14 +217,14 @@ public async Task> GenerateOtpEmail(GenerateOtpReque if (username == null) { - var exception = new UserNotFoundException("token"); + var exception = new UserNotFoundException(); return new Result(exception); } var user = await _userManager.FindByNameAsync(username); if (user == null) { - var exception = new UserNotFoundException(username); + var exception = new UserNotFoundException(); return new Result(exception); } @@ -266,7 +266,7 @@ public async Task> RefreshToken(RefreshRequest request) string? username = principal.FindFirstValue(ClaimTypes.NameIdentifier); if (username == null) { - var exception = new JwtTokenException("Invalid access token"); + var exception = new JwtTokenException(); return new Result(exception); } @@ -274,7 +274,7 @@ public async Task> RefreshToken(RefreshRequest request) if (user is null) { - var exception = new UserNotFoundException(username); + var exception = new UserNotFoundException(); return new Result(exception); } @@ -282,7 +282,7 @@ public async Task> RefreshToken(RefreshRequest request) user.RefreshTokenExpirationDateTime <= DateTime.Now; if (isValidRefreshToken) { - var exception = new JwtTokenException("Invalid refresh token"); + var exception = new JwtTokenException(); return new Result(exception); } diff --git a/Unitagram.Identity/Services/EmailVerificationService.cs b/Unitagram.Identity/Services/EmailVerificationService.cs index 1422f56..321f0b8 100644 --- a/Unitagram.Identity/Services/EmailVerificationService.cs +++ b/Unitagram.Identity/Services/EmailVerificationService.cs @@ -80,7 +80,7 @@ public async Task> ValidateAsync(Guid userId, string token) if (otpConfirmation is null || IsRetryTimeElapsed(otpConfirmation)) { - var exception = new CreateNewConfirmationCodeException(); + var exception = new EmailOtpNotFoundException(); return new Result(exception); }