| [5b42c8d] | 1 | using System.IdentityModel.Tokens.Jwt;
|
|---|
| 2 | using System.Security.Claims;
|
|---|
| 3 | using System.Text;
|
|---|
| 4 | using BCrypt.Net;
|
|---|
| [3347c1b] | 5 | using iknow_api.Services;
|
|---|
| [e49c3ed] | 6 | using iknow_api.Models;
|
|---|
| [277bd72] | 7 | using iknow_api.DTOs;
|
|---|
| 8 | using iknow_api.Repositories;
|
|---|
| [5b42c8d] | 9 | using Microsoft.IdentityModel.Tokens;
|
|---|
| 10 |
|
|---|
| [277bd72] | 11 | namespace iknow_api.Services
|
|---|
| [5b42c8d] | 12 | {
|
|---|
| 13 | public class AuthService : IAuthService
|
|---|
| 14 | {
|
|---|
| 15 | private readonly IUserRepository _userRepository;
|
|---|
| [3347c1b] | 16 | private readonly IRefreshTokenService _refreshTokenService;
|
|---|
| [5b42c8d] | 17 | private readonly IConfiguration _configuration;
|
|---|
| 18 |
|
|---|
| [3347c1b] | 19 | public AuthService(IUserRepository userRepository, IConfiguration configuration, IRefreshTokenService refreshTokenService)
|
|---|
| [5b42c8d] | 20 | {
|
|---|
| 21 | _userRepository = userRepository;
|
|---|
| 22 | _configuration = configuration;
|
|---|
| [3347c1b] | 23 | _refreshTokenService = refreshTokenService;
|
|---|
| [5b42c8d] | 24 | }
|
|---|
| 25 |
|
|---|
| [c7d4a59] | 26 | public async Task<bool> RegisterAsync(RegisterDto registerDto)
|
|---|
| [5b42c8d] | 27 | {
|
|---|
| [c7d4a59] | 28 | if (await _userRepository.UserExistsAsync(registerDto.Email))
|
|---|
| [5b42c8d] | 29 | return false;
|
|---|
| 30 |
|
|---|
| 31 | var user = new User
|
|---|
| 32 | {
|
|---|
| [c7d4a59] | 33 | Name = registerDto.Name,
|
|---|
| 34 | Surname = registerDto.Surname,
|
|---|
| 35 | Index = registerDto.Index,
|
|---|
| 36 | Email = registerDto.Email,
|
|---|
| 37 | PasswordHash = BCrypt.Net.BCrypt.HashPassword(registerDto.Password),
|
|---|
| 38 | Bday = DateTime.SpecifyKind(registerDto.Bday, DateTimeKind.Utc),
|
|---|
| 39 | CreatedAt = DateTime.UtcNow,
|
|---|
| [e49c3ed] | 40 | Role = (Models.UserRole)registerDto.Role
|
|---|
| [5b42c8d] | 41 | };
|
|---|
| 42 |
|
|---|
| 43 | await _userRepository.AddUserAsync(user);
|
|---|
| 44 | return true;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| [3347c1b] | 47 | public async Task<AuthResultDto?> LoginAsync(LoginDto loginDto)
|
|---|
| [5b42c8d] | 48 | {
|
|---|
| [c7d4a59] | 49 | var user = await _userRepository.GetUserByUsernameAsync(loginDto.Email);
|
|---|
| 50 | if (user == null || !BCrypt.Net.BCrypt.Verify(loginDto.Password, user.PasswordHash))
|
|---|
| [5b42c8d] | 51 | return null;
|
|---|
| 52 |
|
|---|
| [3347c1b] | 53 | var result = new AuthResultDto
|
|---|
| 54 | {
|
|---|
| 55 | AccessToken = CreateToken(user)
|
|---|
| 56 | };
|
|---|
| 57 |
|
|---|
| 58 | if (loginDto.GenerateRefreshToken)
|
|---|
| 59 | {
|
|---|
| 60 | //int userid = _userRepository.GetUserByUsernameAsync(loginDto.Email).Id;
|
|---|
| 61 | result.RefreshToken = await _refreshTokenService.GenerateRefreshToken(6);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | return result;
|
|---|
| [5b42c8d] | 65 | }
|
|---|
| 66 |
|
|---|
| 67 | public async Task<int> GetUsersCountAsync()
|
|---|
| 68 | {
|
|---|
| 69 | return await _userRepository.GetUsersCountAsync();
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | private string CreateToken(User user)
|
|---|
| 73 | {
|
|---|
| 74 | var claims = new[]
|
|---|
| 75 | {
|
|---|
| 76 | new Claim("id", user.Id.ToString()),
|
|---|
| [e49c3ed] | 77 | new Claim("email", user.Email ?? string.Empty),
|
|---|
| [c7d4a59] | 78 | new Claim("role", user.Role.ToString())
|
|---|
| [5b42c8d] | 79 | };
|
|---|
| 80 |
|
|---|
| 81 | var keyString = _configuration["Jwt:Key"];
|
|---|
| 82 | if (string.IsNullOrEmpty(keyString))
|
|---|
| 83 | throw new Exception("JWT Key is missing in configuration!");
|
|---|
| 84 |
|
|---|
| 85 | var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(keyString));
|
|---|
| 86 | var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
|---|
| 87 |
|
|---|
| 88 | var token = new JwtSecurityToken(
|
|---|
| [c7d4a59] | 89 | issuer: "iknow-api",
|
|---|
| 90 | audience: "iknow-api",
|
|---|
| [5b42c8d] | 91 | claims: claims,
|
|---|
| [c7d4a59] | 92 | expires: DateTime.UtcNow.AddHours(1),
|
|---|
| [5b42c8d] | 93 | signingCredentials: creds
|
|---|
| 94 | );
|
|---|
| 95 |
|
|---|
| 96 | return new JwtSecurityTokenHandler().WriteToken(token);
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 | } |
|---|