- Add LoopFit.Domain with entities (ApplicationUser, Workout, CalorieEntry, VitalData), enums (WorkoutType), and interfaces (IJwtTokenGenerator, IUserRepository, IWorkoutRepository) - Add LoopFit.Application with auth DTOs, service interfaces, and DI setup - Add LoopFit.Infrastructure with EF Core DbContext (PostgreSQL), ASP.NET Core Identity, JWT token generation, and repository implementations - Add AuthController with register/login/me endpoints and JWT Bearer auth - Add MAUI auth services (AuthService, TokenStorageService) and Login/Register Blazor pages
59 lines
1.6 KiB
C#
59 lines
1.6 KiB
C#
using System.Security.Claims;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using LoopFit.Application.DTOs.Auth;
|
|
using LoopFit.Application.Interfaces;
|
|
|
|
namespace LoopFit.Web.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class AuthController : ControllerBase
|
|
{
|
|
private readonly IAuthService _authService;
|
|
|
|
public AuthController(IAuthService authService)
|
|
{
|
|
_authService = authService;
|
|
}
|
|
|
|
[HttpPost("register")]
|
|
public async Task<ActionResult<AuthResponse>> Register([FromBody] RegisterRequest request)
|
|
{
|
|
var result = await _authService.RegisterAsync(request);
|
|
|
|
if (result is null)
|
|
return BadRequest(new { message = "Registrierung fehlgeschlagen. Email existiert bereits oder Passwort ist nicht gültig." });
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpPost("login")]
|
|
public async Task<ActionResult<AuthResponse>> Login([FromBody] LoginRequest request)
|
|
{
|
|
var result = await _authService.LoginAsync(request);
|
|
|
|
if (result is null)
|
|
return Unauthorized(new { message = "Email oder Passwort falsch." });
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpGet("me")]
|
|
public async Task<ActionResult<AuthResponse>> GetCurrentUser()
|
|
{
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
|
|
if (userId is null)
|
|
return Unauthorized();
|
|
|
|
var result = await _authService.GetCurrentUserAsync(Guid.Parse(userId));
|
|
|
|
if (result is null)
|
|
return NotFound();
|
|
|
|
return Ok(result);
|
|
}
|
|
}
|