main
|
Last change
on this file since 3ae4bab was 3ae4bab, checked in by kikisrbinoska <srbinoskakristina07@…>, 3 days ago |
|
Changes for log in
|
-
Property mode
set to
100644
|
|
File size:
1.3 KB
|
| Line | |
|---|
| 1 | using ChapterX.Domain.Repositories;
|
|---|
| 2 | using MediatR;
|
|---|
| 3 |
|
|---|
| 4 | namespace ChapterX.Application.Auth
|
|---|
| 5 | {
|
|---|
| 6 | public class LoginHandler : IRequestHandler<LoginRequest, LoginResponse>
|
|---|
| 7 | {
|
|---|
| 8 | private readonly IUserRepository _userRepository;
|
|---|
| 9 | private readonly IJwtTokenService _jwtTokenService;
|
|---|
| 10 |
|
|---|
| 11 | public LoginHandler(IUserRepository userRepository, IJwtTokenService jwtTokenService)
|
|---|
| 12 | {
|
|---|
| 13 | _userRepository = userRepository;
|
|---|
| 14 | _jwtTokenService = jwtTokenService;
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | public async Task<LoginResponse> Handle(LoginRequest request, CancellationToken cancellationToken)
|
|---|
| 18 | {
|
|---|
| 19 | var user = await _userRepository.GetByEmailAsync(request.Email, cancellationToken)
|
|---|
| 20 | ?? throw new UnauthorizedAccessException("No account found with this email. Please register first.");
|
|---|
| 21 |
|
|---|
| 22 | if (!BCrypt.Net.BCrypt.Verify(request.Password, user.Password))
|
|---|
| 23 | throw new UnauthorizedAccessException("Incorrect password. Please try again.");
|
|---|
| 24 |
|
|---|
| 25 | var token = _jwtTokenService.GenerateToken(user);
|
|---|
| 26 |
|
|---|
| 27 | var role = user.Admin != null ? "admin"
|
|---|
| 28 | : user.Writer != null ? "writer"
|
|---|
| 29 | : "regular";
|
|---|
| 30 |
|
|---|
| 31 | return new LoginResponse(token, user.Id, user.Username, user.Email, user.Name, user.Surname, role);
|
|---|
| 32 | }
|
|---|
| 33 | }
|
|---|
| 34 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.