This commit is contained in:
Veljko Tosic
2026-02-12 22:04:48 +01:00
parent 5ecf8435f4
commit 8253e8bc3f
22 changed files with 250 additions and 126 deletions

View File

@@ -1,8 +1,9 @@
using AipsCore.Domain.Models.User;
using AipsCore.Domain.Models.User.External;
namespace AipsCore.Application.Abstract.UserContext;
public interface ITokenProvider
{
string Generate(User user, IList<string> roles);
string Generate(User user, IList<UserRole> roles);
}

View File

@@ -1,3 +0,0 @@
namespace AipsCore.Application.Authentication;
public record Token(string Value);

View File

@@ -0,0 +1,9 @@
using AipsCore.Domain.Models.User;
namespace AipsCore.Application.Common.Authentication;
public interface IAuthService
{
Task SignUpWithPasswordAsync(User user, string password, CancellationToken cancellationToken = default);
Task<LoginResult> LoginWithEmailAndPasswordAsync(string email, string password, CancellationToken cancellationToken = default);
}

View File

@@ -0,0 +1,6 @@
using AipsCore.Domain.Models.User;
using AipsCore.Domain.Models.User.External;
namespace AipsCore.Application.Common.Authentication;
public record LoginResult(User User, IList<UserRole> Roles);

View File

@@ -0,0 +1,3 @@
namespace AipsCore.Application.Common.Authentication;
public record Token(string Value);

View File

@@ -1,5 +1,5 @@
using AipsCore.Application.Abstract.Command;
using AipsCore.Application.Authentication;
using AipsCore.Application.Common.Authentication;
namespace AipsCore.Application.Models.User.Command.LogIn;

View File

@@ -1,6 +1,6 @@
using AipsCore.Application.Abstract.Command;
using AipsCore.Application.Abstract.UserContext;
using AipsCore.Application.Authentication;
using AipsCore.Application.Common.Authentication;
using AipsCore.Domain.Abstract;
using AipsCore.Domain.Models.User.External;
@@ -10,18 +10,18 @@ public class LogInUserCommandHandler : ICommandHandler<LogInUserCommand, Token>
{
private readonly IUserRepository _userRepository;
private readonly ITokenProvider _tokenProvider;
private readonly IUnitOfWork _unitOfWork;
public LogInUserCommandHandler(IUserRepository userRepository, ITokenProvider tokenProvider, IUnitOfWork unitOfWork)
private readonly IAuthService _authService;
public LogInUserCommandHandler(IUserRepository userRepository, ITokenProvider tokenProvider, IAuthService authService)
{
_userRepository = userRepository;
_tokenProvider = tokenProvider;
_unitOfWork = unitOfWork;
_authService = authService;
}
public async Task<Token> Handle(LogInUserCommand command, CancellationToken cancellationToken = default)
{
var loginResult = await _userRepository.LoginWithEmailAndPasswordAsync(command.Email, command.Password, cancellationToken);
var loginResult = await _authService.LoginWithEmailAndPasswordAsync(command.Email, command.Password, cancellationToken);
return new Token(_tokenProvider.Generate(loginResult.User, loginResult.Roles));
}

View File

@@ -1,4 +1,5 @@
using AipsCore.Application.Abstract.Command;
using AipsCore.Application.Common.Authentication;
using AipsCore.Domain.Abstract;
using AipsCore.Domain.Models.User.External;
using AipsCore.Domain.Models.User.ValueObjects;
@@ -8,19 +9,19 @@ namespace AipsCore.Application.Models.User.Command.SignUp;
public class SignUpUserCommandHandler : ICommandHandler<SignUpUserCommand, UserId>
{
private readonly IUserRepository _userRepository;
private readonly IUnitOfWork _unitOfWork;
public SignUpUserCommandHandler(IUserRepository userRepository, IUnitOfWork unitOfWork)
private readonly IAuthService _authService;
public SignUpUserCommandHandler(IUserRepository userRepository, IAuthService authService)
{
_userRepository = userRepository;
_unitOfWork = unitOfWork;
_authService = authService;
}
public async Task<UserId> Handle(SignUpUserCommand command, CancellationToken cancellationToken = default)
{
var user = Domain.Models.User.User.Create(command.Email, command.Username);
await _userRepository.SignUpWithPasswordAsync(user, command.Password, cancellationToken);
await _authService.SignUpWithPasswordAsync(user, command.Password, cancellationToken);
return user.Id;
}