79 lines
2.0 KiB
C#
79 lines
2.0 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using PlotLine.Services;
|
|
using PlotLine.ViewModels;
|
|
|
|
namespace PlotLine.Controllers;
|
|
|
|
public sealed class AccountController(IAuthService authService) : Controller
|
|
{
|
|
[HttpGet]
|
|
public IActionResult Register(string? returnUrl = null)
|
|
{
|
|
return View(new RegisterViewModel());
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Register(RegisterViewModel model)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return View(model);
|
|
}
|
|
|
|
var result = await authService.RegisterAsync(model);
|
|
if (!result.Success)
|
|
{
|
|
ModelState.AddModelError(string.Empty, result.ErrorMessage ?? "Registration could not be completed.");
|
|
return View(model);
|
|
}
|
|
|
|
TempData["AuthMessage"] = "Your account has been created. You can now log in.";
|
|
return RedirectToAction(nameof(Login));
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult Login(string? returnUrl = null)
|
|
{
|
|
return View(new LoginViewModel { ReturnUrl = returnUrl });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Login(LoginViewModel model)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return View(model);
|
|
}
|
|
|
|
var result = await authService.LoginAsync(model);
|
|
if (!result.Success)
|
|
{
|
|
ModelState.AddModelError(string.Empty, result.ErrorMessage ?? "Login could not be completed.");
|
|
return View(model);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(model.ReturnUrl) && Url.IsLocalUrl(model.ReturnUrl))
|
|
{
|
|
return LocalRedirect(model.ReturnUrl);
|
|
}
|
|
|
|
return RedirectToAction("Index", "Home");
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Logout()
|
|
{
|
|
await authService.LogoutAsync();
|
|
return RedirectToAction("Index", "Home");
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult AccessDenied()
|
|
{
|
|
return View();
|
|
}
|
|
}
|