update auth for refresh tokens and token invalidation

This commit is contained in:
voltsrage
2026-06-26 04:32:01 +08:00
parent 9777a335c5
commit abb33de5c1
20 changed files with 1183 additions and 12 deletions
@@ -4,7 +4,7 @@ using Microsoft.AspNetCore.Mvc;
/// <summary>
/// JWT authentication and current user info.
/// JWT authentication, token refresh, logout, and current user info.
/// </summary>
[ApiController]
[Route("api/v1/auth")]
@@ -16,7 +16,7 @@ public class AuthController : ControllerBase
public AuthController(IAuthService auth) => _auth = auth;
/// <summary>
/// Authenticates a user and returns a JWT with role claims.
/// Authenticates a user and returns a JWT access token, refresh token, and profile.
/// </summary>
[HttpPost("login")]
[AllowAnonymous]
@@ -28,6 +28,32 @@ public class AuthController : ControllerBase
return Ok(ApiResponse<LoginResponse>.Ok(result));
}
/// <summary>
/// Rotates the refresh token and issues a new access token.
/// </summary>
[HttpPost("refresh")]
[AllowAnonymous]
[ProducesResponseType(typeof(ApiResponse<TokenResponse>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status422UnprocessableEntity)]
public async Task<IActionResult> Refresh([FromBody] RefreshRequest req)
{
var result = await _auth.RefreshAsync(req);
return Ok(ApiResponse<TokenResponse>.Ok(result));
}
/// <summary>
/// Revokes the refresh token server-side.
/// </summary>
[HttpPost("logout")]
[AllowAnonymous]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status422UnprocessableEntity)]
public async Task<IActionResult> Logout([FromBody] LogoutRequest req)
{
await _auth.LogoutAsync(req);
return NoContent();
}
/// <summary>
/// Returns the current authenticated user's profile.
/// </summary>
@@ -42,4 +68,4 @@ public class AuthController : ControllerBase
return Ok(ApiResponse<UserProfileResponse>.Ok(new UserProfileResponse(
user.Id, user.Username, user.FullName, user.Role.ToDbString())));
}
}
}