1
This commit is contained in:
@@ -1,10 +0,0 @@
|
||||
namespace Yes.Blog.Endpoints.Admins
|
||||
{
|
||||
public class AdminEndpointScheme
|
||||
{
|
||||
public int Priority { get; set; } = 3;
|
||||
public string Prefix { get; set; } = "admin/api";
|
||||
|
||||
public string[] Tags { get; set; } = new[] { "Blog" };
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
namespace Yes.Blog.Endpoints.Admins.Auths
|
||||
using Yes.Infrastructure.Authorizations;
|
||||
|
||||
namespace Yes.Blog.Endpoints.Admins.Auths
|
||||
{
|
||||
public class UserLoginEndpoint : AuthEndpointScheme, IEndpoint
|
||||
{
|
||||
|
||||
public void Map(IEndpointRouteBuilder app) => app.MapPost("/login", Handle).WithRequestValidation<Request>();
|
||||
public void Map(IEndpointRouteBuilder app) => app.MapPost("/login", Handle).WithRequestValidation<Request>().AllowAnonymous();
|
||||
|
||||
public record Request(string Name, string Password);
|
||||
|
||||
@@ -21,7 +23,7 @@
|
||||
var command = mapper.Map<UserLoginCommand>(request);
|
||||
var response = await mediator.Send(command, cancellationToken);
|
||||
|
||||
await context.SignIn(response.Identity);
|
||||
await context.SignIn(response.Identity, response.TokenLifetimeMinutes);
|
||||
|
||||
return Result.Ok(response);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
public class UserLogoutEndpoint : AuthEndpointScheme, IEndpoint
|
||||
{
|
||||
|
||||
public void Map(IEndpointRouteBuilder app) => app.MapPost("/logout", Handle);
|
||||
public void Map(IEndpointRouteBuilder app) => app.MapPost("/logout", Handle).AllowAnonymous();
|
||||
|
||||
|
||||
private async Task<IResult> Handle(IMediator mediator, IMapper mapper, HttpContext context, CancellationToken cancellationToken)
|
||||
|
||||
34
Yes.Blog/Endpoints/Admins/Auths/ValidateEndpoint.cs
Normal file
34
Yes.Blog/Endpoints/Admins/Auths/ValidateEndpoint.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using Yes.Infrastructure.Authorizations;
|
||||
|
||||
namespace Yes.Blog.Endpoints.Admins.Auths
|
||||
{
|
||||
public class ValidateEndpoint : AuthEndpointScheme, IEndpoint
|
||||
{
|
||||
public void Map(IEndpointRouteBuilder app) => app.MapPost("/validate", Handle).AllowAnonymous();
|
||||
|
||||
|
||||
private async Task<IResult> Handle([FromHeader] string authorization,
|
||||
IMediator mediator,
|
||||
IMapper mapper,
|
||||
HttpContext context,
|
||||
IOptionsMonitor<BlogSettings> options,CancellationToken cancellationToken)
|
||||
{
|
||||
var token = authorization?.Replace("Bearer ", "");
|
||||
|
||||
try
|
||||
{
|
||||
var command = new ValidateCommand(token);
|
||||
var response = await mediator.Send(command, cancellationToken);
|
||||
|
||||
await context.SignIn(response.Identity, response.TokenLifetimeMinutes);
|
||||
|
||||
|
||||
return Result.Ok(new { IsValid = true });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Ok(new { IsValid = false, Error = ex.Message });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user