mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-14 17:54:42 +00:00
39 lines
1.4 KiB
C#
39 lines
1.4 KiB
C#
using Domain.Exceptions;
|
|
using Features.Users.Dtos;
|
|
using Features.Users.Identity;
|
|
using Features.Users.Services;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
namespace Features.Users.Commands.Authentication.Login;
|
|
|
|
/// <summary>
|
|
/// Handles <see cref="LoginCommand" /> by verifying credentials via
|
|
/// <see cref="UserManager{TUser}" /> and issuing access/refresh tokens.
|
|
/// </summary>
|
|
public class LoginHandler(UserManager<ApplicationUser> userManager, ITokenService tokenService)
|
|
: IRequestHandler<LoginCommand, LoginPayload>
|
|
{
|
|
/// <exception cref="UnauthorizedException">
|
|
/// Thrown when the username does not match any account or the supplied password does not match
|
|
/// the stored hash.
|
|
/// </exception>
|
|
public async Task<LoginPayload> Handle(
|
|
LoginCommand request,
|
|
CancellationToken cancellationToken
|
|
)
|
|
{
|
|
ApplicationUser user =
|
|
await userManager.FindByNameAsync(request.Username)
|
|
?? throw new UnauthorizedException("Invalid username or password.");
|
|
|
|
if (!await userManager.CheckPasswordAsync(user, request.Password))
|
|
throw new UnauthorizedException("Invalid username or password.");
|
|
|
|
string accessToken = tokenService.GenerateAccessToken(user.Id, user.UserName);
|
|
string refreshToken = tokenService.GenerateRefreshToken(user.Id, user.UserName);
|
|
|
|
return new LoginPayload(user.Id, user.UserName, refreshToken, accessToken);
|
|
}
|
|
}
|