-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement resend code functionality in signup verification
- Loading branch information
Showing
10 changed files
with
193 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
application/account-management/Core/Features/Signups/Commands/ResendSignupCode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using JetBrains.Annotations; | ||
using Microsoft.AspNetCore.Identity; | ||
using PlatformPlatform.AccountManagement.Features.Signups.Domain; | ||
using PlatformPlatform.SharedKernel.Authentication; | ||
using PlatformPlatform.SharedKernel.Cqrs; | ||
using PlatformPlatform.SharedKernel.Integrations.Email; | ||
using PlatformPlatform.SharedKernel.Telemetry; | ||
|
||
namespace PlatformPlatform.AccountManagement.Features.Signups.Commands; | ||
|
||
[PublicAPI] | ||
public sealed record ResendSignupCodeCommand(SignupId Id) : ICommand, IRequest<Result<ResendSignupCodeResponse>>; | ||
|
||
[PublicAPI] | ||
public record ResendSignupCodeResponse(int ValidForSeconds); | ||
|
||
public sealed class ResendSignupCodeCommandHandler( | ||
ISignupRepository signupRepository, | ||
IEmailClient emailClient, | ||
IPasswordHasher<object> passwordHasher, | ||
ITelemetryEventsCollector events | ||
) : IRequestHandler<ResendSignupCodeCommand, Result<ResendSignupCodeResponse>> | ||
{ | ||
public async Task<Result<ResendSignupCodeResponse>> Handle(ResendSignupCodeCommand command, CancellationToken cancellationToken) | ||
{ | ||
var signup = await signupRepository.GetByIdAsync(command.Id, cancellationToken); | ||
if (signup is null) | ||
{ | ||
return Result<ResendSignupCodeResponse>.NotFound($"Signup with id '{command.Id}' not found."); | ||
} | ||
|
||
if (signup.ModifiedAt > TimeProvider.System.GetUtcNow().AddSeconds(-30)) | ||
{ | ||
return Result<ResendSignupCodeResponse>.BadRequest("You must wait at least 30 seconds before requesting a new code."); | ||
} | ||
|
||
// Generate new verification code and update expiration | ||
var oneTimePassword = OneTimePasswordHelper.GenerateOneTimePassword(6); | ||
var oneTimePasswordHash = passwordHasher.HashPassword(this, oneTimePassword); | ||
signup.UpdateVerificationCode(oneTimePasswordHash); | ||
signupRepository.Update(signup); | ||
|
||
await emailClient.SendAsync(signup.Email, "Confirm your email address", | ||
$""" | ||
<h1 style="text-align:center;font-family=sans-serif;font-size:20px">Your confirmation code is below</h1> | ||
<p style="text-align:center;font-family=sans-serif;font-size:16px">Enter it in your open browser window. It is only valid for a few minutes.</p> | ||
<p style="text-align:center;font-family=sans-serif;font-size:40px;background:#f5f4f5">{oneTimePassword}</p> | ||
""", | ||
cancellationToken | ||
); | ||
|
||
var secondsSinceSignupStarted = (TimeProvider.System.GetUtcNow() - signup.CreatedAt).TotalSeconds; | ||
|
||
events.CollectEvent(new SignupCodeResend(signup.TenantId, (int)secondsSinceSignupStarted)); | ||
|
||
return new ResendSignupCodeResponse(Signup.ValidForSeconds); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters