This commit is contained in:
Tizian.Breuch
2025-07-29 15:40:47 +02:00
parent 93a1f2411b
commit 5f8b8f3971
12 changed files with 269 additions and 60 deletions

View File

@@ -2,6 +2,8 @@
using Webshop.Application.DTOs.Auth;
using Webshop.Application.Services.Auth;
using Microsoft.AspNetCore.Authorization;
using System.ComponentModel.DataAnnotations;
using Webshop.Application.Services.Customers;
namespace Webshop.Api.Controllers.Auth // Beachten Sie den Namespace
{
@@ -10,18 +12,33 @@ namespace Webshop.Api.Controllers.Auth // Beachten Sie den Namespace
public class AuthController : ControllerBase
{
private readonly IAuthService _authService;
private readonly ICustomerService _customerService;
public AuthController(IAuthService authService)
public class ResendEmailConfirmationRequestDto
{
_authService = authService;
[Required]
[EmailAddress]
public string Email { get; set; } = string.Empty;
}
[HttpPost("register")] // /api/v1/auth/register (für Kunden)
[AllowAnonymous] // Jeder darf sich registrieren
public AuthController(IAuthService authService, ICustomerService customerService)
{
_authService = authService;
_customerService = customerService;
}
[HttpPost("register")]
[AllowAnonymous]
public async Task<IActionResult> Register([FromBody] RegisterRequestDto request)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var result = await _authService.RegisterUserAsync(request);
// Wenn Registrierung erfolgreich, aber E-Mail-Bestätigung aussteht, sollte der Token leer sein
if (result.IsAuthSuccessful && string.IsNullOrEmpty(result.Token))
{
return Ok(new { Message = result.ErrorMessage, Email = result.Email }); // Sende Status und Info, dass Mail gesendet
}
if (!result.IsAuthSuccessful) return BadRequest(new { Message = result.ErrorMessage });
return Ok(result);
}
@@ -45,5 +62,36 @@ namespace Webshop.Api.Controllers.Auth // Beachten Sie den Namespace
if (!result.IsAuthSuccessful) return Unauthorized(new { Message = result.ErrorMessage });
return Ok(result);
}
[HttpGet("confirm-email")] // Für Registrierungsbestätigung
[AllowAnonymous]
public async Task<IActionResult> ConfirmEmail([FromQuery] string userId, [FromQuery] string token)
{
var (success, errorMessage) = await _authService.ConfirmEmailAsync(userId, token);
if (!success) return BadRequest(new { Message = errorMessage });
return Ok(new { Message = "E-Mail-Adresse erfolgreich bestätigt. Sie können sich jetzt anmelden!" });
}
[HttpPost("resend-email-confirmation")]
[AllowAnonymous]
public async Task<IActionResult> ResendEmailConfirmation([FromBody] ResendEmailConfirmationRequestDto request)
{
if (string.IsNullOrEmpty(request.Email)) return BadRequest(new { Message = "E-Mail ist erforderlich." });
var (success, errorMessage) = await _authService.ResendEmailConfirmationAsync(request.Email);
if (!success) return BadRequest(new { Message = errorMessage });
return Ok(new { Message = errorMessage });
}
[HttpGet("change-email-confirm")] // Für E-Mail-Änderungsbestätigung
[AllowAnonymous]
public async Task<IActionResult> ConfirmEmailChange([FromQuery] string userId, [FromQuery] string newEmail, [FromQuery] string token)
{
var (success, errorMessage) = await _customerService.ConfirmEmailChangeAsync(userId, newEmail, token); // << Jetzt korrekt >>
if (!success) return BadRequest(new { Message = errorMessage });
return Ok(new { Message = "Ihre E-Mail-Adresse wurde erfolgreich geändert und bestätigt!" });
}
}
}

View File

@@ -8,9 +8,20 @@ using Webshop.Application.DTOs.Customers; // UpdateCustomerProfileDto
using Webshop.Application.Services;
using System.Threading.Tasks;
using Webshop.Application.Services.Customers;
using System.ComponentModel.DataAnnotations;
namespace Webshop.Api.Controllers.Customer
{
public class ChangeEmailRequestDto
{
[Required(ErrorMessage = "Neue E-Mail ist erforderlich.")]
[EmailAddress(ErrorMessage = "Ung<6E>ltiges E-Mail-Format.")]
public string NewEmail { get; set; } = string.Empty;
[Required(ErrorMessage = "Aktuelles Passwort ist erforderlich.")]
public string CurrentPassword { get; set; } = string.Empty;
}
[ApiController]
[Route("api/v1/customer/[controller]")] // z.B. /api/v1/customer/profile
[Authorize(Roles = "Customer")]
@@ -66,6 +77,19 @@ namespace Webshop.Api.Controllers.Customer
return Ok(new { Message = "Profil erfolgreich aktualisiert." });
}
// << ENTFERNT: UpdateContactInfo Endpoint >>
[HttpPost("change-email-request")] // /api/v1/customer/profile/change-email-request
public async Task<IActionResult> ChangeEmailRequest([FromBody] ChangeEmailRequestDto request) // DTO erstellen
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrEmpty(userId)) return Unauthorized(new { Message = "Benutzer-ID nicht im Token gefunden." });
var (success, errorMessage) = await _customerService.ChangeEmailAsync(userId, request.NewEmail, request.CurrentPassword);
if (!success) return BadRequest(new { Message = errorMessage });
return Ok(new { Message = errorMessage }); // Sendet Info, dass Mail gesendet wurde
}
}
}

View File

@@ -19,6 +19,7 @@ using Webshop.Application.Services.Public.Interfaces;
using Webshop.Application.Services.Customers.Interfaces;
using Webshop.Application.Services.Customers;
using Webshop.Domain.Identity;
using Resend;
var builder = WebApplication.CreateBuilder(args);
@@ -45,7 +46,7 @@ builder.Services.Configure<IdentityOptions>(options =>
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
options.SignIn.RequireConfirmedEmail = false; // F<>r einfache Entwicklung/Tests
options.SignIn.RequireConfirmedEmail = true; // F<>r einfache Entwicklung/Tests
});
@@ -93,6 +94,16 @@ builder.Services.AddScoped<IAdminSupplierService, AdminSupplierService>();
// CUSTOMER Services (sp<73>ter Implementierungen hinzuf<75>gen)
builder.Services.AddScoped<ICustomerService, CustomerService>();
// --- NEU: Resend E-Mail-Dienst konfigurieren ---
builder.Services.AddOptions(); // Stellt sicher, dass Options konfiguriert werden k<>nnen
builder.Services.AddHttpClient<ResendClient>(); // F<>gt HttpClient f<>r Resend hinzu
builder.Services.Configure<ResendClientOptions>(options =>
{
// Der API-Token kommt aus den Konfigurationen (z.B. appsettings.json oder Umgebungsvariablen)
options.ApiToken = builder.Configuration["Resend:ApiToken"]!; // << ANPASSEN >>
});
builder.Services.AddTransient<IResend, ResendClient>();
// 5. Controller und Swagger/OpenAPI hinzuf<75>gen
builder.Services.AddControllers();

View File

@@ -15,6 +15,7 @@ using Webshop.Application.DTOs.Payments;
using Webshop.Application.DTOs.Orders;
using Webshop.Application.DTOs.Discounts;
using Webshop.Application.DTOs.Categorys;
using Webshop.Api.Controllers.Auth;
namespace Webshop.Api.SwaggerFilters
{
@@ -70,6 +71,13 @@ namespace Webshop.Api.SwaggerFilters
["emailConfirmed"] = new OpenApiBoolean(true)
};
}
else if (type == typeof(AuthController.ResendEmailConfirmationRequestDto))
{
schema.Example = new OpenApiObject
{
["email"] = new OpenApiString("me@tzbre.dev")
};
}
// --- Produkte & Lieferanten ---
else if (type == typeof(ProductDto))
{

View File

@@ -18,6 +18,8 @@
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.20.1" />
<PackageReference Include="Resend" Version="0.1.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

View File

@@ -14,5 +14,9 @@
"Issuer": "https://dein-webshop.com",
"Audience": "webshop-users",
"ExpirationMinutes": 120
}
},
"Resend": {
"ApiToken": "re_7uRkboan_6M5QGD36TpnADwTwbVLCmAz9"
},
"App:BaseUrl": "https://shopsolution-backend.tzbre.dev"
}

View File

@@ -14,5 +14,9 @@
"Issuer": "https://dein-webshop.com",
"Audience": "webshop-users",
"ExpirationMinutes": 120
}
},
"Resend": {
"ApiToken": "re_7uRkboan_6M5QGD36TpnADwTwbVLCmAz9"
},
"App:BaseUrl": "https://shopsolution-backend.tzbre.dev"
}