Changeset cb8e531 for iknow-api


Ignore:
Timestamp:
10/22/25 23:34:14 (11 months ago)
Author:
stefansaveski <stefansaveski19@…>
Branches:
master
Children:
8e32210
Parents:
271178d
Message:

Refactor DTOs and remove user management

Updated AuthController to use RegisterDto and LoginDto for more specific DTO usage in registration and login methods. Removed UsersController and its methods, indicating a refactor or removal of user management functionality. Replaced UserDto with LoginDto, RegisterDto, and UserResponseDto in UserDTO.cs, and introduced a UserRole enum. Updated AuthService to use new DTOs and adjusted JWT token configuration. Removed UserService and IUserService, suggesting a redesign or deprecation of user service functionality. Updated IAuthService to align with AuthService changes.

Location:
iknow-api
Files:
3 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • iknow-api/Controllers/AuthController.cs

    r271178d rcb8e531  
    22using iknow_api.Services;
    33using Microsoft.AspNetCore.Mvc;
    4 //test
     4
    55namespace iknow_api.Controllers
    66{
     
    3131
    3232        [HttpPost("register")]
    33         public async Task<IActionResult> Register([FromBody] UserDto request)
     33        public async Task<IActionResult> Register([FromBody] RegisterDto request)
    3434        {
    3535            var result = await _authService.RegisterAsync(request);
     
    3939
    4040        [HttpPost("login")]
    41         public async Task<IActionResult> Login([FromBody] UserDto request)
     41        public async Task<IActionResult> Login([FromBody] LoginDto request)
    4242        {
    4343            var token = await _authService.LoginAsync(request);
  • iknow-api/DTOs/UserDTO.cs

    r271178d rcb8e531  
    11namespace iknow_api.DTOs
    22{
    3     public class UserDto
     3    // Login DTO
     4    public class LoginDto
    45    {
    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
    741    }
    842}
  • iknow-api/Services/Implementations/AuthService.cs

    r271178d rcb8e531  
    2222        }
    2323
    24         public async Task<bool> RegisterAsync(UserDto userDto)
     24        public async Task<bool> RegisterAsync(RegisterDto registerDto)
    2525        {
    26             if (await _userRepository.UserExistsAsync(userDto.Username))
     26            if (await _userRepository.UserExistsAsync(registerDto.Email))
    2727                return false;
    2828
    2929            var user = new User
    3030            {
    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
    3339            };
    3440
     
    3743        }
    3844
    39         public async Task<string?> LoginAsync(UserDto userDto)
     45        public async Task<string?> LoginAsync(LoginDto loginDto)
    4046        {
    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))
    4349                return null;
    4450
     
    5662            {
    5763                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())
    5966            };
    6067
     
    6774
    6875            var token = new JwtSecurityToken(
    69                 issuer: "finxacces-api",
    70                 audience: "finxacces-api",
     76                issuer: "iknow-api",
     77                audience: "iknow-api",
    7178                claims: claims,
    72                 expires: DateTime.Now.AddHours(1),
     79                expires: DateTime.UtcNow.AddHours(1),
    7380                signingCredentials: creds
    7481            );
  • iknow-api/Services/Interfaces/IAuthService.cs

    r271178d rcb8e531  
    55    public interface IAuthService
    66    {
    7         Task<bool> RegisterAsync(UserDto userDto);
    8         Task<string?> LoginAsync(UserDto userDto);
     7        Task<bool> RegisterAsync(RegisterDto registerDto);
     8        Task<string?> LoginAsync(LoginDto loginDto);
    99        Task<int> GetUsersCountAsync();
    1010    }
Note: See TracChangeset for help on using the changeset viewer.