resend integration
This commit is contained in:
@@ -1,92 +1,149 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
// 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;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Webshop.Application.Services.Customers;
|
||||
|
||||
namespace Webshop.Api.Controllers.Auth // Beachten Sie den Namespace
|
||||
namespace Webshop.Api.Controllers.Auth
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/v1/auth/[controller]")] // z.B. /api/v1/auth
|
||||
[Route("api/v1/auth")]
|
||||
public class AuthController : ControllerBase
|
||||
{
|
||||
private readonly IAuthService _authService;
|
||||
private readonly ICustomerService _customerService;
|
||||
|
||||
|
||||
public AuthController(IAuthService authService, ICustomerService customerService)
|
||||
public AuthController(IAuthService authService)
|
||||
{
|
||||
_authService = authService;
|
||||
_customerService = customerService;
|
||||
}
|
||||
|
||||
|
||||
[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);
|
||||
// Wenn Registrierung erfolgreich, aber E-Mail-Bestätigung aussteht, sollte der Token leer sein
|
||||
if (result.IsAuthSuccessful && string.IsNullOrEmpty(result.Token))
|
||||
|
||||
if (result.Type == ServiceResultType.Success)
|
||||
{
|
||||
return Ok(new { Message = result.ErrorMessage, Email = result.Email }); // Sende Status und Info, dass Mail gesendet
|
||||
return Ok(new { Message = "Registrierung erfolgreich. Bitte bestätigen Sie Ihre E-Mail-Adresse." });
|
||||
}
|
||||
if (!result.IsAuthSuccessful) return BadRequest(new { Message = result.ErrorMessage });
|
||||
return Ok(result);
|
||||
|
||||
return BadRequest(new { Message = result.ErrorMessage });
|
||||
}
|
||||
|
||||
[HttpPost("login/customer")] // /api/v1/auth/login/customer (für Kunden-Login)
|
||||
[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.IsAuthSuccessful) return Unauthorized(new { Message = result.ErrorMessage });
|
||||
return Ok(result);
|
||||
|
||||
if (result.Type == ServiceResultType.Success)
|
||||
{
|
||||
return Ok(result.Value);
|
||||
}
|
||||
|
||||
return Unauthorized(new { Message = result.ErrorMessage });
|
||||
}
|
||||
|
||||
[HttpPost("login/admin")] // /api/v1/auth/login/admin (für Admin-Dashboard Login)
|
||||
[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);
|
||||
if (!result.IsAuthSuccessful) return Unauthorized(new { Message = result.ErrorMessage });
|
||||
return Ok(result);
|
||||
|
||||
return result.Type switch
|
||||
{
|
||||
ServiceResultType.Success => Ok(result.Value),
|
||||
ServiceResultType.Forbidden => Forbid(),
|
||||
_ => Unauthorized(new { Message = result.ErrorMessage })
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("confirm-email")] // Für Registrierungsbestätigung
|
||||
[HttpGet("confirm-email")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
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!" });
|
||||
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 (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 });
|
||||
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 });
|
||||
}
|
||||
|
||||
[HttpGet("change-email-confirm")] // Für E-Mail-Änderungsbestätigung
|
||||
// << NEUER ENDPUNKT FÜR PASSWORT VERGESSEN >>
|
||||
[HttpPost("forgot-password")]
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> ConfirmEmailChange([FromQuery] string userId, [FromQuery] string newEmail, [FromQuery] string token)
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> ForgotPassword([FromBody] ForgotPasswordRequestDto request)
|
||||
{
|
||||
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!" });
|
||||
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.
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -316,4 +316,10 @@ app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
|
||||
//zuletzt <20>berarbeitet categories und discounds fehlermeldungen und results. next is order
|
||||
//zuletzt <20>berarbeitet categories und discounds fehlermeldungen und results. next is order
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -11,12 +11,15 @@
|
||||
},
|
||||
"JwtSettings": {
|
||||
"Secret": "MEIN_DEBUG_PASSWORT",
|
||||
"Issuer": "https://dein-webshop.com",
|
||||
"Issuer": "https://shopsolution-backend.tzbre.de",
|
||||
"Audience": "webshop-users",
|
||||
"ExpirationMinutes": 120
|
||||
},
|
||||
"Resend": {
|
||||
"ApiToken": "re_Zd6aFcvz_CqSa9Krs7WCHXjngDVLTYhGv"
|
||||
"ApiToken": "IHRE_RESEND_API_TOKEN_HIER",
|
||||
"FromEmail": "onboarding@resend.dev"
|
||||
},
|
||||
"App:BaseUrl": "https://shopsolution-backend.tzbre.dev"
|
||||
"App": {
|
||||
"ClientUrl": "https://shopsolution-backend.tzbre.de" //erstmal backend url oder loak "ClientUrl": "http://localhost:3000"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user