implementado login a travez de coockies
This commit is contained in:
@@ -21,17 +21,29 @@ public class LoginController: ControllerBase
|
|||||||
string tokenString = GenerarToken(loginDto);
|
string tokenString = GenerarToken(loginDto);
|
||||||
RepositorioUsuarios.Singleton.GuardarToken(loginDto, tokenString);
|
RepositorioUsuarios.Singleton.GuardarToken(loginDto, tokenString);
|
||||||
|
|
||||||
return Ok( new {Email = loginDto.Email, Token = tokenString, Redirect = "/Menu"});
|
var cookieOptions = new CookieOptions
|
||||||
|
{
|
||||||
|
HttpOnly = true,
|
||||||
|
Secure = true,
|
||||||
|
//SameSite = SameSiteMode.Strict,
|
||||||
|
Expires = DateTimeOffset.UtcNow.AddHours(1)
|
||||||
|
};
|
||||||
|
|
||||||
|
Response.Cookies.Append("token", tokenString, cookieOptions);
|
||||||
|
return Ok( new {Email = loginDto.Email, Redirect = "/Menu"});
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("api/login/validar")]
|
[HttpPost("api/login/validar")]
|
||||||
public IActionResult Verificar([FromBody] TokenDto tokenRequest){
|
public IActionResult Verificar([FromBody] AccessDto request){
|
||||||
if (tokenRequest.Email == String.Empty ||tokenRequest.Token == string.Empty ||tokenRequest.Redirect == string.Empty)
|
|
||||||
|
Request.Cookies.TryGetValue("token", out var token);
|
||||||
|
|
||||||
|
if (request.Email == String.Empty || token == null ||request.Redirect == string.Empty)
|
||||||
{
|
{
|
||||||
return Unauthorized(new { esValido = false});
|
return Unauthorized(new { esValido = false});
|
||||||
}
|
}
|
||||||
|
|
||||||
bool esValido = RepositorioUsuarios.Singleton.CheckToken(tokenRequest);
|
bool esValido = RepositorioUsuarios.Singleton.CheckToken(request.Email, token);
|
||||||
return (esValido) ?
|
return (esValido) ?
|
||||||
Ok( new { esValido = true}) : Unauthorized( new {esValido = false});
|
Ok( new { esValido = true}) : Unauthorized( new {esValido = false});
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -11,9 +11,10 @@ builder.Services.AddCors(options =>
|
|||||||
options.AddPolicy("AllowSvelteApp",
|
options.AddPolicy("AllowSvelteApp",
|
||||||
builder =>
|
builder =>
|
||||||
{
|
{
|
||||||
builder.AllowAnyOrigin()
|
builder.WithOrigins("http://localhost:5173")
|
||||||
.AllowAnyHeader()
|
.AllowAnyHeader()
|
||||||
.AllowAnyMethod();
|
.AllowAnyMethod()
|
||||||
|
.AllowCredentials();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
namespace Entidades.Dto;
|
namespace Entidades.Dto;
|
||||||
|
|
||||||
public class TokenDto{
|
public class AccessDto {
|
||||||
public string Email { get; set; } = null!;
|
public string Email { get; set; } = null!;
|
||||||
public string Token {get; set;} = String.Empty;
|
|
||||||
public string Redirect { get; set; } = String.Empty;
|
public string Redirect { get; set; } = String.Empty;
|
||||||
}
|
}
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
export let component;
|
export let component;
|
||||||
|
|
||||||
let redirect = window.location.pathname;
|
let redirect = window.location.pathname;
|
||||||
const token = localStorage.getItem('token');
|
|
||||||
const email = localStorage.getItem('email');
|
const email = localStorage.getItem('email');
|
||||||
|
|
||||||
const handleAccess = async () => {
|
const handleAccess = async () => {
|
||||||
@@ -20,7 +20,8 @@
|
|||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify( {email, token, redirect} ),
|
body: JSON.stringify( {email, redirect} ),
|
||||||
|
credentials: "include"
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
|
|||||||
@@ -18,7 +18,8 @@
|
|||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify(data)
|
body: JSON.stringify(data),
|
||||||
|
credentials: "include"
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok){
|
if (!response.ok){
|
||||||
@@ -30,7 +31,6 @@
|
|||||||
|
|
||||||
const ret = await response.json();
|
const ret = await response.json();
|
||||||
localStorage.clear();
|
localStorage.clear();
|
||||||
localStorage.setItem('token', ret.token);
|
|
||||||
localStorage.setItem('email', ret.email);
|
localStorage.setItem('email', ret.email);
|
||||||
//setTimeout(() => console.log("50ms") ,50);
|
//setTimeout(() => console.log("50ms") ,50);
|
||||||
navigate(ret.redirect);
|
navigate(ret.redirect);
|
||||||
|
|||||||
@@ -7,22 +7,19 @@ namespace Modelo;
|
|||||||
public abstract class RepositorioBase<S>
|
public abstract class RepositorioBase<S>
|
||||||
where S : new()
|
where S : new()
|
||||||
{
|
{
|
||||||
protected AlquilaFacilContext Context { get; set; } = new AlquilaFacilContext();
|
protected AlquilaFacilContext Context { get{ return new AlquilaFacilContext();}}
|
||||||
|
|
||||||
private static readonly S instance = new();
|
private static readonly S instance = new();
|
||||||
public static S Singleton { get{return instance;}}
|
public static S Singleton { get{return instance;}}
|
||||||
|
|
||||||
public bool Guardar(){
|
public bool Guardar(AlquilaFacilContext context){
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Context.SaveChanges();
|
context.SaveChanges();
|
||||||
Context.Dispose();
|
context.Dispose();
|
||||||
Context = new AlquilaFacilContext();
|
|
||||||
ret = true;
|
ret = true;
|
||||||
} catch (DbUpdateException ex)
|
} catch (DbUpdateException ex)
|
||||||
{
|
{
|
||||||
Context = new AlquilaFacilContext();
|
|
||||||
Console.Error.WriteLine(ex.Message);
|
Console.Error.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
using Modelo;
|
|
||||||
|
|
||||||
public class RepositorioInquilinos: RepositorioBase<RepositorioInquilinos>
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
using Modelo;
|
||||||
|
|
||||||
|
public class RepositorioPropiedades: RepositorioBase<RepositorioPropiedades>
|
||||||
|
{
|
||||||
|
|
||||||
|
public bool AñadirPropiedad(){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,55 +3,46 @@ using System.Security.Cryptography;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using Entidades.Dto;
|
using Entidades.Dto;
|
||||||
using Entidades;
|
using Entidades;
|
||||||
|
using System.Reflection.Metadata.Ecma335;
|
||||||
|
|
||||||
namespace Modelo;
|
namespace Modelo;
|
||||||
|
|
||||||
public class RepositorioUsuarios: RepositorioBase<RepositorioUsuarios>
|
public class RepositorioUsuarios: RepositorioBase<RepositorioUsuarios>
|
||||||
{
|
{
|
||||||
public bool AltaCliente(CrearClienteDto cid){
|
public bool AltaCliente(CrearClienteDto cid){
|
||||||
var usu = new Usuario {
|
|
||||||
email = cid.email,
|
|
||||||
contraseña = Encoding.UTF8.GetBytes(HacerHash(cid.contraseña))
|
|
||||||
};
|
|
||||||
|
|
||||||
var cli = new Cliente {
|
var cli = new Cliente {
|
||||||
dni = cid.dni,
|
Dni = cid.dni,
|
||||||
nombre = cid.nombre,
|
Nombre = cid.nombre,
|
||||||
domicilio = cid.domicilio,
|
Domicilio = cid.domicilio,
|
||||||
apellido = cid.apellido,
|
Apellido = cid.apellido,
|
||||||
celular = cid.celular
|
Celular = cid.celular,
|
||||||
|
Email = cid.email,
|
||||||
|
Contraseña = Encoding.UTF8.GetBytes(HacerHash(cid.contraseña))
|
||||||
};
|
};
|
||||||
|
var con = Context;
|
||||||
|
var grupo = con.Grupos.Find(2);
|
||||||
|
if (grupo == null || grupo.Id == 0) return false;
|
||||||
|
|
||||||
var cant = Context.Usuarios
|
con.Clientes.Add(cli);
|
||||||
.GroupBy(u => u.id)
|
Guardar(con);
|
||||||
.Select(x => x.Count())
|
|
||||||
.ToList();
|
|
||||||
if (cant.Count < 1) return false;
|
|
||||||
|
|
||||||
usu.id = cant.Count() + 1;
|
con = Context;
|
||||||
cli.idusuario = cant.Count() + 1;
|
cli = con.Clientes.Find(cli.Dni) ?? new();
|
||||||
|
if (cli.Dni == 0) return false;
|
||||||
|
|
||||||
var grupo = Context.Grupos.Find(2);
|
cli.Idgrupos.Add(grupo);
|
||||||
if (grupo == null || grupo.id == 0) return false;
|
return Guardar(con);
|
||||||
|
|
||||||
Context.Usuarios.Add(usu);
|
|
||||||
Guardar();
|
|
||||||
|
|
||||||
var usut = Context.Usuarios.Find(usu.id);
|
|
||||||
usut.idgrupos.Add(grupo);
|
|
||||||
Guardar();
|
|
||||||
|
|
||||||
Context.Clientes.Add(cli);
|
|
||||||
return Guardar();
|
|
||||||
}
|
}
|
||||||
public bool CheckUsuario(LoginDto logindto) {
|
public bool CheckUsuario(LoginDto logindto) {
|
||||||
|
|
||||||
string Contraseña = HacerHash(logindto.Contraseña);
|
string Contraseña = HacerHash(logindto.Contraseña);
|
||||||
|
|
||||||
Usuario? usu = Context.Usuarios.FirstOrDefault(a => a.email == logindto.Email);
|
Cliente? usu = Context.Clientes.FirstOrDefault(a => a.Email == logindto.Email);
|
||||||
if (usu == null) return false;
|
if (usu == null) return false;
|
||||||
|
|
||||||
string hashdb = Encoding.UTF8.GetString(usu.contraseña);
|
string hashdb = Encoding.UTF8.GetString(usu.Contraseña);
|
||||||
|
|
||||||
if (hashdb == Contraseña) return true;
|
if (hashdb == Contraseña) return true;
|
||||||
return false;
|
return false;
|
||||||
@@ -62,20 +53,20 @@ public class RepositorioUsuarios: RepositorioBase<RepositorioUsuarios>
|
|||||||
return BitConverter.ToString(buf).Replace("-","");
|
return BitConverter.ToString(buf).Replace("-","");
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool CheckToken(TokenDto token){
|
public bool CheckToken(string email, string token){
|
||||||
var usu = Context.Usuarios.FirstOrDefault(x => x.email == token.Email);
|
var usu = Context.Clientes.FirstOrDefault(x => x.Email == email);
|
||||||
if (usu == null) return false;
|
if (usu == null) return false;
|
||||||
|
|
||||||
return usu.token == token.Token;
|
return usu.Token == token;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void GuardarToken(LoginDto login, string tokenString)
|
public void GuardarToken(LoginDto login, string tokenString)
|
||||||
{
|
{
|
||||||
var usu = Context.Usuarios.FirstOrDefault(x => x.email == login.Email);
|
var con = Context;
|
||||||
|
var usu = con.Clientes.FirstOrDefault(x => x.Email == login.Email);
|
||||||
if (usu == null) return;
|
if (usu == null) return;
|
||||||
usu.token = tokenString;
|
usu.Token = tokenString;
|
||||||
Guardar();
|
Guardar(con);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user