41 lines
1.4 KiB
C#
41 lines
1.4 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
/// <summary>
|
|
/// JWT authentication: login and current-user profile.
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("api/v1/auth")]
|
|
[Produces("application/json")]
|
|
public class AuthController : ControllerBase
|
|
{
|
|
private readonly IAuthService _auth;
|
|
|
|
public AuthController(IAuthService auth) => _auth = auth;
|
|
|
|
/// <summary>Authenticate and receive a JWT bearer token.</summary>
|
|
[HttpPost("login")]
|
|
[AllowAnonymous]
|
|
[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 authenticated user's profile.</summary>
|
|
[HttpGet("me")]
|
|
[Authorize]
|
|
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)]
|
|
public IActionResult Me([FromServices] ICurrentUserService currentUser)
|
|
{
|
|
return Ok(ApiResponse<object>.Ok(new
|
|
{
|
|
userId = currentUser.UserId,
|
|
username = currentUser.Username,
|
|
displayName = currentUser.DisplayName,
|
|
role = currentUser.Role?.ToDbString()
|
|
}));
|
|
}
|
|
} |