From 8c355b82df02bc650c5ba101d838121f485e8581 Mon Sep 17 00:00:00 2001 From: ivar Date: Thu, 4 Dec 2025 23:30:39 +0100 Subject: Improve login experience --- api/WhatApi/Endpoints/GetLoginPageEndpoint.cs | 6 ++-- api/WhatApi/Endpoints/GetMapPageEndpoint.cs | 2 +- api/WhatApi/Endpoints/GetUploadPageEndpoint.cs | 2 +- api/WhatApi/Endpoints/LoginEndpoint.cs | 4 +-- api/WhatApi/Templates/TemplateFulfiller.cs | 28 ++++++++++++----- api/WhatApi/Templates/web_login.liquid | 43 ++++++++++++++++++++++++-- 6 files changed, 68 insertions(+), 17 deletions(-) (limited to 'api/WhatApi') diff --git a/api/WhatApi/Endpoints/GetLoginPageEndpoint.cs b/api/WhatApi/Endpoints/GetLoginPageEndpoint.cs index dd17669..8b07a0e 100644 --- a/api/WhatApi/Endpoints/GetLoginPageEndpoint.cs +++ b/api/WhatApi/Endpoints/GetLoginPageEndpoint.cs @@ -6,7 +6,9 @@ public class GetLoginPageEndpoint : BaseEndpoint { [AllowAnonymous] [HttpGet("~/login")] - public ActionResult Handle() { - return Content(TemplateFulfiller.WebLoginPage(), "text/html"); + public ActionResult Handle(string? error = null) { + return Content(TemplateFulfiller.WebLoginPage(new TemplateFulfiller.WebLoginModel() { + Error = error ?? string.Empty + }), "text/html"); } } \ No newline at end of file diff --git a/api/WhatApi/Endpoints/GetMapPageEndpoint.cs b/api/WhatApi/Endpoints/GetMapPageEndpoint.cs index 833a98c..bafad6a 100644 --- a/api/WhatApi/Endpoints/GetMapPageEndpoint.cs +++ b/api/WhatApi/Endpoints/GetMapPageEndpoint.cs @@ -6,7 +6,7 @@ public class GetMapPageEndpoint : BaseEndpoint { [HttpGet("~/map")] - public ActionResult GetMapPage() { + public ActionResult Handle() { return Content(TemplateFulfiller.WebMapPage(), "text/html"); } } \ No newline at end of file diff --git a/api/WhatApi/Endpoints/GetUploadPageEndpoint.cs b/api/WhatApi/Endpoints/GetUploadPageEndpoint.cs index ea14819..309fa49 100644 --- a/api/WhatApi/Endpoints/GetUploadPageEndpoint.cs +++ b/api/WhatApi/Endpoints/GetUploadPageEndpoint.cs @@ -6,7 +6,7 @@ public class GetUploadPageEndpoint : BaseEndpoint { [HttpGet("~/upload")] - public ActionResult GetMapPage() { + public ActionResult Handle() { return Content(TemplateFulfiller.WebUploadPage(), "text/html"); } } \ No newline at end of file diff --git a/api/WhatApi/Endpoints/LoginEndpoint.cs b/api/WhatApi/Endpoints/LoginEndpoint.cs index cb76696..470ef34 100644 --- a/api/WhatApi/Endpoints/LoginEndpoint.cs +++ b/api/WhatApi/Endpoints/LoginEndpoint.cs @@ -17,10 +17,10 @@ public class LoginEndpoint(AppDatabase db, IConfiguration configuration) : BaseE [HttpPost("~/login")] public async Task HandleAsync([FromForm] LoginRequest login, CancellationToken ct = default) { var user = await db.Users.FirstOrDefaultAsync(c => c.Name == login.Username, ct); - if (user?.PasswordHash is null) return Unauthorized(); + if (user?.PasswordHash is null) return Redirect("/login?error=Ukjent bruker/passord"); var verificationResult = PasswordHasher.VerifyHashedPassword(user.PasswordHash, login.Password); - if (verificationResult == PasswordVerificationResult.Failed) return Unauthorized(); + if (verificationResult == PasswordVerificationResult.Failed) return Redirect("/login?error=Ukjent bruker/passord"); var tokenEntropy = configuration.GetValue(Constants.Env.TokenEntropy); diff --git a/api/WhatApi/Templates/TemplateFulfiller.cs b/api/WhatApi/Templates/TemplateFulfiller.cs index 3433701..e24e77d 100644 --- a/api/WhatApi/Templates/TemplateFulfiller.cs +++ b/api/WhatApi/Templates/TemplateFulfiller.cs @@ -5,25 +5,37 @@ namespace WhatApi.Templates; public class TemplateFulfiller { private static readonly FluidParser Parser = new(); + private static readonly TemplateContext EmptyContext = new(); private static readonly string TemplateDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Templates"); private static string WebMapTemplate => File.ReadAllText(Path.Combine(TemplateDirectory, "web_map.liquid")); private static string WebUploadTemplate => File.ReadAllText(Path.Combine(TemplateDirectory, "web_upload.liquid")); private static string WebLoginTemplate => File.ReadAllText(Path.Combine(TemplateDirectory, "web_login.liquid")); + private static string WebTermsTemplate => File.ReadAllText(Path.Combine(TemplateDirectory, "web_terms.liquid")); - public static string WebMapPage(object? data = null) { + + public static string WebMapPage() { Parser.TryParse(WebMapTemplate, out var template); - var context = data is null ? new TemplateContext() : new TemplateContext(data); - return template.Render(context); + return template.Render(EmptyContext); } - public static string WebUploadPage(object? data = null) { + public static string WebUploadPage() { Parser.TryParse(WebUploadTemplate, out var template); - var context = data is null ? new TemplateContext() : new TemplateContext(data); - return template.Render(context); + return template.Render(EmptyContext); + } + + public class WebLoginModel + { + public string Error { get; set; } = string.Empty; } - public static string WebLoginPage(object? data = null) { + + public static string WebLoginPage(WebLoginModel model) { Parser.TryParse(WebLoginTemplate, out var template); - var context = data is null ? new TemplateContext() : new TemplateContext(data); + var context = new TemplateContext(model); return template.Render(context); } + + public static string WebTermsPage() { + Parser.TryParse(WebTermsTemplate, out var template); + return template.Render(EmptyContext); + } } \ No newline at end of file diff --git a/api/WhatApi/Templates/web_login.liquid b/api/WhatApi/Templates/web_login.liquid index c5de790..314c9ef 100644 --- a/api/WhatApi/Templates/web_login.liquid +++ b/api/WhatApi/Templates/web_login.liquid @@ -7,11 +7,17 @@ Logg inn @@ -57,19 +79,34 @@ autocomplete="off">
Logg inn + {% if Error != '' %} +

{{ Error }}

+ {% endif %} +
+ \ No newline at end of file -- cgit v1.3