web: registrace, ověření e-mailu, obnova hesla a profil

Stránky na cestách z e-mailových odkazů API (/confirm-email, /reset-password).
Token se spotřebuje až tlačítkem, ne otevřením odkazu (poštovní skenery).
Profil s úpravou jména, telefonu a e-mailových upozornění, odhlášení ze všech
zařízení. Chyby validace z API se zobrazují u polí formuláře.

Refs #21
This commit is contained in:
PatTheEvil committed 2026-12-02 00:31:07 +01:00
1 parent d3a1b1cb17
commit 3965a8a2a1
16 files changed
+566 -1

No files matched your search

@@ -10,7 +10,10 @@ using Microsoft.AspNetCore.Mvc;
namespace Helpdesk.Web.Controllers;
/// <summary>Přihlášení a odhlášení. Stránky pro nepřihlášené jsou označené <see cref="AllowAnonymousAttribute"/>.</summary>
/// <summary>
/// Přihlášení, registrace, ověření e-mailu a obnova hesla (FR-01 – FR-03).
/// Stránky pro nepřihlášené jsou označené <see cref="AllowAnonymousAttribute"/>.
/// </summary>
public sealed class AccountController(AuthApiClient authApi, HelpdeskApiClient api) : Controller
{
[AllowAnonymous]
@@ -48,6 +51,112 @@ public sealed class AccountController(AuthApiClient authApi, HelpdeskApiClient a
}
}
[AllowAnonymous]
[HttpGet("register")]
public IActionResult Register() =>
User.Identity?.IsAuthenticated == true ? RedirectToAction("Index", "Home") : View(new RegisterViewModel());
[AllowAnonymous]
[HttpPost("register")]
public async Task<IActionResult> Register(RegisterViewModel model, CancellationToken ct)
{
if (!ModelState.IsValid)
return View(model);
try
{
var user = await authApi.RegisterAsync(
new RegisterRequest(model.Email.Trim(), model.Password, model.FirstName.Trim(), model.LastName.Trim()), ct);
return View("RegisterDone", user.Email);
}
catch (ApiException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.Conflict or HttpStatusCode.TooManyRequests)
{
// 400 cizí doména nebo validace, 409 účet už existuje.
ModelState.AddApiErrors(ex, typeof(RegisterViewModel));
model.Password = model.ConfirmPassword = "";
return View(model);
}
}
/// <summary>Odkaz z registračního e-mailu. Token se spotřebuje až tlačítkem, ne samotným otevřením odkazu.</summary>
[AllowAnonymous]
[HttpGet("confirm-email")]
public IActionResult ConfirmEmail(string? token) => View(new EmailTokenViewModel { Token = token ?? "" });
[AllowAnonymous]
[HttpPost("confirm-email")]
public async Task<IActionResult> ConfirmEmail(EmailTokenViewModel model, CancellationToken ct)
{
if (!ModelState.IsValid)
return View(model);
try
{
await authApi.ConfirmEmailAsync(model.Token, ct);
TempData.SetSuccess("E-mail je ověřený. Teď se můžete přihlásit.");
return RedirectToAction(nameof(Login));
}
catch (ApiException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.TooManyRequests)
{
ModelState.AddModelError(string.Empty, ex.UserMessage);
return View(model);
}
}
[AllowAnonymous]
[HttpGet("forgot-password")]
public IActionResult ForgotPassword() => View(new ForgotPasswordViewModel());
[AllowAnonymous]
[HttpPost("forgot-password")]
public async Task<IActionResult> ForgotPassword(ForgotPasswordViewModel model, CancellationToken ct)
{
if (!ModelState.IsValid)
return View(model);
try
{
await authApi.ForgotPasswordAsync(model.Email.Trim(), ct);
return View("ForgotPasswordDone", model.Email.Trim());
}
catch (ApiException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.TooManyRequests)
{
ModelState.AddApiErrors(ex, typeof(ForgotPasswordViewModel));
return View(model);
}
}
[AllowAnonymous]
[HttpGet("reset-password")]
public IActionResult ResetPassword(string? token) => View(new ResetPasswordViewModel { Token = token ?? "" });
[AllowAnonymous]
[HttpPost("reset-password")]
public async Task<IActionResult> ResetPassword(ResetPasswordViewModel model, CancellationToken ct)
{
if (!ModelState.IsValid)
{
model.NewPassword = model.ConfirmPassword = "";
return View(model);
}
try
{
await authApi.ResetPasswordAsync(new ResetPasswordRequest(model.Token, model.NewPassword), ct);
}
catch (ApiException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.TooManyRequests)
{
ModelState.AddApiErrors(ex, typeof(ResetPasswordViewModel));
model.NewPassword = model.ConfirmPassword = "";
return View(model);
}
// API po změně hesla zneplatnilo všechna přihlášení – i to v tomto prohlížeči.
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
TempData.SetSuccess("Heslo je změněné. Přihlaste se novým heslem.");
return RedirectToAction(nameof(Login));
}
[HttpPost("logout")]
public async Task<IActionResult> Logout(CancellationToken ct)
{
@@ -0,0 +1,60 @@
using System.Net;
using Helpdesk.Web.Api;
using Helpdesk.Web.Auth;
using Helpdesk.Web.Infrastructure;
using Helpdesk.Web.Models;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
namespace Helpdesk.Web.Controllers;
/// <summary>Profil přihlášeného uživatele a nastavení notifikací (FR-05).</summary>
[Route("profile")]
public sealed class ProfileController(HelpdeskApiClient api) : Controller
{
[HttpGet("")]
public async Task<IActionResult> Index(CancellationToken ct) =>
View(ProfileViewModel.From(await api.GetMeAsync(ct)));
[HttpPost("")]
public async Task<IActionResult> Index(ProfileViewModel model, CancellationToken ct)
{
if (!ModelState.IsValid)
return View(await WithReadOnlyFieldsAsync(model, ct));
try
{
var user = await api.UpdateMeAsync(
new UpdateProfileRequest(model.FirstName.Trim(), model.LastName.Trim(), model.Phone, model.EmailNotificationsEnabled), ct);
// Jméno je i v cookie (menu, pozdrav na nástěnce) – přepíšeme ho.
await AuthCookie.UpdateUserAsync(HttpContext, user);
TempData.SetSuccess("Profil je uložený.");
return RedirectToAction(nameof(Index));
}
catch (ApiException ex) when (ex.StatusCode == HttpStatusCode.BadRequest)
{
ModelState.AddApiErrors(ex, typeof(ProfileViewModel));
return View(await WithReadOnlyFieldsAsync(model, ct));
}
}
/// <summary>Odhlášení ze všech zařízení – zneplatní tokeny i v mobilní aplikaci.</summary>
[HttpPost("logout-all")]
public async Task<IActionResult> LogoutAll(CancellationToken ct)
{
await api.LogoutAllAsync(ct);
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
TempData.SetSuccess("Byli jste odhlášeni ze všech zařízení.");
return RedirectToAction("Login", "Account");
}
// E-mail a role se formulářem neposílají; při opakovaném zobrazení je doplníme z API.
private async Task<ProfileViewModel> WithReadOnlyFieldsAsync(ProfileViewModel model, CancellationToken ct)
{
var user = await api.GetMeAsync(ct);
model.Email = user.Email;
model.Role = user.Role;
return model;
}
}