You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

53 lines
1.7 KiB

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<ActionResult<AuthIdentity>> 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<ActionResult<GenericResponse>> 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);
}
}
}