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

102 lines
2.9 KiB
C#
Raw Normal View History

2022-10-27 20:13:02 +03:00
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;
2022-09-26 04:05:23 +03:00
using Microsoft.AspNetCore.Mvc;
2022-10-27 20:13:02 +03:00
using Controller = Microsoft.AspNetCore.Mvc.Controller;
2022-09-26 04:05:23 +03:00
2022-10-30 19:48:24 +03:00
namespace CodeLiturgy.Views.Controllers
2022-09-26 04:05:23 +03:00
{
2022-10-27 20:13:02 +03:00
public class AuthController : UserController
2022-09-26 04:05:23 +03:00
{
2022-10-27 20:13:02 +03:00
private readonly IAuthManager _authManager;
public AuthController(ApplicationUserManager userManager, ILogger<AuthController> logger, IAuthManager authManager) : base(userManager, logger)
{
_userManager = userManager;
_logger = logger;
_authManager = authManager;
}
2022-09-29 02:37:24 +03:00
public IActionResult Index()
2022-10-27 20:13:02 +03:00
{
OnEveryAction();
return View();
}
[AllowAnonymous]
2022-11-13 14:27:48 +03:00
[ActionName("LoginAction")]
2022-10-27 20:13:02 +03:00
public async Task<IActionResult> LoginAction(LoginRequest loginRequest)
{
2022-11-13 14:27:48 +03:00
var (identity,success) =
await _authManager.DoLogin(loginRequest);
2022-10-27 20:13:02 +03:00
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()
2022-09-29 02:37:24 +03:00
{
this.HandleGlobalization();
return View();
}
2022-10-27 20:13:02 +03:00
2022-11-17 00:17:44 +03:00
public async Task<IActionResult> Account()
{
await OnEveryAction();
this.HandleGlobalization();
return View();
}
2022-10-27 20:13:02 +03:00
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);
2022-09-26 04:05:23 +03:00
2022-10-27 20:13:02 +03:00
if (result.Succeeded)
{
return RedirectToAction("Login");
}
return RedirectToAction("Signup");
}
}
}