add "me" endpoint

This commit is contained in:
Вячеслав 2025-05-13 18:09:06 +05:00
parent 98b38f645f
commit ae1a8d2b97
7 changed files with 49 additions and 10 deletions

View file

@ -1,6 +1,9 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SurveyBackend.Core.Contexts;
using SurveyBackend.Core.Services;
using SurveyBackend.DTOs;
using SurveyBackend.DTOs.User;
using SurveyBackend.Mappers;
using IAuthorizationService = SurveyBackend.Core.Services.IAuthorizationService;
@ -14,14 +17,19 @@ namespace SurveyBackend.Controllers;
public class AuthController : ControllerBase
{
private readonly IAuthorizationService _authorizationService;
private readonly IUserContext _userContext;
private readonly IUserService _userService;
/// <summary>
/// Нет ну вы прикалываетесь что ли мне ща каждый контроллер описывать?
/// </summary>
/// <param name="authorizationService"></param>
public AuthController(IAuthorizationService authorizationService)
public AuthController(IAuthorizationService authorizationService, IUserContext userContext,
IUserService userService)
{
_authorizationService = authorizationService;
_userContext = userContext;
_userService = userService;
}
/// <summary>
@ -55,7 +63,19 @@ public class AuthController : ControllerBase
public async Task<IActionResult> Register([FromBody] UserRegistrationDto registerData)
{
var token = await _authorizationService.RegisterUser(
AuthMapper.UserRegistrationToModel(registerData));
UserMapper.UserRegistrationToModel(registerData));
return Ok(new { token = token });
}
[Authorize]
[HttpGet("me")]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IActionResult> GetMe()
{
var userId = _userContext.UserId;
var user = await _userService.GetUserById(userId);
var result = UserMapper.ModelToOutput(user);
return Ok(result);
}
}