49 lines
1.2 KiB
C#
49 lines
1.2 KiB
C#
using Socialize.Infrastructure.Security;
|
|
using Socialize.Modules.Identity.Data;
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
namespace Socialize.Modules.Identity.Handlers;
|
|
|
|
[PublicAPI]
|
|
public record ChangePhoneRequest(
|
|
string? PhoneNumber);
|
|
|
|
[PublicAPI]
|
|
public class ChangePhoneHandler(
|
|
UserManager userManager)
|
|
: Endpoint<ChangePhoneRequest>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Post("/api/users/phone");
|
|
Options(o => o.WithTags("Users"));
|
|
}
|
|
|
|
public override async Task HandleAsync(
|
|
ChangePhoneRequest request,
|
|
CancellationToken ct)
|
|
{
|
|
User? user = await userManager.FindByIdAsync(HttpContext.User.GetUserId().ToString());
|
|
|
|
if (user is null)
|
|
{
|
|
await SendNotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
user.PhoneNumber = request.PhoneNumber;
|
|
// TODO: check to see if identity resets the `phone confirmed` flag - @jonathan
|
|
|
|
IdentityResult result = await userManager.UpdateAsync(user);
|
|
|
|
if (result.Succeeded)
|
|
{
|
|
await SendOkAsync(ct);
|
|
}
|
|
else
|
|
{
|
|
await SendUnauthorizedAsync(ct);
|
|
}
|
|
}
|
|
}
|