Modified commands to use auth and current user
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
using AipsCore.Application.Abstract.Command;
|
||||
using AipsCore.Application.Authentication;
|
||||
|
||||
namespace AipsCore.Application.Models.User.Command.LogIn;
|
||||
|
||||
public record LogInUserCommand(string Email, string Password) : ICommand<Token>;
|
||||
@@ -0,0 +1,28 @@
|
||||
using AipsCore.Application.Abstract.Command;
|
||||
using AipsCore.Application.Abstract.UserContext;
|
||||
using AipsCore.Application.Authentication;
|
||||
using AipsCore.Domain.Abstract;
|
||||
using AipsCore.Domain.Models.User.External;
|
||||
|
||||
namespace AipsCore.Application.Models.User.Command.LogIn;
|
||||
|
||||
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)
|
||||
{
|
||||
_userRepository = userRepository;
|
||||
_tokenProvider = tokenProvider;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<Token> Handle(LogInUserCommand command, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var loginResult = await _userRepository.LoginWithEmailAndPasswordAsync(command.Email, command.Password, cancellationToken);
|
||||
|
||||
return new Token(_tokenProvider.Generate(loginResult.User, loginResult.Roles));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using AipsCore.Application.Abstract.Command;
|
||||
using AipsCore.Domain.Models.User.ValueObjects;
|
||||
|
||||
namespace AipsCore.Application.Models.User.Command.SignUp;
|
||||
|
||||
public record SignUpUserCommand(
|
||||
string Username,
|
||||
string Email,
|
||||
string Password) : ICommand<UserId>;
|
||||
@@ -0,0 +1,27 @@
|
||||
using AipsCore.Application.Abstract.Command;
|
||||
using AipsCore.Domain.Abstract;
|
||||
using AipsCore.Domain.Models.User.External;
|
||||
using AipsCore.Domain.Models.User.ValueObjects;
|
||||
|
||||
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)
|
||||
{
|
||||
_userRepository = userRepository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
return user.Id;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user