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)
{