All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
- Added Role to AuthResponse DTO and all auth endpoints - Fixed null config handling in MistralConnector, TtsService, VoskService, MistralService - Fixed BaseAddress setup in MistralConnector to work without API key - Reverted seed data to use hardcoded bcrypt hashes (compatible with PasswordHasher) - Added integration tests for StoryController - Added unit tests for MistralConnector - Updated frontend AuthResponse type to include role Fixes admin redirect to /, story generation null reference, and Docker build failures. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
171 lines
5.7 KiB
C#
171 lines
5.7 KiB
C#
using GermanApp.Application.DTOs.Auth;
|
|
using GermanApp.Application.Interfaces;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Net;
|
|
using System.Security.Claims;
|
|
|
|
namespace GermanApp.Presentation.Controllers;
|
|
|
|
/// <summary>
|
|
/// Controller for authentication endpoints.
|
|
/// Part of the Presentation layer.
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class AuthController : ControllerBase
|
|
{
|
|
private readonly IAuthService _authService;
|
|
|
|
public AuthController(IAuthService authService)
|
|
{
|
|
_authService = authService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register a new user.
|
|
/// </summary>
|
|
/// <param name="registerDto">Registration data</param>
|
|
/// <returns>Authentication response with JWT token</returns>
|
|
[HttpPost("register")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(AuthResponse), (int)HttpStatusCode.OK)]
|
|
[ProducesResponseType(typeof(string), (int)HttpStatusCode.BadRequest)]
|
|
public async Task<IActionResult> Register([FromBody] RegisterDto registerDto)
|
|
{
|
|
try
|
|
{
|
|
var result = await _authService.RegisterAsync(registerDto);
|
|
return Ok(result);
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return BadRequest(ex.Message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Login an existing user.
|
|
/// </summary>
|
|
/// <param name="loginDto">Login data</param>
|
|
/// <returns>Authentication response with JWT token</returns>
|
|
[HttpPost("login")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(AuthResponse), (int)HttpStatusCode.OK)]
|
|
[ProducesResponseType(typeof(string), (int)HttpStatusCode.Unauthorized)]
|
|
public async Task<IActionResult> Login([FromBody] LoginDto loginDto)
|
|
{
|
|
try
|
|
{
|
|
var result = await _authService.LoginAsync(loginDto);
|
|
return Ok(result);
|
|
}
|
|
catch (UnauthorizedAccessException ex)
|
|
{
|
|
return Unauthorized(ex.Message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get current authenticated user information.
|
|
/// </summary>
|
|
/// <returns>Current user information</returns>
|
|
[HttpGet("me")]
|
|
[Authorize]
|
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
|
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
|
|
public async Task<IActionResult> GetCurrentUser()
|
|
{
|
|
try
|
|
{
|
|
// Get user ID from JWT claim
|
|
// Note: JWT "sub" claim is mapped by ASP.NET Core JWT middleware to ClaimTypes.NameIdentifier
|
|
// (http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier)
|
|
var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier) ?? User.FindFirst("sub");
|
|
if (userIdClaim == null || !int.TryParse(userIdClaim.Value, out var userId) || userId == 0)
|
|
return Unauthorized();
|
|
|
|
var user = await _authService.GetCurrentUserAsync(userId);
|
|
if (user == null)
|
|
return Unauthorized();
|
|
|
|
// Return full user profile including role, level, streak, and points
|
|
return Ok(new
|
|
{
|
|
userId = user.Id,
|
|
username = user.Username,
|
|
email = user.Email,
|
|
role = user.Role,
|
|
currentLevel = user.CurrentLevel,
|
|
streak = user.Streak,
|
|
totalPoints = user.TotalPoints
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Refresh the access token using a refresh token.
|
|
/// </summary>
|
|
/// <param name="refreshToken">The refresh token</param>
|
|
/// <returns>New access token and refresh token</returns>
|
|
[HttpPost("refresh")]
|
|
[ProducesResponseType(typeof(RefreshTokenResponse), (int)HttpStatusCode.OK)]
|
|
[ProducesResponseType(typeof(string), (int)HttpStatusCode.Unauthorized)]
|
|
[ProducesResponseType(typeof(string), (int)HttpStatusCode.BadRequest)]
|
|
public async Task<IActionResult> Refresh([FromBody] string refreshToken)
|
|
{
|
|
try
|
|
{
|
|
var result = await _authService.RefreshTokenAsync(refreshToken);
|
|
return Ok(result);
|
|
}
|
|
catch (UnauthorizedAccessException ex)
|
|
{
|
|
return Unauthorized(ex.Message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Revoke a refresh token.
|
|
/// </summary>
|
|
/// <param name="refreshToken">The refresh token to revoke</param>
|
|
/// <returns>Success or error response</returns>
|
|
[HttpPost("revoke-refresh")]
|
|
[Authorize]
|
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
|
[ProducesResponseType(typeof(string), (int)HttpStatusCode.Unauthorized)]
|
|
[ProducesResponseType(typeof(string), (int)HttpStatusCode.BadRequest)]
|
|
public async Task<IActionResult> RevokeRefreshToken([FromBody] string refreshToken)
|
|
{
|
|
try
|
|
{
|
|
await _authService.RevokeRefreshTokenAsync(refreshToken);
|
|
return Ok(new { message = "Refresh token revoked successfully" });
|
|
}
|
|
catch (UnauthorizedAccessException ex)
|
|
{
|
|
return Unauthorized(ex.Message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, ex.Message);
|
|
}
|
|
}
|
|
}
|