2026-12-02 00:26:42 +01:00
|
|
|
|
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.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Helpdesk.Web.Controllers;
|
|
|
|
|
|
|
2026-12-02 00:31:07 +01:00
|
|
|
|
/// <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>
|
2026-12-02 00:26:42 +01:00
|
|
|
|
public sealed class AccountController(AuthApiClient authApi, HelpdeskApiClient api) : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
[HttpGet("login")]
|
|
|
|
|
|
public IActionResult Login(string? returnUrl)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (User.Identity?.IsAuthenticated == true)
|
|
|
|
|
|
return LocalRedirect(SafeReturnUrl(returnUrl));
|
|
|
|
|
|
|
|
|
|
|
|
ViewData["ReturnUrl"] = returnUrl;
|
|
|
|
|
|
return View(new LoginViewModel());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
[HttpPost("login")]
|
|
|
|
|
|
public async Task<IActionResult> Login(LoginViewModel model, string? returnUrl, CancellationToken ct)
|
|
|
|
|
|
{
|
|
|
|
|
|
ViewData["ReturnUrl"] = returnUrl;
|
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
|
return View(model);
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var auth = await authApi.LoginAsync(new LoginRequest(model.Email.Trim(), model.Password), ct);
|
|
|
|
|
|
await AuthCookie.SignInAsync(HttpContext, auth, model.RememberMe);
|
|
|
|
|
|
return LocalRedirect(SafeReturnUrl(returnUrl));
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (ApiException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.Unauthorized
|
|
|
|
|
|
or HttpStatusCode.Forbidden or HttpStatusCode.TooManyRequests)
|
|
|
|
|
|
{
|
|
|
|
|
|
ModelState.AddApiErrors(ex, typeof(LoginViewModel));
|
|
|
|
|
|
model.Password = "";
|
|
|
|
|
|
return View(model);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-12-02 00:31:07 +01:00
|
|
|
|
[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(
|
2026-12-04 20:14:00 +01:00
|
|
|
|
new RegisterRequest(model.Email.Trim(), model.FirstName.Trim(), model.LastName.Trim()), ct);
|
2026-12-02 00:31:07 +01:00
|
|
|
|
return View("RegisterDone", user.Email);
|
|
|
|
|
|
}
|
2026-12-05 02:42:22 +01:00
|
|
|
|
catch (ApiException ex) when (ex is { StatusCode: HttpStatusCode.ServiceUnavailable, ProblemType: "urn:ntbug:email-delivery-failed" })
|
|
|
|
|
|
{
|
|
|
|
|
|
ViewData["VerificationEmailFailed"] = true;
|
|
|
|
|
|
ModelState.AddModelError(string.Empty, "Účet je uložený. Ověřovací e-mail se nepodařilo odeslat, zkuste ho prosím poslat znovu.");
|
|
|
|
|
|
return View(model);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (ApiException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.Conflict
|
|
|
|
|
|
or HttpStatusCode.TooManyRequests or HttpStatusCode.ServiceUnavailable)
|
2026-12-02 00:31:07 +01:00
|
|
|
|
{
|
|
|
|
|
|
ModelState.AddApiErrors(ex, typeof(RegisterViewModel));
|
|
|
|
|
|
return View(model);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-12-04 20:14:00 +01:00
|
|
|
|
/// <summary>Odkaz z ověřovacího e-mailu. Token se spotřebuje při uložení hesla.</summary>
|
2026-12-02 00:31:07 +01:00
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
[HttpGet("confirm-email")]
|
2026-12-04 20:14:00 +01:00
|
|
|
|
public IActionResult ConfirmEmail(string? token) => View(new EmailPasswordViewModel { Token = token ?? "" });
|
2026-12-02 00:31:07 +01:00
|
|
|
|
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
[HttpPost("confirm-email")]
|
2026-12-04 20:14:00 +01:00
|
|
|
|
public async Task<IActionResult> ConfirmEmail(EmailPasswordViewModel model, CancellationToken ct)
|
2026-12-02 00:31:07 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (!ModelState.IsValid)
|
2026-12-04 20:14:00 +01:00
|
|
|
|
{
|
|
|
|
|
|
model.NewPassword = model.ConfirmPassword = "";
|
2026-12-02 00:31:07 +01:00
|
|
|
|
return View(model);
|
2026-12-04 20:14:00 +01:00
|
|
|
|
}
|
2026-12-02 00:31:07 +01:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-12-04 20:14:00 +01:00
|
|
|
|
await authApi.ConfirmEmailAsync(new ConfirmEmailRequest(model.Token, model.NewPassword), ct);
|
|
|
|
|
|
TempData.SetSuccess("E-mail je ověřený. Teď se můžete přihlásit nastaveným heslem.");
|
2026-12-02 00:31:07 +01:00
|
|
|
|
return RedirectToAction(nameof(Login));
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (ApiException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.TooManyRequests)
|
|
|
|
|
|
{
|
2026-12-04 20:14:00 +01:00
|
|
|
|
ModelState.AddApiErrors(ex, typeof(EmailPasswordViewModel));
|
|
|
|
|
|
model.NewPassword = model.ConfirmPassword = "";
|
2026-12-02 00:31:07 +01:00
|
|
|
|
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")]
|
2026-12-04 20:14:00 +01:00
|
|
|
|
public IActionResult ResetPassword(string? token) => View(new EmailPasswordViewModel { Token = token ?? "" });
|
2026-12-02 00:31:07 +01:00
|
|
|
|
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
[HttpPost("reset-password")]
|
2026-12-04 20:14:00 +01:00
|
|
|
|
public async Task<IActionResult> ResetPassword(EmailPasswordViewModel model, CancellationToken ct)
|
2026-12-02 00:31:07 +01:00
|
|
|
|
{
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2026-12-04 20:14:00 +01:00
|
|
|
|
ModelState.AddApiErrors(ex, typeof(EmailPasswordViewModel));
|
2026-12-02 00:31:07 +01:00
|
|
|
|
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));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-12-02 00:26:42 +01:00
|
|
|
|
[HttpPost("logout")]
|
|
|
|
|
|
public async Task<IActionResult> Logout(CancellationToken ct)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (await HttpContext.GetTokenAsync(AuthCookie.RefreshToken) is { Length: > 0 } refreshToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await api.LogoutAsync(refreshToken, ct);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex) when (ex is ApiException or HttpRequestException)
|
|
|
|
|
|
{
|
|
|
|
|
|
// I když API zrovna nejde (nebo jsou tokeny už neplatné), lokální cookie smažeme.
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
|
|
|
|
TempData.SetSuccess("Byli jste odhlášeni.");
|
|
|
|
|
|
return RedirectToAction(nameof(Login));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private string SafeReturnUrl(string? returnUrl) =>
|
|
|
|
|
|
!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl) ? returnUrl : "/";
|
|
|
|
|
|
}
|