using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using DigitalData.UserManager.Application.DTOs.Auth; using DigitalData.UserManager.Application.Contracts; using DigitalData.Core.DTO; using Microsoft.Extensions.Localization; using DigitalData.UserManager.Application; using System.Security.Claims; namespace DigitalData.UserManager.API.Controllers; [Route("api/Auth")] [ApiController] [Tags("Auth")] public class PlaceholderAuthController : ControllerBase { private readonly ILogger _logger; private readonly IUserService _userService; private readonly IStringLocalizer _localizer; public PlaceholderAuthController(ILogger logger, IUserService userService, IStringLocalizer localizer) { _logger = logger; _userService = userService; _localizer = localizer; } [Authorize] [HttpGet("check")] public IActionResult CheckAuthentication() => Ok(); [AllowAnonymous] [HttpPost("login")] public Task Login([FromBody] LogInDto login) => throw new NotImplementedException(); [Authorize] [HttpGet("user")] public async Task GetUserWithClaims() { try { // Extract the username from the Name claim. string? username = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value; if (string.IsNullOrEmpty(username)) return Unauthorized(); return await _userService.ReadByUsernameAsync(username) .ThenAsync(Ok, IActionResult (m, n) => { _logger.LogNotice(n); return NotFound(Result.Fail().Message(_localizer[Key.UserNotFound])); }); } catch (Exception ex) { _logger.LogError(ex, "{Message}", ex.Message); return StatusCode(StatusCodes.Status500InternalServerError); } } [Authorize] [HttpPost("logout")] public IActionResult Logout() { Response.Cookies.Delete("AuthToken", new() { Path = "/" }); return Ok(); } }