using System.Security.Claims; using BlueWest.Data.Auth; using BlueWest.Data.Auth.Context.Users; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Controller = Microsoft.AspNetCore.Mvc.Controller; namespace CodeLiturgy.Views.Controllers { public class AuthController : UserController { private readonly IAuthManager _authManager; public AuthController(ApplicationUserManager userManager, ILogger logger, IAuthManager authManager) : base(userManager, logger) { _userManager = userManager; _logger = logger; _authManager = authManager; } public IActionResult Index() { OnEveryAction(); return View(); } [AllowAnonymous] [ActionName("LoginAction")] public async Task LoginAction(LoginRequest loginRequest) { var (identity,success) = await _authManager.DoLogin(loginRequest); if (!success) return Redirect(AuthLoginRoute); if (success) { await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity), new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTime.UtcNow.Add(SessionConstants.DefaultSessionMaxAge) }); HttpContext.Session.SetString("hello", "world"); return Redirect(RootLocation); } return Redirect(RootLocation); } public IActionResult Login() { this.HandleGlobalization(); return View(); } public async Task Account() { await OnEveryAction(); this.HandleGlobalization(); return View(); } public async Task Logout() { await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); return Redirect("/"); } public async Task Signup() { await OnEveryAction(); return View(); } [Microsoft.AspNetCore.Mvc.ActionName("SignupAction")] public async Task SignupAction(RegisterRequest registerRequest) { var result = await _authManager.CreateUserAsync(registerRequest); if (result.Succeeded) { return RedirectToAction("Login"); } return RedirectToAction("Signup"); } } }