using CoviDok.Api; using CoviDok.Api.Request; using CoviDok.Api.Response; using CoviDok.BLL; using CoviDok.BLL.Sessions; using CoviDok.BLL.User; using CoviDok.Data.SessionProviders; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace CoviDok.Controllers { [Route("api/[controller]")] [ApiController] public class AuthController : ControllerBase { SessionHandler Handler = new SessionHandler(new RedisProvider("redis")); // POST: /api/Auth/login [HttpPost("login")] public async Task> PostLogin(AuthLogin authLogin) { try { AuthIdentity authIdentity = await Auth.AuthenticateUser(authLogin.Email, authLogin.Password); if (authIdentity == null) return Unauthorized(); authIdentity.SessionId = await Handler.CreateSession(authIdentity.Role, authIdentity.UserId); return authIdentity; } catch (KeyNotFoundException) { return NotFound(); } } // POST: /api/Auth/register [HttpPost("register")] public async Task> PostRegister(AuthRegistration authRegistration) { // System.Diagnostics.Debug.WriteLine(authRegistration.ToString()); // Validate Email GenericResponse genericResponse = Auth.ValidateRegistration(authRegistration.Email, authRegistration.Password); if (genericResponse.Status == Status.Error) return genericResponse; return await Auth.CreateUser(authRegistration); } } }