Вы что издеваетесь?
Вот вам код, добавляете свой базовый класс и вперед.
using EggCloud.Code;
using EggCloud.Data.Abstract;
using EggCloud.Model.Entities;
using EggCloud.Services.Abstraction;
using EggCloud.ViewModels.Auth;
using Microsoft.AspNetCore.Mvc;
namespace EggCloud.Controllers;
/// <summary>
/// Контроллер аутентификации
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class AuthController : ControllerBase
{
private readonly IAuthService authService;
private readonly IUserRepository userRepository;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="authService">сервис авторизации</param>
/// <param name="userRepository">пользовательский репозиторий</param>
public AuthController(IAuthService authService, IUserRepository userRepository)
{
this.authService = authService;
this.userRepository = userRepository;
}
/// <summary>
/// Вход в систему
/// </summary>
/// <param name="model">Модель входа в систему</param>
/// <returns></returns>
[HttpPost("login")]
public ActionResult<AuthData> Post([FromBody] LoginViewModel model)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var user = userRepository.GetSingle(u => u.Email == model.Email);
if (user == null)
{
return BadRequest(new { error = "no user with this email" });
}
var passwordValid = authService.VerifyPassword(model.Password, user.Password);
if (!passwordValid)
{
return BadRequest(new { error = "invalid password" });
}
return authService.GetAuthData(user.Id, user.IsAdmin,user.Username);
}
/// <summary>
/// Регистрация в системе
/// </summary>
/// <param name="model">Модель регистрации</param>
/// <returns></returns>
[HttpPost("register")]
public ActionResult<AuthData> Post([FromBody] RegisterViewModel model)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var emailUniq = userRepository.isEmailUniq(model.Email);
if (!emailUniq) return BadRequest(new { error = "user with this email already exists" });
var user = new User
{
Username = model.Username,
Email = model.Email,
Password = authService.HashPassword(model.Password),
RateId = Rate.Default.Id,
PaidUntil = Utils.GetTimeStamp()
};
userRepository.Add(user);
userRepository.Commit();
return authService.GetAuthData(user.Id, user.IsAdmin, user.Username);
}
}