145 lines
6.8 KiB
C#
145 lines
6.8 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/[controller]")]
|
|
public class AuthController : ControllerBase
|
|
{
|
|
private readonly IAuthService _authService;
|
|
|
|
public AuthController(IAuthService authService)
|
|
{
|
|
_authService = authService;
|
|
}
|
|
|
|
[HttpPost("register")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> Register([FromBody] RegisterRequestDto request)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
|
|
var result = await _authService.RegisterUserAsync(request);
|
|
|
|
return result.Type switch
|
|
{
|
|
ServiceResultType.Success => Ok(new { Message = "Registrierung erfolgreich. Bitte bestätigen Sie Ihre E-Mail-Adresse." }),
|
|
ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }),
|
|
ServiceResultType.Failure => BadRequest(new { Message = result.ErrorMessage }),
|
|
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
|
|
};
|
|
}
|
|
|
|
[HttpPost("login/customer")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(AuthResponseDto), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status401Unauthorized)]
|
|
public async Task<IActionResult> LoginCustomer([FromBody] LoginRequestDto request)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
|
|
var result = await _authService.LoginUserAsync(request);
|
|
|
|
return result.Type switch
|
|
{
|
|
ServiceResultType.Success => Ok(result.Value),
|
|
ServiceResultType.Unauthorized => Unauthorized(new { Message = result.ErrorMessage }),
|
|
_ => Unauthorized(new { Message = result.ErrorMessage ?? "Ungültige Anmeldeinformationen." })
|
|
};
|
|
}
|
|
|
|
[HttpPost("login/admin")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(AuthResponseDto), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), 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(),
|
|
ServiceResultType.Unauthorized => Unauthorized(new { Message = result.ErrorMessage }),
|
|
_ => Unauthorized(new { Message = result.ErrorMessage ?? "Ungültige Anmeldeinformationen." })
|
|
};
|
|
}
|
|
|
|
[HttpGet("confirm-email")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> ConfirmEmail([FromQuery] string userId, [FromQuery] string token)
|
|
{
|
|
var result = await _authService.ConfirmEmailAsync(userId, token);
|
|
|
|
return result.Type switch
|
|
{
|
|
ServiceResultType.Success => Ok(new { Message = "E-Mail-Adresse erfolgreich bestätigt. Sie können sich jetzt anmelden." }),
|
|
_ => BadRequest(new { Message = result.ErrorMessage ?? "Der Bestätigungslink ist ungültig oder abgelaufen." })
|
|
};
|
|
}
|
|
|
|
[HttpPost("resend-email-confirmation")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> ResendEmailConfirmation([FromBody] ResendEmailConfirmationRequestDto request)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
|
|
var result = await _authService.ResendEmailConfirmationAsync(request.Email);
|
|
|
|
return result.Type switch
|
|
{
|
|
// Aus Sicherheitsgründen wird immer eine Erfolgsmeldung zurückgegeben, es sei denn, die E-Mail wurde bereits bestätigt.
|
|
ServiceResultType.Success => Ok(new { Message = "Wenn ein Konto mit dieser E-Mail-Adresse existiert und noch nicht bestätigt wurde, wurde eine neue E-Mail gesendet." }),
|
|
ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }),
|
|
_ => Ok(new { Message = "Wenn ein Konto mit dieser E-Mail-Adresse existiert und noch nicht bestätigt wurde, wurde eine neue E-Mail gesendet." })
|
|
};
|
|
}
|
|
|
|
[HttpPost("forgot-password")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
|
|
public async Task<IActionResult> ForgotPassword([FromBody] ForgotPasswordRequestDto request)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
|
|
// Diese Methode gibt bewusst kein Fehlerdetail zurück, um das Vorhandensein von E-Mail-Adressen nicht preiszugeben.
|
|
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." });
|
|
}
|
|
|
|
[HttpPost("reset-password")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> ResetPassword([FromBody] ResetPasswordDto request)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
|
|
var result = await _authService.ResetPasswordAsync(request);
|
|
|
|
return result.Type switch
|
|
{
|
|
ServiceResultType.Success => Ok(new { Message = "Ihr Passwort wurde erfolgreich zurückgesetzt." }),
|
|
_ => BadRequest(new { Message = result.ErrorMessage ?? "Das Passwort konnte nicht zurückgesetzt werden. Der Link ist möglicherweise ungültig oder abgelaufen." })
|
|
};
|
|
}
|
|
}
|
|
} |