main project split, authpassword endpoint created
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
using MediatR;
|
||||
|
||||
namespace TimetableDesigner.Backend.Services.Authentication.Core.Commands.AuthPassword;
|
||||
|
||||
public record AuthPasswordCommand
|
||||
(
|
||||
string Email,
|
||||
string Password,
|
||||
bool RememberMe
|
||||
)
|
||||
: IRequest<AuthPasswordResult>;
|
||||
@@ -0,0 +1,36 @@
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TimetableDesigner.Backend.Services.Authentication.Core.Helpers;
|
||||
using TimetableDesigner.Backend.Services.Authentication.Database;
|
||||
using TimetableDesigner.Backend.Services.Authentication.Database.Model;
|
||||
|
||||
namespace TimetableDesigner.Backend.Services.Authentication.Core.Commands.AuthPassword;
|
||||
|
||||
public class AuthPasswordHandler : IRequestHandler<AuthPasswordCommand, AuthPasswordResult>
|
||||
{
|
||||
private readonly DatabaseContext _databaseContext;
|
||||
private readonly IPasswordHasher _passwordHasher;
|
||||
|
||||
public AuthPasswordHandler(DatabaseContext databaseContext, IPasswordHasher passwordHasher)
|
||||
{
|
||||
_databaseContext = databaseContext;
|
||||
_passwordHasher = passwordHasher;
|
||||
}
|
||||
|
||||
public async Task<AuthPasswordResult> Handle(AuthPasswordCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
Account? account = await _databaseContext.Accounts.FirstOrDefaultAsync(x => x.Email == request.Email, cancellationToken);
|
||||
if (account is null)
|
||||
{
|
||||
return AuthPasswordResult.Failure();
|
||||
}
|
||||
|
||||
PasswordHashData hash = new PasswordHashData(account.Password, account.PasswordSalt);
|
||||
if (!_passwordHasher.ValidatePassword(hash, request.Password))
|
||||
{
|
||||
return AuthPasswordResult.Failure();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace TimetableDesigner.Backend.Services.Authentication.Core.Commands.AuthPassword;
|
||||
|
||||
public record AuthPasswordResult
|
||||
{
|
||||
public bool IsSuccess { get; }
|
||||
public string? AccessToken { get; }
|
||||
public string? RefreshToken { get; }
|
||||
|
||||
private AuthPasswordResult(bool isSuccess, string? accessToken, string? refreshToken)
|
||||
{
|
||||
IsSuccess = isSuccess;
|
||||
AccessToken = accessToken;
|
||||
RefreshToken = refreshToken;
|
||||
}
|
||||
|
||||
public static AuthPasswordResult Success(string accessToken, string refreshToken) =>
|
||||
new AuthPasswordResult(true, accessToken, refreshToken);
|
||||
|
||||
public static AuthPasswordResult Failure() =>
|
||||
new AuthPasswordResult(false, null, null);
|
||||
}
|
||||
Reference in New Issue
Block a user