using Hutopy.Infrastructure.Security; using Hutopy.Modules.Identity.Data; using Microsoft.AspNetCore.Identity; namespace Hutopy.Modules.Identity.Handlers; [PublicAPI] public record ChangeAliasRequest( string? Alias); [PublicAPI] public class ChangeAliasHandler( UserManager userManager) : Endpoint { public override void Configure() { Post("/api/users/alias"); Options(o => o.WithTags("Users")); } public override async Task HandleAsync( ChangeAliasRequest request, CancellationToken ct) { User? user = await userManager.FindByIdAsync(HttpContext.User.GetUserId().ToString()); if (user is null) { await SendNotFoundAsync(ct); return; } user.Alias = request.Alias; IdentityResult result = await userManager.UpdateAsync(user); if (result.Succeeded) { await SendOkAsync(ct); } else { await SendUnauthorizedAsync(ct); } } }