aufrüumen
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user