|
Last change
on this file since fc7b4b4 was fc7b4b4, checked in by stefansaveski <stefansaveski19@…>, 11 months ago |
|
Implement JWT Authentication and Update User Schema
Replaced repository interface with FinXaccesApi.Repositories in UsersControllers.cs. Updated database schema to use PasswordHash instead of Password. Added new migration files reflecting these changes. Updated service registration in Program.cs to include IAuthService and IUserRepository. Refactored UserRepository.cs and IUserRepository.cs with new methods and namespace changes. Commented out UserService.cs. Updated appsettings with new connection strings and JWT settings. Added BCrypt.Net-Next and System.IdentityModel.Tokens.Jwt packages. Introduced AuthController, UserDTO, AuthService, and IAuthService for handling authentication and user management.
|
-
Property mode
set to
100644
|
|
File size:
1.5 KB
|
| Line | |
|---|
| 1 | using FinXaccesApi.DTOs;
|
|---|
| 2 | using FinXaccesApi.Services;
|
|---|
| 3 | using Microsoft.AspNetCore.Mvc;
|
|---|
| 4 |
|
|---|
| 5 | namespace FinXaccesApi.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] UserDto 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] UserDto 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.