Skip to content

Commit

Permalink
add email verification exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
emrecoskun705 committed Sep 17, 2023
1 parent 72bdbb2 commit 0ff2dca
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Unitagram.Application.Exceptions.EmailVerification;

public class CreateNewConfirmationCodeException : Exception
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Unitagram.Application.Exceptions.EmailVerification;

public class InvalidCodeException : Exception
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Globalization;

namespace Unitagram.Application.Exceptions.EmailVerification;

public class OtpCodeTryAgainLaterException : Exception
{
public OtpCodeTryAgainLaterException(int minutesDifference) : base(minutesDifference.ToString(CultureInfo.InvariantCulture))
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Unitagram.Application.Exceptions.EmailVerification;

public class ReachedMaximumCodeUsageException : Exception
{
}
4 changes: 4 additions & 0 deletions Unitagram.Application/Unitagram.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@










Expand Down
14 changes: 7 additions & 7 deletions Unitagram.Identity/Services/EmailVerificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Unitagram.Application.Contracts.Email;
using Unitagram.Application.Contracts.Identity;
using Unitagram.Application.Contracts.Persistence;
using Unitagram.Application.Exceptions;
using Unitagram.Application.Exceptions.EmailVerification;
using Unitagram.Application.Models.Email;
using Unitagram.Application.Models.Identity.OTP;
using Unitagram.Domain;
Expand Down Expand Up @@ -66,7 +66,7 @@ public async Task<Result<Unit>> GenerateAsync(Guid userId)
}

var minutesDifference = CalculateMinutesDifference(otpConfirmation.RetryDateTimeUtc!.Value);
var exception = new BadRequestException($"Please try again after {minutesDifference} minutes later");
var exception = new OtpCodeTryAgainLaterException(minutesDifference);
return new Result<Unit>(exception);
}

Expand All @@ -80,14 +80,14 @@ public async Task<Result<bool>> ValidateAsync(Guid userId, string token)

if (otpConfirmation is null || IsRetryTimeElapsed(otpConfirmation))
{
var exception = new BadRequestException("Please create new confirmation code");
var exception = new CreateNewConfirmationCodeException();
return new Result<bool>(exception);
}

// check max retry count is passed
if (otpConfirmation.RetryCount > maxRetryCount)
{
var exception = new BadRequestException("You've exceeded the maximum code usage");
var exception = new ReachedMaximumCodeUsageException();
return new Result<bool>(exception);
}

Expand All @@ -96,7 +96,7 @@ public async Task<Result<bool>> ValidateAsync(Guid userId, string token)
{
otpConfirmation.RetryCount++;
await _otpConfirmationRepository.UpdateAsync(otpConfirmation);
var exception = new BadRequestException("Invalid code");
var exception = new InvalidCodeException();
return new Result<bool>(exception);
}

Expand Down Expand Up @@ -128,11 +128,11 @@ private bool IsRetryTimeElapsed(OtpConfirmation otpConfirmation)
return (otpConfirmation.RetryDateTimeUtc.HasValue && now >= otpConfirmation.RetryDateTimeUtc.Value);
}

private double CalculateMinutesDifference(DateTimeOffset retryDateTime)
private int CalculateMinutesDifference(DateTimeOffset retryDateTime)
{
var now = DateTimeOffset.UtcNow;
var timeDifference = retryDateTime - now;
return timeDifference.TotalMinutes + 1;
return (int)(timeDifference.TotalMinutes) + 1;
}

private async Task CreateOtpConfirmation(Guid userId, string purpose, string token)
Expand Down
6 changes: 3 additions & 3 deletions Unitagram.WebAPI/Controllers/ControllerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ private static ActionResult<TResult> HandleException<TResult>(Exception exceptio
{ typeof(ValidationException), StatusCodes.Status400BadRequest },
{ typeof(BadRequestException), StatusCodes.Status400BadRequest },
{ typeof(InvalidAccountCredentialsException), StatusCodes.Status400BadRequest },
{ typeof(EmailAlreadyConfirmedException), StatusCodes.Status400BadRequest },
{ typeof(AccountLockoutException), StatusCodes.Status403Forbidden },
{ typeof(NotFoundException), StatusCodes.Status404NotFound },
{ typeof(UserNotFoundException), StatusCodes.Status404NotFound },
{ typeof(EmailAlreadyConfirmedException), StatusCodes.Status400BadRequest },
};

// Get the status code from the dictionary, defaulting to 500 if not found
var statusCode = statusCodeMap.TryGetValue(exception.GetType(), out var code) ? code : 500;
// Get the status code from the dictionary, defaulting to 400 if not found
var statusCode = statusCodeMap.TryGetValue(exception.GetType(), out var code) ? code : 400;

var problemDetails = new ProblemDetails
{
Expand Down

0 comments on commit 0ff2dca

Please sign in to comment.