using System; using Microsoft.AspNetCore.Mvc; using CarParkValidationAPI.Authorization; using CarParkValidationAPI.Entities; using CarParkValidationAPI.Models.Users; using CarParkValidationAPI.Interface; using System.Threading.Tasks; using CarParkValidationAPI.Models; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity.Data; namespace CarParkValidationAPI.Controllers { // [Authorize] [ApiController] [Route("api/v1/[controller]")] public class UsersController : ControllerBase { private IUserService _userService; public UsersController(IUserService userService) { _userService = userService; } [AllowAnonymous] [HttpPost("[action]")] public IActionResult Authenticate([FromBody] AuthenticateRequest model) { var response = _userService.Authenticate(model); return Ok(response); } [AllowAnonymous] [HttpPost("[action]")] public async Task LoginAD([FromBody] AuthenticateRequest model) { var response = await _userService.LoginAD(model); // Configure cookie options var cookieOptions = new CookieOptions { HttpOnly = true, // Blocks client-side JavaScript reading/theft Secure = true, // Requires HTTPS connections SameSite = SameSiteMode.Unspecified, // Prevents CSRF attacks Expires = DateTimeOffset.UtcNow.AddDays(1), // Match your token lifespan Path = "/" // Accessible across entire domain structure }; var jwttoken = response.Data.Token; // Write cookie into response headers Response.Cookies.Append("token", jwttoken, cookieOptions); return Ok(response); } [AllowAnonymous] // 3. POST: api/auth/logout [HttpPost("[action]")] public IActionResult Logout() { // Erase cookie immediately by forcing an expired timestamp Response.Cookies.Delete("token", new CookieOptions { HttpOnly = true, Secure = true, SameSite = SameSiteMode.Unspecified, Path = "/" }); return Ok(new { message = "Logged out successfully" }); } [AllowAnonymous] [HttpPost("[action]")] public async Task LoginApiAD([FromBody] AuthenticateRequest model) { var response = await _userService.LoginApiAD(model); // Configure cookie options var cookieOptions = new CookieOptions { HttpOnly = true, // Blocks client-side JavaScript reading/theft Secure = true, // Requires HTTPS connections SameSite = SameSiteMode.Unspecified, // Prevents CSRF attacks Expires = DateTimeOffset.UtcNow.AddDays(1), // Match your token lifespan Path = "/" // Accessible across entire domain structure }; var jwttoken = response.Data.Token; // Write cookie into response headers Response.Cookies.Append("token", jwttoken, cookieOptions); return Ok(response); } [AllowAnonymous] [HttpGet("[action]")] public async Task SearchApiStaff(string stafflinkNo) { var user = await _userService.SearchApiStaff(stafflinkNo); return Ok(user); } [AllowAnonymous] [HttpGet("[action]")] public async Task> SearchADStaff(string stafflinkNo) { var user = await _userService.SearchADStaff(stafflinkNo); return user; } } }