59 lines
1.6 KiB
C#
59 lines
1.6 KiB
C#
using Hutopy.Modules.Identity.Data;
|
|
using Hutopy.Modules.Identity.Services;
|
|
|
|
namespace Hutopy.Modules.Identity.Handlers;
|
|
|
|
[PublicAPI]
|
|
public record ResendVerificationRequest(
|
|
string Email);
|
|
|
|
[PublicAPI]
|
|
public record ResendVerificationResponse(
|
|
string Message);
|
|
|
|
[PublicAPI]
|
|
public class ResendVerificationHandler(
|
|
EmailVerificationService emailWriter,
|
|
UserManager userManager)
|
|
: Endpoint<ResendVerificationRequest, ResendVerificationResponse>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
AllowAnonymous();
|
|
Post("/api/users/resend-verification");
|
|
Options(o => o.WithTags("Users"));
|
|
}
|
|
|
|
public override async Task HandleAsync(
|
|
ResendVerificationRequest request,
|
|
CancellationToken ct)
|
|
{
|
|
// Find a user by email
|
|
User? user = await userManager.FindByEmailAsync(request.Email);
|
|
if (user is null)
|
|
{
|
|
// Don't reveal that the user doesn't exist
|
|
await SendOkAsync(
|
|
new ResendVerificationResponse(
|
|
"If your email exists in our system, a verification link has been sent."),
|
|
ct);
|
|
return;
|
|
}
|
|
|
|
// Check if the email is already confirmed
|
|
if (user.EmailConfirmed)
|
|
{
|
|
await SendOkAsync(
|
|
new ResendVerificationResponse("Your email is already verified. You can log in."),
|
|
ct);
|
|
return;
|
|
}
|
|
|
|
await emailWriter.SendVerificationEmailAsync(user);
|
|
|
|
await SendOkAsync(
|
|
new ResendVerificationResponse("If your email exists in our system, a verification link has been sent."),
|
|
ct);
|
|
}
|
|
}
|