initial commit
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// JWT authentication and current user info.
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/v1/auth")]
|
||||
[Produces("application/json")]
|
||||
public class AuthController : ControllerBase
|
||||
{
|
||||
private readonly IAuthService _auth;
|
||||
|
||||
public AuthController(IAuthService auth) => _auth = auth;
|
||||
|
||||
/// <summary>
|
||||
/// Authenticates a user and returns a JWT with role claims.
|
||||
/// </summary>
|
||||
[HttpPost("login")]
|
||||
[ProducesResponseType(typeof(ApiResponse<LoginResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status422UnprocessableEntity)]
|
||||
public async Task<IActionResult> Login([FromBody] LoginRequest req)
|
||||
{
|
||||
var result = await _auth.LoginAsync(req);
|
||||
return Ok(ApiResponse<LoginResponse>.Ok(result));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current authenticated user's profile.
|
||||
/// </summary>
|
||||
[HttpGet("me")]
|
||||
[Authorize]
|
||||
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> Me()
|
||||
{
|
||||
var userId = Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
|
||||
var user = await _auth.GetCurrentUserAsync(userId);
|
||||
return Ok(ApiResponse<object>.Ok(new
|
||||
{
|
||||
user.Id, user.Username, user.FullName,
|
||||
role = user.Role.ToDbString()
|
||||
}));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user