Spaces:
Sleeping
Sleeping
| using Microsoft.AspNetCore.Authorization; | |
| using Microsoft.AspNetCore.Mvc; | |
| using System.Security.Claims; | |
| using LibraryManagement.Shared.Models; | |
| namespace Backend.Features.Auth | |
| { | |
| [] | |
| [] | |
| public class AuthController : ControllerBase | |
| { | |
| private readonly IAuthService _authService; | |
| public AuthController(IAuthService authService) | |
| { | |
| _authService = authService; | |
| } | |
| [] | |
| public async Task<ActionResult<AuthResponse>> Register([FromBody] RegisterRequest request) | |
| { | |
| try | |
| { | |
| var response = await _authService.Register(request); | |
| return Ok(response); | |
| } | |
| catch (Exception ex) | |
| { | |
| return BadRequest(new { message = ex.Message }); | |
| } | |
| } | |
| [] | |
| public async Task<ActionResult<AuthResponse>> Login([FromBody] LoginRequest request) | |
| { | |
| try | |
| { | |
| var response = await _authService.Login(request); | |
| return Ok(response); | |
| } | |
| catch (Exception ex) | |
| { | |
| return Unauthorized(new { message = ex.Message }); | |
| } | |
| } | |
| [] | |
| [] | |
| public IActionResult GetCurrentUser() | |
| { | |
| var claims = User.Claims.Select(c => new { c.Type, c.Value }).ToList(); | |
| return Ok(new | |
| { | |
| IsAuthenticated = User.Identity?.IsAuthenticated, | |
| Name = User.Identity?.Name, | |
| Claims = claims | |
| }); | |
| } | |
| } | |
| } | |