45 lines
1.8 KiB
C#
45 lines
1.8 KiB
C#
using Knot.Modules.Auth.Application.Users.GetMe;
|
|
using Knot.Modules.Auth.Application.Users.Login;
|
|
using Knot.Modules.Auth.Application.Users.RefreshToken;
|
|
using Knot.Modules.Auth.Application.Users.Register;
|
|
using Knot.Shared.Kernel;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Routing;
|
|
|
|
namespace Knot.Modules.Auth.Presentation.Endpoints;
|
|
|
|
public static class AuthEndpoints
|
|
{
|
|
public static void MapAuthEndpoints(this WebApplication app)
|
|
{
|
|
var group = app.MapGroup("api/auth");
|
|
|
|
group.MapPost("register", async ([FromBody] RegisterUserCommand command, ISender sender, CancellationToken ct) =>
|
|
{
|
|
var result = await sender.Send(command, ct);
|
|
return result.IsSuccess ? Results.Ok(result.Value) : Results.BadRequest(new { error = result.Error.Code ?? result.Error.Description });
|
|
});
|
|
|
|
group.MapPost("login", async ([FromBody] LoginUserCommand command, ISender sender, CancellationToken ct) =>
|
|
{
|
|
var result = await sender.Send(command, ct);
|
|
return result.IsSuccess ? Results.Ok(result.Value) : Results.Unauthorized();
|
|
});
|
|
|
|
group.MapPost("refresh", async ([FromBody] RefreshTokenCommand command, ISender sender, CancellationToken ct) =>
|
|
{
|
|
var result = await sender.Send(command, ct);
|
|
return result.IsSuccess ? Results.Ok(result.Value) : Results.Unauthorized();
|
|
});
|
|
|
|
group.MapGet("me", async (ISender sender, IUserContext userContext, CancellationToken ct) =>
|
|
{
|
|
var result = await sender.Send(new GetMeQuery(userContext.UserId), ct);
|
|
return result.IsSuccess ? Results.Ok(result.Value) : Results.NotFound();
|
|
}).RequireAuthorization();
|
|
}
|
|
}
|