- Add DTOs: RegisterDto, LoginDto, AuthResponse - Add IAuthService interface - Implement AuthService with JWT token generation - Create AuthController with register, login, me endpoints - Add JWT configuration to appsettings.json - Configure JWT Bearer authentication in Program.cs - Add PasswordHasher for custom User entity - Update User entity with ChangePassword method - Add necessary NuGet packages for JWT auth Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
106 lines
3.2 KiB
C#
106 lines
3.2 KiB
C#
using GermanApp.Application.DTOs.Auth;
|
|
using GermanApp.Application.Interfaces;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Net;
|
|
|
|
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")]
|
|
[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")]
|
|
[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(typeof(AuthResponse), (int)HttpStatusCode.OK)]
|
|
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
|
|
public async Task<IActionResult> GetCurrentUser()
|
|
{
|
|
try
|
|
{
|
|
var userId = int.Parse(User.FindFirst("nameid")?.Value ?? "0");
|
|
if (userId == 0)
|
|
return Unauthorized();
|
|
|
|
var user = await _authService.GetCurrentUserAsync(userId);
|
|
if (user == null)
|
|
return Unauthorized();
|
|
|
|
return Ok(new AuthResponse
|
|
{
|
|
UserId = user.Id,
|
|
Username = user.Username,
|
|
Email = user.Email
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, ex.Message);
|
|
}
|
|
}
|
|
}
|