Initial Commit
This commit is contained in:
55
src/SportsDivision.Web/Controllers/AccountController.cs
Normal file
55
src/SportsDivision.Web/Controllers/AccountController.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SportsDivision.Infrastructure.Identity;
|
||||
|
||||
namespace SportsDivision.Web.Controllers;
|
||||
|
||||
public class AccountController : Controller
|
||||
{
|
||||
private readonly SignInManager<ApplicationUser> _signInManager;
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
|
||||
public AccountController(SignInManager<ApplicationUser> signInManager, UserManager<ApplicationUser> userManager)
|
||||
{
|
||||
_signInManager = signInManager;
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Login(string? returnUrl = null)
|
||||
{
|
||||
ViewData["ReturnUrl"] = returnUrl;
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Login(string email, string password, bool rememberMe, string? returnUrl = null)
|
||||
{
|
||||
ViewData["ReturnUrl"] = returnUrl;
|
||||
if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
|
||||
{
|
||||
ModelState.AddModelError("", "Email and password are required.");
|
||||
return View();
|
||||
}
|
||||
|
||||
var result = await _signInManager.PasswordSignInAsync(email, password, rememberMe, lockoutOnFailure: false);
|
||||
if (result.Succeeded)
|
||||
{
|
||||
return LocalRedirect(returnUrl ?? "/");
|
||||
}
|
||||
ModelState.AddModelError("", "Invalid login attempt.");
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Logout()
|
||||
{
|
||||
await _signInManager.SignOutAsync();
|
||||
return RedirectToAction("Index", "Home");
|
||||
}
|
||||
|
||||
public IActionResult AccessDenied() => View();
|
||||
}
|
||||
Reference in New Issue
Block a user