/
renix
/
TravelGuide
Обзор
Документация
Войти
/
renix
/
TravelGuide
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
back/Controllers/AuthorizationController.cs
85 строк
2 KB
artem777764
Красивые письма и почти хороший контролер ResetPassword
09 апр 2025, 13:00
09 апр 2025, 13:00
d846b0c
Код
Авторство
О чём код?
using System.Threading.Tasks; using back.DTOs; using back.DTOs.EmailDTOs; using back.DTOs.UserDTO; using back.Extensions; using back.Interfaces; using back.Models; using back.Models.Entities; using back.Services; using back.Services.EntityServices; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace back.Controllers; [ApiController] [Route("auth")] public class AuthorizationController : ControllerBase { private readonly IAuthorizationServiceOur _service; private readonly JwtService _jwtService; public AuthorizationController(IAuthorizationServiceOur authorizationService, JwtService jwtService) { _service = authorizationService; _jwtService = jwtService; } [HttpPost("register")] public async Task<IActionResult> Registr([FromBody] PostUserDTO userDTO) { EntityIdDTO? entityIdDTO = await _service.RegistrAsync(userDTO); if(entityIdDTO != null) return Ok(entityIdDTO); return Conflict(); } [HttpGet("login")] public async Task<IActionResult> Login([FromQuery] string Login, [FromQuery] string Password) { UserEntity? user = await _service.LoginAsync(Login, Password); if (user == null) return NotFound(); var token = _jwtService.GenerateToken(user); Response.Cookies.Append("jwt", token, new CookieOptions { HttpOnly = true, Secure = true, Expires = DateTime.UtcNow.AddHours(12) }); EntityIdDTO entityIdDTO = new EntityIdDTO {id = user.Id}; return Ok(entityIdDTO); } [HttpGet("")] public async Task<IActionResult> Get() { return Ok(await _service.GetAllAsync()); } [HttpGet("{id}")] public async Task<IActionResult> GetById(int id) { GetUserDTO? userDTO = await _service.GetByIdAsync(id); if (userDTO == null) return NotFound(); return Ok(userDTO); } [HttpPut("Update")] public async Task<IActionResult> Update([FromBody] PutUserDTO putUserDTO) { EntityIdDTO? entityIdDTO = await _service.UpdateAsync(putUserDTO); if (entityIdDTO == null) return NotFound(); return Ok(entityIdDTO); } [HttpDelete("delete")] public async Task<IActionResult> Delete([FromBody] DeleteUserDTO deleteUserDTO) { bool isSuccessful = await _service.DeleteAsync(deleteUserDTO.Id); if(!isSuccessful) return NotFound(); return NoContent(); } }