source: iknow-api/Controllers/AuthController.cs@ 848c1c5

Last change on this file since 848c1c5 was c2f79d7, checked in by Stefan-Saveski <stefan@…>, 10 months ago

Refactor and enhance application structure

Enhanced error handling in AuthController with detailed exception handling for user registration. Updated UserDTO to improve JSON serialization and renamed enums for clarity. Introduced new entities (Major, PassedSubject, etc.) and seeded initial data in AppDbContext.

Replaced legacy migrations with 20251208203846_InitialCreate, reflecting the updated schema. Fixed typographical errors and improved JSON serialization in Program.cs with case-insensitive property matching and enum handling.

Refactored repository interfaces for better null safety. Improved debugging in AuthService and updated connection strings. Added the PassedSubject entity and cleaned up redundant code and unused imports for better maintainability.

  • Property mode set to 100644
File size: 2.7 KB
Line 
1using iknow_api.DTOs;
2using iknow_api.Services;
3using Microsoft.AspNetCore.Authorization;
4using Microsoft.AspNetCore.Authentication.JwtBearer;
5using Microsoft.AspNetCore.Mvc;
6
7namespace iknow_api.Controllers
8{
9 [ApiController]
10 [Route("api/[controller]")]
11 public class AuthController : ControllerBase
12 {
13 private readonly IAuthService _authService;
14 private readonly IRefreshTokenService _refreshTokenService;
15
16 public AuthController(IAuthService authService, IRefreshTokenService refreshTokenService)
17 {
18 _authService = authService;
19 _refreshTokenService = refreshTokenService;
20 }
21
22 [HttpGet("testdb")]
23 public async Task<IActionResult> TestDb()
24 {
25 try
26 {
27 var count = await _authService.GetUsersCountAsync();
28 return Ok(new { message = "DB connection works!", usersCount = count });
29 }
30 catch (Exception ex)
31 {
32 return BadRequest(new { message = "DB connection failed", error = ex.Message });
33 }
34 }
35
36 [HttpPost("register")]
37 public async Task<IActionResult> Register([FromBody] RegisterDto request)
38 {
39 //Console.WriteLine(request);
40
41 try
42 {
43 var result = await _authService.RegisterAsync(request);
44 return Ok("User registered successfully");
45 }
46 catch (InvalidOperationException ex)
47 {
48 return BadRequest(new { message = ex.Message });
49 }
50 catch (Exception ex)
51 {
52 return StatusCode(500, new { message = "An error occurred during registration", error = ex.Message });
53 }
54 }
55
56 [HttpPost("login")]
57 public async Task<IActionResult> Login([FromBody] LoginDto request)
58 {
59 var token = await _authService.LoginAsync(request);
60 if (token == null) return Unauthorized("Invalid credentials");
61 return Ok(new { Token = token });
62 }
63
64 [Authorize]
65 [HttpGet("getstring")]
66 public async Task<IActionResult> getstring()
67 {
68 return Ok(new { message = "You are authorized!" });
69 }
70
71 [HttpPost("verify-user-token")]
72 public async Task<IActionResult> VerifyToken([FromBody] VerifyRefreshTokenDto verifyRefreshToken)
73 {
74 if (await _refreshTokenService.VerifyRefreshToken(verifyRefreshToken))
75 { return Ok(await _authService.GenerateNewJWT(verifyRefreshToken)); }
76 else
77 { return BadRequest(new { message = "No token found!" }); }
78 }
79
80 }
81}
Note: See TracBrowser for help on using the repository browser.