Removed Immediate.Apis, Added AOT Testing Scripts.
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Htmx.ApiDemo.Templates;
|
||||
using Microsoft.AspNetCore.Antiforgery;
|
||||
using Htmx.ApiDemo.Data;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Htmx.ApiDemo;
|
||||
|
||||
public static partial class RouteMap
|
||||
{
|
||||
private static void GetRegister(WebApplication app)
|
||||
=> app.MapGet("/register", (IHttpContextAccessor contextAccessor, IAntiforgery antiforgery) =>
|
||||
{
|
||||
var context = contextAccessor.HttpContext
|
||||
?? throw new InvalidOperationException("HttpContext is not available.");
|
||||
|
||||
if (context.User.Identity?.IsAuthenticated == true)
|
||||
{
|
||||
context.Response.Redirect("/");
|
||||
return;
|
||||
}
|
||||
|
||||
var afTokens = antiforgery.GetAndStoreTokens(context);
|
||||
var registerComponent = new Register(afToken: afTokens.RequestToken);
|
||||
registerComponent.HtmxAwareWriteToBody(
|
||||
context: context,
|
||||
title: "Register",
|
||||
appName: "HtmxApp",
|
||||
pageTitle: "Create account"
|
||||
);
|
||||
});
|
||||
|
||||
private static void PostRegister(WebApplication app)
|
||||
=> app.MapPost("/register", async ValueTask
|
||||
(
|
||||
[FromForm] string email,
|
||||
[FromForm] string password,
|
||||
[FromForm] string confirmPassword,
|
||||
[FromForm] string? displayName,
|
||||
[FromServices] IHttpContextAccessor httpContextAccessor,
|
||||
[FromServices] IAntiforgery antiforgery,
|
||||
[FromServices] AppAuthService authService
|
||||
) =>
|
||||
{
|
||||
var context = httpContextAccessor.HttpContext
|
||||
?? throw new InvalidOperationException("HttpContext is not available.");
|
||||
|
||||
var afToken = antiforgery.GetAndStoreTokens(context).RequestToken;
|
||||
|
||||
if (password != confirmPassword)
|
||||
{
|
||||
var errorComponent = new Register("Passwords do not match.", afToken: afToken);
|
||||
errorComponent.HtmxAwareWriteToBody(
|
||||
context: context,
|
||||
title: "Register",
|
||||
appName: "HtmxApp",
|
||||
pageTitle: "Create account"
|
||||
);
|
||||
}
|
||||
|
||||
var (success, error) = await authService.RegisterAsync(email, password, displayName);
|
||||
|
||||
if (success)
|
||||
{
|
||||
context.Response.Redirect("/");
|
||||
return;
|
||||
}
|
||||
|
||||
var registerComponent = new Register(error, afToken: afToken);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user