diff --git a/dotnet/AipsCore/Infrastructure/Persistence/User/User.cs b/dotnet/AipsCore/Infrastructure/Persistence/User/User.cs index 3d6f3a2..0bc6504 100644 --- a/dotnet/AipsCore/Infrastructure/Persistence/User/User.cs +++ b/dotnet/AipsCore/Infrastructure/Persistence/User/User.cs @@ -1,21 +1,11 @@ using System.ComponentModel.DataAnnotations; +using Microsoft.AspNetCore.Identity; namespace AipsCore.Infrastructure.Persistence.User; -public class User +public class User : IdentityUser { - [Key] - public Guid Id { get; set; } - - [Required] - [MaxLength(255)] - public string Username { get; set; } = null!; - - [Required] - [MaxLength(255)] - public string Email { get; set; } = null!; - public DateTime CreatedAt { get; set; } public DateTime? DeletedAt { get; set; } diff --git a/dotnet/AipsCore/Infrastructure/Persistence/User/UserRepository.cs b/dotnet/AipsCore/Infrastructure/Persistence/User/UserRepository.cs index 2e4a126..e4f6965 100644 --- a/dotnet/AipsCore/Infrastructure/Persistence/User/UserRepository.cs +++ b/dotnet/AipsCore/Infrastructure/Persistence/User/UserRepository.cs @@ -1,15 +1,21 @@ +using AipsCore.Domain.Common.Validation; using AipsCore.Domain.Models.User.External; +using AipsCore.Domain.Models.User.Validation; using AipsCore.Domain.Models.User.ValueObjects; using AipsCore.Infrastructure.Persistence.Abstract; using AipsCore.Infrastructure.Persistence.Db; +using Microsoft.AspNetCore.Identity; namespace AipsCore.Infrastructure.Persistence.User; public class UserRepository : AbstractRepository, IUserRepository { - public UserRepository(AipsDbContext context) + private readonly UserManager _userManager; + + public UserRepository(AipsDbContext context, UserManager userManager) : base(context) { + _userManager = userManager; } protected override Domain.Models.User.User MapToModel(User entity) @@ -17,7 +23,7 @@ public class UserRepository : AbstractRepository e.Description)); + throw new Exception($"User registration failed: {errors}"); + } + + //Realno nebitno, ali postoji infrastruktura ako treba da se koristi kasnije + //await _userManager.AddToRoleAsync(entity, "User"); + } + + public async Task LoginWithEmailAndPasswordAsync(string email, string password, CancellationToken cancellationToken = default) + { + var entity = await _userManager.FindByEmailAsync(email); + + if (entity is null) + { + throw new ValidationException(UserErrors.LoginErrorUserNotFoundByEmail(email)); + } + + var isPasswordValid = await _userManager.CheckPasswordAsync(entity, password); + + if (!isPasswordValid) + { + throw new ValidationException(UserErrors.LoginErrorIncorrectPassword()); + } + + var roles = await _userManager.GetRolesAsync(entity); + + return new LoginResult(MapToModel(entity), roles); + } } \ No newline at end of file