Spaces:
Sleeping
Sleeping
File size: 3,900 Bytes
b0a0ed0 6f32ae8 b0a0ed0 c312181 b0a0ed0 c312181 b0a0ed0 6f32ae8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using System.Security.Claims;
using LibraryManagement.Shared.Models;
using Frontend.Services;
namespace Frontend.Controllers
{
public class AuthController : Controller
{
private readonly LibraryApiClient _apiClient;
public AuthController(LibraryApiClient apiClient)
{
_apiClient = apiClient;
}
[HttpGet]
public IActionResult Login() => View();
[HttpPost]
public async Task<IActionResult> Login(LoginRequest request)
{
if (!ModelState.IsValid) return View(request);
var response = await _apiClient.LoginAsync(request);
if (response != null && !string.IsNullOrEmpty(response.Token))
{
// Store token in cookie
var cookieOptions = new CookieOptions
{
HttpOnly = true,
Secure = false,
SameSite = SameSiteMode.Lax,
Expires = DateTime.UtcNow.AddDays(7)
};
Response.Cookies.Append("AuthToken", response.Token, cookieOptions);
await SignInUserAsync(response, request.Email);
TempData["Success"] = "Welcome back!";
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError("", "Invalid email or password.");
return View(request);
}
[HttpGet]
public IActionResult Register() => View();
[HttpPost]
public async Task<IActionResult> Register(RegisterRequest request)
{
if (!ModelState.IsValid) return View(request);
var response = await _apiClient.RegisterAsync(request);
if (response != null && !string.IsNullOrEmpty(response.Token))
{
var cookieOptions = new CookieOptions
{
HttpOnly = true,
Secure = false,
SameSite = SameSiteMode.Lax,
Expires = DateTime.UtcNow.AddDays(7)
};
Response.Cookies.Append("AuthToken", response.Token, cookieOptions);
await SignInUserAsync(response, request.Email);
TempData["Success"] = "Account created successfully!";
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError("", "Registration failed. Email might already be in use.");
return View(request);
}
private async Task SignInUserAsync(AuthResponse response, string email)
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, response.FullName),
new Claim(ClaimTypes.Email, email),
new Claim(ClaimTypes.Role, string.IsNullOrEmpty(response.Role) ? "Member" : response.Role)
};
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties
{
IsPersistent = true,
ExpiresUtc = DateTimeOffset.UtcNow.AddDays(7)
};
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity),
authProperties);
}
public async Task<IActionResult> Logout()
{
Response.Cookies.Delete("AuthToken");
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
TempData["Success"] = "Logged out successfully.";
return RedirectToAction("Login");
}
}
}
|