Skip to content

Commit

Permalink
add new exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
emrecoskun705 committed Sep 18, 2023
1 parent 0ff2dca commit 9aa92a4
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 37 deletions.
4 changes: 0 additions & 4 deletions Unitagram.Application/Exceptions/AccountLockoutException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ namespace Unitagram.Application.Exceptions;

public class AccountLockoutException : Exception
{
public AccountLockoutException(string message) : base(message)
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ namespace Unitagram.Application.Exceptions;

public class EmailAlreadyConfirmedException : Exception
{
public EmailAlreadyConfirmedException() : base("Email is already confirmed.")
{

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Unitagram.Application.Exceptions.EmailVerification;

public class CreateNewConfirmationCodeException : Exception
public class EmailOtpNotFoundException : Exception
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ namespace Unitagram.Application.Exceptions;

public class InvalidAccountCredentialsException : Exception
{
public InvalidAccountCredentialsException(string message) : base(message)
{

}
}
4 changes: 0 additions & 4 deletions Unitagram.Application/Exceptions/JwtTokenException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ namespace Unitagram.Application.Exceptions;

public class JwtTokenException : Exception
{
public JwtTokenException(string message) : base(message)
{

}
}
9 changes: 0 additions & 9 deletions Unitagram.Application/Exceptions/UserNotFoundException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
{

}
}
20 changes: 10 additions & 10 deletions Unitagram.Identity/Services/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public async Task<Result<AuthResponse>> Login(AuthRequest request)

if (user == null)
{
var notFoundException = new UserNotFoundException(request.UserName);
var notFoundException = new UserNotFoundException();
return new Result<AuthResponse>(notFoundException);
}

Expand All @@ -71,7 +71,7 @@ public async Task<Result<AuthResponse>> 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<AuthResponse>(lockoutException);
}

Expand All @@ -90,11 +90,11 @@ public async Task<Result<AuthResponse>> 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<AuthResponse>(lockoutException);
}

var badRequestException = new InvalidAccountCredentialsException($"Credentials for '{request.UserName} aren't valid'.");
var badRequestException = new InvalidAccountCredentialsException();
return new Result<AuthResponse>(badRequestException);
}

Expand Down Expand Up @@ -189,7 +189,7 @@ public async Task<Result<EmailVerificationResponse>> ConfirmEmail(EmailVerificat

if (username == null)
{
var exception = new UserNotFoundException("token");
var exception = new UserNotFoundException();
return new Result<EmailVerificationResponse>(exception);
}

Expand Down Expand Up @@ -217,14 +217,14 @@ public async Task<Result<GenerateOtpResponse>> GenerateOtpEmail(GenerateOtpReque

if (username == null)
{
var exception = new UserNotFoundException("token");
var exception = new UserNotFoundException();
return new Result<GenerateOtpResponse>(exception);
}

var user = await _userManager.FindByNameAsync(username);
if (user == null)
{
var exception = new UserNotFoundException(username);
var exception = new UserNotFoundException();
return new Result<GenerateOtpResponse>(exception);
}

Expand Down Expand Up @@ -266,23 +266,23 @@ public async Task<Result<AuthResponse>> 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<AuthResponse>(exception);
}

ApplicationUser? user = await _userManager.FindByNameAsync(username);

if (user is null)
{
var exception = new UserNotFoundException(username);
var exception = new UserNotFoundException();
return new Result<AuthResponse>(exception);
}

bool isValidRefreshToken = user.RefreshToken != request.RefreshToken ||
user.RefreshTokenExpirationDateTime <= DateTime.Now;
if (isValidRefreshToken)
{
var exception = new JwtTokenException("Invalid refresh token");
var exception = new JwtTokenException();
return new Result<AuthResponse>(exception);
}

Expand Down
2 changes: 1 addition & 1 deletion Unitagram.Identity/Services/EmailVerificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public async Task<Result<bool>> ValidateAsync(Guid userId, string token)

if (otpConfirmation is null || IsRetryTimeElapsed(otpConfirmation))
{
var exception = new CreateNewConfirmationCodeException();
var exception = new EmailOtpNotFoundException();
return new Result<bool>(exception);
}

Expand Down

0 comments on commit 9aa92a4

Please sign in to comment.