CodeLiturgy.Dashboard/CodeLiturgy.Views/Controllers/AuthController.cs

96 lines
2.8 KiB
C#

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<AuthController> logger, IAuthManager authManager) : base(userManager, logger)
{
_userManager = userManager;
_logger = logger;
_authManager = authManager;
}
public IActionResult Index()
{
OnEveryAction();
return View();
}
[AllowAnonymous]
[Microsoft.AspNetCore.Mvc.ActionName("LoginAction")]
public async Task<IActionResult> LoginAction(LoginRequest loginRequest)
{
var (success, sessionToken, identity) =
await _authManager.GetSessionTokenIdByLoginRequestViaCookie(loginRequest,
CookieAuthenticationDefaults.AuthenticationScheme);
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<IActionResult> Logout()
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
return Redirect("/");
}
public async Task<IActionResult> Signup()
{
await OnEveryAction();
return View();
}
[Microsoft.AspNetCore.Mvc.ActionName("SignupAction")]
public async Task<IActionResult> SignupAction(RegisterRequest registerRequest)
{
var result = await _authManager.CreateUserAsync(registerRequest);
if (result.Succeeded)
{
return RedirectToAction("Login");
}
return RedirectToAction("Signup");
}
}
}