149 lines
5.7 KiB
C#
149 lines
5.7 KiB
C#
// src/Webshop.Api/Controllers/Auth/AuthController.cs
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Threading.Tasks;
|
|
using Webshop.Application;
|
|
using Webshop.Application.DTOs.Auth;
|
|
using Webshop.Application.DTOs.Email;
|
|
using Webshop.Application.Services.Auth;
|
|
|
|
namespace Webshop.Api.Controllers.Auth
|
|
{
|
|
[ApiController]
|
|
[Route("api/v1/auth")]
|
|
public class AuthController : ControllerBase
|
|
{
|
|
private readonly IAuthService _authService;
|
|
|
|
public AuthController(IAuthService authService)
|
|
{
|
|
_authService = authService;
|
|
}
|
|
|
|
[HttpPost("register")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> Register([FromBody] RegisterRequestDto request)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
|
|
var result = await _authService.RegisterUserAsync(request);
|
|
|
|
if (result.Type == ServiceResultType.Success)
|
|
{
|
|
return Ok(new { Message = "Registrierung erfolgreich. Bitte bestätigen Sie Ihre E-Mail-Adresse." });
|
|
}
|
|
|
|
return BadRequest(new { Message = result.ErrorMessage });
|
|
}
|
|
|
|
[HttpPost("login/customer")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(AuthResponseDto), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
public async Task<IActionResult> LoginCustomer([FromBody] LoginRequestDto request)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
|
|
var result = await _authService.LoginUserAsync(request);
|
|
|
|
if (result.Type == ServiceResultType.Success)
|
|
{
|
|
return Ok(result.Value);
|
|
}
|
|
|
|
return Unauthorized(new { Message = result.ErrorMessage });
|
|
}
|
|
|
|
[HttpPost("login/admin")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(AuthResponseDto), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
|
public async Task<IActionResult> LoginAdmin([FromBody] LoginRequestDto request)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
|
|
var result = await _authService.LoginAdminAsync(request);
|
|
|
|
return result.Type switch
|
|
{
|
|
ServiceResultType.Success => Ok(result.Value),
|
|
ServiceResultType.Forbidden => Forbid(),
|
|
_ => Unauthorized(new { Message = result.ErrorMessage })
|
|
};
|
|
}
|
|
|
|
[HttpGet("confirm-email")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> ConfirmEmail([FromQuery] string userId, [FromQuery] string token)
|
|
{
|
|
var result = await _authService.ConfirmEmailAsync(userId, token);
|
|
|
|
if (result.Type == ServiceResultType.Success)
|
|
{
|
|
return Ok(new { Message = "E-Mail-Adresse erfolgreich bestätigt. Sie können sich jetzt anmelden." });
|
|
}
|
|
|
|
return BadRequest(new { Message = result.ErrorMessage });
|
|
}
|
|
|
|
[HttpPost("resend-email-confirmation")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> ResendEmailConfirmation([FromBody] ResendEmailConfirmationRequestDto request)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
|
|
var result = await _authService.ResendEmailConfirmationAsync(request.Email);
|
|
|
|
if (result.Type == ServiceResultType.Success)
|
|
{
|
|
return Ok(new { Message = "Wenn ein Konto mit dieser E-Mail-Adresse existiert und noch nicht bestätigt wurde, wurde eine neue E-Mail gesendet." });
|
|
}
|
|
|
|
return BadRequest(new { Message = result.ErrorMessage });
|
|
}
|
|
|
|
// << NEUER ENDPUNKT FÜR PASSWORT VERGESSEN >>
|
|
[HttpPost("forgot-password")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> ForgotPassword([FromBody] ForgotPasswordRequestDto request)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
|
|
await _authService.ForgotPasswordAsync(request);
|
|
|
|
return Ok(new { Message = "Wenn ein Konto mit dieser E-Mail-Adresse existiert, wurde eine E-Mail zum Zurücksetzen des Passworts gesendet." });
|
|
}
|
|
|
|
// << NEUER ENDPUNKT FÜR PASSWORT ZURÜCKSETZEN >>
|
|
[HttpPost("reset-password")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> ResetPassword([FromBody] ResetPasswordDto request)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
|
|
var result = await _authService.ResetPasswordAsync(request);
|
|
|
|
if (result.Type == ServiceResultType.Success)
|
|
{
|
|
return Ok(new { Message = "Ihr Passwort wurde erfolgreich zurückgesetzt." });
|
|
}
|
|
|
|
return BadRequest(new { Message = result.ErrorMessage });
|
|
}
|
|
|
|
// HINWEIS: Der 'change-email-confirm'-Endpunkt gehört logisch eher zum CustomerController,
|
|
// da er eine Aktion eines eingeloggten Benutzers ist.
|
|
}
|
|
} |