using AsbCloudApp.Data; using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Threading; using System.Threading.Tasks; using Swashbuckle.AspNetCore.Annotations; namespace AsbCloudWebApi.Controllers { [Route("/auth")] [ApiController] public class AuthController : ControllerBase { private readonly IAuthService authService; public AuthController(IAuthService authService) { this.authService = authService; } /// /// Аутентификация пользователя /// /// /// Токен отмены задачи /// новый токен /// логин и пароль не подходят [AllowAnonymous] [HttpPost("login")] [SwaggerOperation(OperationId = "login")] [ProducesResponseType(typeof(UserTokenDto), (int)System.Net.HttpStatusCode.OK)] public async Task LoginAsync([FromBody] AuthDto auth, CancellationToken token = default) { var userToken = await authService.LoginAsync(auth.Login, auth.Password, token).ConfigureAwait(false); if (userToken is null) BadRequest();//"wrong login or password" return Ok(userToken); } /// /// Продление срока действия токена /// /// новый токен [Authorize] [HttpGet("refresh")] public IActionResult Refresh() { var newToken = authService.Refresh(User); return Ok(newToken); } } }