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

140 lines
4.2 KiB
C#
Raw Normal View History

2022-10-27 20:13:02 +03:00
using System.Security.Claims;
2022-12-09 03:27:00 +03:00
using CodeLiturgy.Data.Application.Users;
2022-11-18 03:15:53 +03:00
using CodeLiturgy.Data.Auth;
using CodeLiturgy.Data.Auth.Context.Users;
2022-10-27 20:13:02 +03:00
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-12-09 03:27:00 +03:00
using Microsoft.EntityFrameworkCore;
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;
2022-11-22 18:05:35 +03:00
public AuthController(
ApplicationUserManager userManager,
ILogger<AuthController> logger,
IAuthManager authManager) : base(userManager, logger)
2022-10-27 20:13:02 +03:00
{
_userManager = userManager;
_logger = logger;
_authManager = authManager;
}
public async Task<IActionResult> Index()
2022-10-27 20:13:02 +03:00
{
await OnEveryAction();
2022-10-27 20:13:02 +03:00
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-12-09 03:27:00 +03:00
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);
}
2022-12-09 03:27:00 +03:00
2022-10-27 20:13:02 +03:00
return Redirect(RootLocation);
}
2022-12-09 03:27:00 +03:00
public async Task<IActionResult> Login()
2022-09-29 02:37:24 +03:00
{
2022-12-09 03:27:00 +03:00
#if DEBUG
// Check if the debug user exists, if not create it
if (!await _userManager.Users.AnyAsync(x => x.Email == "debuguser@admin.com"))
{
var registerRequest = new RegisterRequest();
registerRequest.Email = "debuguser@admin.com";
registerRequest.Username = "debuguser";
registerRequest.Password = "debuguser";
await _authManager.CreateUserAsync(registerRequest);
}
var (identity,success) = await _authManager.DoLogin(new LoginRequest{Email = "debuguser@admin.com", Password = "debuguser"});
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);
#else
2022-09-29 02:37:24 +03:00
this.HandleGlobalization();
return View();
2022-12-09 03:27:00 +03:00
#endif
2022-09-29 02:37:24 +03:00
}
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");
}
}
}