source: iknow-api/Controllers/AuthController.cs@ 9538edc

Last change on this file since 9538edc was c7d4a59, checked in by stefansaveski <stefansaveski19@…>, 11 months ago

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.

  • Property mode set to 100644
File size: 1.5 KB
Line 
1using iknow_api.DTOs;
2using iknow_api.Services;
3using Microsoft.AspNetCore.Mvc;
4
5namespace iknow_api.Controllers
6{
7 [ApiController]
8 [Route("api/[controller]")]
9 public class AuthController : ControllerBase
10 {
11 private readonly IAuthService _authService;
12
13 public AuthController(IAuthService authService)
14 {
15 _authService = authService;
16 }
17
18 [HttpGet("testdb")]
19 public async Task<IActionResult> TestDb()
20 {
21 try
22 {
23 var count = await _authService.GetUsersCountAsync();
24 return Ok(new { message = "DB connection works!", usersCount = count });
25 }
26 catch (Exception ex)
27 {
28 return BadRequest(new { message = "DB connection failed", error = ex.Message });
29 }
30 }
31
32 [HttpPost("register")]
33 public async Task<IActionResult> Register([FromBody] RegisterDto request)
34 {
35 var result = await _authService.RegisterAsync(request);
36 if (!result) return BadRequest("User already exists");
37 return Ok("User registered successfully");
38 }
39
40 [HttpPost("login")]
41 public async Task<IActionResult> Login([FromBody] LoginDto request)
42 {
43 var token = await _authService.LoginAsync(request);
44 if (token == null) return Unauthorized("Invalid credentials");
45 return Ok(new { Token = token });
46 }
47 }
48}
Note: See TracBrowser for help on using the repository browser.