Refactor frontend entry to login and play routes
This commit is contained in:
22
RpgRoller/Api/FrontendEntryEndpoints.cs
Normal file
22
RpgRoller/Api/FrontendEntryEndpoints.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using RpgRoller.Services;
|
||||
|
||||
namespace RpgRoller.Api;
|
||||
|
||||
public static class FrontendEntryEndpoints
|
||||
{
|
||||
public static void MapFrontendEntryEndpoints(this IEndpointRouteBuilder app)
|
||||
{
|
||||
app.MapGet("/", RedirectRootRequest);
|
||||
}
|
||||
|
||||
private static RedirectHttpResult RedirectRootRequest(HttpContext context, IGameService game)
|
||||
{
|
||||
var redirectPath = context.TryReadSessionTokenFromCookie(out var sessionToken) &&
|
||||
game.GetUserBySession(sessionToken) is not null
|
||||
? "/play"
|
||||
: "/login";
|
||||
|
||||
return TypedResults.Redirect(context.Request.PathBase.Add(redirectPath).Value!);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,4 @@
|
||||
@using RpgRoller.Api
|
||||
@using RpgRoller.Components.Pages.HomeControls
|
||||
@using RpgRoller.Services
|
||||
@attribute [ExcludeFromCodeCoverage]
|
||||
|
||||
<!DOCTYPE html>
|
||||
@@ -38,32 +36,18 @@ else
|
||||
</html>
|
||||
|
||||
@code {
|
||||
[Inject] private IGameService GameService { get; set; } = null!;
|
||||
|
||||
[CascadingParameter] private HttpContext? HttpContext { get; set; }
|
||||
|
||||
private bool UseInteractiveApp => !UseStaticAuthPage;
|
||||
|
||||
private bool UseStaticAuthPage => IsRootRequest && !HasAuthenticatedSession;
|
||||
private bool UseStaticAuthPage => IsLoginRequest;
|
||||
|
||||
private bool IsRootRequest
|
||||
private bool IsLoginRequest
|
||||
{
|
||||
get
|
||||
{
|
||||
var path = HttpContext?.Request.Path.Value;
|
||||
return string.IsNullOrWhiteSpace(path) || string.Equals(path, "/", StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
|
||||
private bool HasAuthenticatedSession
|
||||
{
|
||||
get
|
||||
{
|
||||
if (HttpContext is null)
|
||||
return false;
|
||||
|
||||
var sessionToken = HttpContext.Request.Cookies[SessionCookie.Name];
|
||||
return !string.IsNullOrWhiteSpace(sessionToken) && GameService.GetUserBySession(sessionToken) is not null;
|
||||
return string.Equals(path, "/login", StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +69,7 @@ else
|
||||
|
||||
private string? ReadAuthQueryValue(string key)
|
||||
{
|
||||
if (!UseStaticAuthPage || HttpContext is null)
|
||||
if (!IsLoginRequest || HttpContext is null)
|
||||
return null;
|
||||
|
||||
var value = HttpContext.Request.Query[key];
|
||||
|
||||
1
RpgRoller/Components/Pages/LoginPage.razor
Normal file
1
RpgRoller/Components/Pages/LoginPage.razor
Normal file
@@ -0,0 +1 @@
|
||||
@page "/login"
|
||||
@@ -1,2 +1,2 @@
|
||||
@page "/"
|
||||
@page "/play"
|
||||
<Workspace LoggedOut="OnLoggedOutAsync"/>
|
||||
@@ -5,13 +5,13 @@ using Microsoft.AspNetCore.WebUtilities;
|
||||
namespace RpgRoller.Components.Pages;
|
||||
|
||||
[ExcludeFromCodeCoverage]
|
||||
public partial class Home
|
||||
public partial class PlayPage
|
||||
{
|
||||
private Task OnLoggedOutAsync(string? message)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(message))
|
||||
{
|
||||
Navigation.NavigateTo("/", forceLoad: true);
|
||||
Navigation.NavigateTo("/login", forceLoad: true);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ public partial class Home
|
||||
["kind"] = message.Contains("expired", StringComparison.OrdinalIgnoreCase) ? "error" : "success"
|
||||
};
|
||||
|
||||
Navigation.NavigateTo(QueryHelpers.AddQueryString("/", query), forceLoad: true);
|
||||
Navigation.NavigateTo(QueryHelpers.AddQueryString("/login", query), forceLoad: true);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ app.UseResponseCompression();
|
||||
app.UseAntiforgery();
|
||||
|
||||
app.MapRpgRollerApi();
|
||||
app.MapFrontendEntryEndpoints();
|
||||
app.MapStaticAssets();
|
||||
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
|
||||
app.Run();
|
||||
|
||||
@@ -366,7 +366,7 @@ window.rpgRollerApi = (() => {
|
||||
}
|
||||
|
||||
if (formType === "login") {
|
||||
window.location.assign(toAppUrl("/"));
|
||||
window.location.assign(toAppUrl("/play"));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user