Changeset c7d4a59
- Timestamp:
- 10/22/25 23:34:14 (11 months ago)
- Branches:
- master
- Children:
- 9538edc
- Parents:
- 277bd72
- Location:
- iknow-api
- Files:
-
- 3 deleted
- 4 edited
-
Controllers/AuthController.cs (modified) (3 diffs)
-
Controllers/UsersControllers.cs (deleted)
-
DTOs/UserDTO.cs (modified) (1 diff)
-
Services/Implementations/AuthService.cs (modified) (4 diffs)
-
Services/Implementations/UserService.cs (deleted)
-
Services/Interfaces/IAuthService.cs (modified) (1 diff)
-
Services/Interfaces/IUserService.cs (deleted)
Legend:
- Unmodified
- Added
- Removed
-
iknow-api/Controllers/AuthController.cs
r277bd72 rc7d4a59 2 2 using iknow_api.Services; 3 3 using Microsoft.AspNetCore.Mvc; 4 //test 4 5 5 namespace iknow_api.Controllers 6 6 { … … 31 31 32 32 [HttpPost("register")] 33 public async Task<IActionResult> Register([FromBody] UserDto request)33 public async Task<IActionResult> Register([FromBody] RegisterDto request) 34 34 { 35 35 var result = await _authService.RegisterAsync(request); … … 39 39 40 40 [HttpPost("login")] 41 public async Task<IActionResult> Login([FromBody] UserDto request)41 public async Task<IActionResult> Login([FromBody] LoginDto request) 42 42 { 43 43 var token = await _authService.LoginAsync(request); -
iknow-api/DTOs/UserDTO.cs
r277bd72 rc7d4a59 1 1 namespace iknow_api.DTOs 2 2 { 3 public class UserDto 3 // Login DTO 4 public class LoginDto 4 5 { 5 public string Username { get; set; } = null!; 6 public string Password { get; set; } = null!; 6 public string Email { get; set; } = string.Empty; 7 public string Password { get; set; } = string.Empty; 8 } 9 10 // Registration DTO 11 public class RegisterDto 12 { 13 public string Name { get; set; } = string.Empty; 14 public string Surname { get; set; } = string.Empty; 15 public string? Index { get; set; } 16 public string Email { get; set; } = string.Empty; 17 public string Password { get; set; } = string.Empty; 18 public DateTime Bday { get; set; } 19 public UserRole Role { get; set; } 20 } 21 22 // User response DTO (for returning user data without password) 23 public class UserResponseDto 24 { 25 public int Id { get; set; } 26 public string? Name { get; set; } 27 public string? Surname { get; set; } 28 public string? Index { get; set; } 29 public string? Email { get; set; } 30 public DateTime Bday { get; set; } 31 public DateTime CreatedAt { get; set; } 32 public UserRole Role { get; set; } 33 } 34 35 // User role enum 36 public enum UserRole 37 { 38 Admin, 39 Professor, 40 Student 7 41 } 8 42 } -
iknow-api/Services/Implementations/AuthService.cs
r277bd72 rc7d4a59 22 22 } 23 23 24 public async Task<bool> RegisterAsync( UserDto userDto)24 public async Task<bool> RegisterAsync(RegisterDto registerDto) 25 25 { 26 if (await _userRepository.UserExistsAsync( userDto.Username))26 if (await _userRepository.UserExistsAsync(registerDto.Email)) 27 27 return false; 28 28 29 29 var user = new User 30 30 { 31 Email = userDto.Username, 32 PasswordHash = BCrypt.Net.BCrypt.HashPassword(userDto.Password) 31 Name = registerDto.Name, 32 Surname = registerDto.Surname, 33 Index = registerDto.Index, 34 Email = registerDto.Email, 35 PasswordHash = BCrypt.Net.BCrypt.HashPassword(registerDto.Password), 36 Bday = DateTime.SpecifyKind(registerDto.Bday, DateTimeKind.Utc), 37 CreatedAt = DateTime.UtcNow, 38 Role = (Core.Models.UserRole)registerDto.Role 33 39 }; 34 40 … … 37 43 } 38 44 39 public async Task<string?> LoginAsync( UserDto userDto)45 public async Task<string?> LoginAsync(LoginDto loginDto) 40 46 { 41 var user = await _userRepository.GetUserByUsernameAsync( userDto.Username);42 if (user == null || !BCrypt.Net.BCrypt.Verify( userDto.Password, user.PasswordHash))47 var user = await _userRepository.GetUserByUsernameAsync(loginDto.Email); 48 if (user == null || !BCrypt.Net.BCrypt.Verify(loginDto.Password, user.PasswordHash)) 43 49 return null; 44 50 … … 56 62 { 57 63 new Claim("id", user.Id.ToString()), 58 new Claim("username", user.Email) 64 new Claim("username", user.Email ?? string.Empty), 65 new Claim("role", user.Role.ToString()) 59 66 }; 60 67 … … 67 74 68 75 var token = new JwtSecurityToken( 69 issuer: " finxacces-api",70 audience: " finxacces-api",76 issuer: "iknow-api", 77 audience: "iknow-api", 71 78 claims: claims, 72 expires: DateTime. Now.AddHours(1),79 expires: DateTime.UtcNow.AddHours(1), 73 80 signingCredentials: creds 74 81 ); -
iknow-api/Services/Interfaces/IAuthService.cs
r277bd72 rc7d4a59 5 5 public interface IAuthService 6 6 { 7 Task<bool> RegisterAsync( UserDto userDto);8 Task<string?> LoginAsync( UserDto userDto);7 Task<bool> RegisterAsync(RegisterDto registerDto); 8 Task<string?> LoginAsync(LoginDto loginDto); 9 9 Task<int> GetUsersCountAsync(); 10 10 }
Note:
See TracChangeset
for help on using the changeset viewer.
