aufrüumen

This commit is contained in:
Tizian.Breuch
2025-07-25 15:46:31 +02:00
parent 8218b860ca
commit 9e298a0458
74 changed files with 453 additions and 3557 deletions

View File

@@ -1,36 +1,36 @@
// src/Webshop.Application/Services/Auth/AuthService.cs
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using Webshop.Application.DTOs.Auth;
using System.Threading.Tasks;
using System.Collections.Generic;
using Webshop.Domain.Entities; // <-- WICHTIG: Das Using für Ihre neue Klasse hinzufügen
using Webshop.Domain.Entities;
using Webshop.Infrastructure.Data;
namespace Webshop.Application.Services.Auth
{
public class AuthService : IAuthService
{
// Ändern Sie hier IdentityUser zu ApplicationUser
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly IConfiguration _configuration;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly ApplicationDbContext _context;
public AuthService(
// Ändern Sie auch hier die Typen im Konstruktor
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IConfiguration configuration,
RoleManager<IdentityRole> roleManager)
RoleManager<IdentityRole> roleManager,
ApplicationDbContext context)
{
_userManager = userManager;
_signInManager = signInManager;
_configuration = configuration;
_roleManager = roleManager;
_context = context;
}
public async Task<AuthResponseDto> RegisterUserAsync(RegisterRequestDto request)
@@ -41,12 +41,11 @@ namespace Webshop.Application.Services.Auth
return new AuthResponseDto { IsAuthSuccessful = false, ErrorMessage = "E-Mail ist bereits registriert." };
}
// Erstellen Sie hier eine Instanz Ihrer neuen ApplicationUser-Klasse
var user = new ApplicationUser
{
Email = request.Email,
UserName = request.Email,
CreatedDate = DateTimeOffset.UtcNow // Setzen Sie das neue Feld!
CreatedDate = DateTimeOffset.UtcNow
};
var result = await _userManager.CreateAsync(user, request.Password);
@@ -56,7 +55,17 @@ namespace Webshop.Application.Services.Auth
return new AuthResponseDto { IsAuthSuccessful = false, ErrorMessage = errors };
}
// Der Rest der Logik bleibt gleich, da die Rollenverwaltung nicht vom User-Typ abhängt
// Zugehöriges kaufmännisches Kundenprofil erstellen
var customer = new Customer
{
AspNetUserId = user.Id,
FirstName = request.FirstName,
LastName = request.LastName
};
_context.Customers.Add(customer);
await _context.SaveChangesAsync();
// Dem Benutzer die "Customer"-Rolle zuweisen
if (!await _roleManager.RoleExistsAsync("Customer"))
{
await _roleManager.CreateAsync(new IdentityRole("Customer"));
@@ -78,7 +87,6 @@ namespace Webshop.Application.Services.Auth
public async Task<AuthResponseDto> LoginUserAsync(LoginRequestDto request)
{
// Diese Methode funktioniert ohne Änderungen, da der _userManager jetzt vom richtigen Typ ist.
var user = await _userManager.FindByEmailAsync(request.Email);
if (user == null)
{
@@ -91,6 +99,10 @@ namespace Webshop.Application.Services.Auth
return new AuthResponseDto { IsAuthSuccessful = false, ErrorMessage = "Ungültige Anmeldeinformationen." };
}
// Zeitstempel für "Zuletzt aktiv" aktualisieren
user.LastActive = DateTimeOffset.UtcNow;
await _userManager.UpdateAsync(user);
var roles = await _userManager.GetRolesAsync(user);
var token = await GenerateJwtToken(user, roles);
@@ -106,7 +118,6 @@ namespace Webshop.Application.Services.Auth
public async Task<AuthResponseDto> LoginAdminAsync(LoginRequestDto request)
{
// Diese Methode profitiert direkt von der Korrektur in LoginUserAsync.
var authResponse = await LoginUserAsync(request);
if (!authResponse.IsAuthSuccessful)
{
@@ -114,7 +125,6 @@ namespace Webshop.Application.Services.Auth
}
var user = await _userManager.FindByEmailAsync(request.Email);
// Stellt sicher, dass der User gefunden wurde und die Rolle "Admin" hat.
if (user == null || !await _userManager.IsInRoleAsync(user, "Admin"))
{
return new AuthResponseDto { IsAuthSuccessful = false, ErrorMessage = "Keine Berechtigung." };
@@ -123,7 +133,6 @@ namespace Webshop.Application.Services.Auth
return authResponse;
}
// Ändern Sie hier den Parameter-Typ zu ApplicationUser
private async Task<string> GenerateJwtToken(ApplicationUser user, IList<string> roles)
{
var claims = new List<Claim>

View File

@@ -1,14 +1,26 @@
// src/Webshop.Application/Services/Auth/IAuthService.cs
using System.Threading.Tasks;
using Webshop.Application.DTOs.Auth;
using System.Threading.Tasks; // Sicherstellen, dass für Task vorhanden
using System.Collections.Generic; // Sicherstellen, dass für IList<string> vorhanden
namespace Webshop.Application.Services.Auth
{
/// <summary>
/// Definiert den Vertrag für den Authentifizierungsdienst.
/// </summary>
public interface IAuthService
{
/// <summary>
/// Registriert einen neuen Benutzer und erstellt ein zugehöriges Kundenprofil.
/// </summary>
Task<AuthResponseDto> RegisterUserAsync(RegisterRequestDto request);
/// <summary>
/// Meldet einen Benutzer an und gibt einen JWT zurück.
/// </summary>
Task<AuthResponseDto> LoginUserAsync(LoginRequestDto request);
/// <summary>
/// Meldet einen Benutzer an und überprüft, ob er die Admin-Rolle hat.
/// </summary>
Task<AuthResponseDto> LoginAdminAsync(LoginRequestDto request);
}
}