131 lines
4.6 KiB
Plaintext
131 lines
4.6 KiB
Plaintext
@page
|
|
@using System.Security.Claims;
|
|
@using Jerguero.Model;
|
|
@using Microsoft.AspNetCore.Authentication;
|
|
@using Microsoft.AspNetCore.Authentication.Cookies;
|
|
@using Microsoft.AspNetCore.Mvc;
|
|
@using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
@model LoginModel
|
|
|
|
<h1 class="text-3xl font-bold text-center my-4 fixed top-0 w-screen">
|
|
Jerguero
|
|
</h1>
|
|
<div class="flex items-center h-screen w-full">
|
|
<div class="container mx-auto" hx-ext="response-targets">
|
|
<p class="my-4 p-2 rounded bg-c-bg-2 text-c-on-bg shadow shadow-zinc-500">
|
|
¡Iniciá sesión para comenzar a crear definiciones!
|
|
</p>
|
|
<form class="my-4 py-4 px-2 rounded bg-c-bg-2 text-c-on-bg shadow shadow-zinc-500" method="post">
|
|
<div asp-validation-summary="ModelOnly" class="text-red-400"></div>
|
|
<input type="hidden" value="@Model.RedirectUrl">
|
|
|
|
<div class="py-2">
|
|
<label class="text-sm opacity-85" asp-for="LoginPerson.Email">Correo electronico:</label>
|
|
<br />
|
|
<input asp-for="LoginPerson.Email" class="inline-block w-full rounded bg-c-bg text-c-on-bg py-2 px-1
|
|
disabled:cursor-not-allowed disabled:opacity-50 transition-opacity" type="email"
|
|
required />
|
|
<span asp-validation-for="LoginPerson.Email" class="text-red-400"></span>
|
|
</div>
|
|
|
|
<div class="py-2">
|
|
<label asp-for="LoginPerson.Password" class="text-sm opacity-85"
|
|
for="login-password">Contraseña:</label>
|
|
<br />
|
|
<input asp-for="LoginPerson.Password" class="inline-block w-full rounded bg-c-bg text-c-on-bg py-2 px-1
|
|
disabled:cursor-not-allowed disabled:opacity-50 transition-opacity" type="password"
|
|
pattern=".{8,}" required />
|
|
<span asp-validation-for="LoginPerson.Password" class="text-red-400"></span>
|
|
</div>
|
|
|
|
<div class="text-center pt-2">
|
|
<button type="submit" class="py-1 px-2 rounded bg-c-primary text-c-on-primary hover:underline
|
|
disabled:cursor-not-allowed disabled:animate-pulse" data-loading-disable>
|
|
Iniciar sesión
|
|
</button>
|
|
</div>
|
|
|
|
<div id="login-result" class="text-center pt-2"></div>
|
|
<div id="login-result-error" class="text-center pt-2 text-red-400"></div>
|
|
</form>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
@section Scripts {
|
|
<partial name="_ValidationScriptsPartial" />
|
|
}
|
|
|
|
@functions {
|
|
public class LoginModel(ILogger<LoginModel> logger) : PageModel
|
|
{
|
|
private readonly ILogger<LoginModel> _logger = logger;
|
|
|
|
[BindProperty]
|
|
public Person LoginPerson { get; set; } = default!;
|
|
|
|
public string RedirectUrl = "/";
|
|
|
|
public IActionResult OnGet(string? redirect)
|
|
{
|
|
RedirectUrl = redirect ?? "/";
|
|
if (User.Identity?.IsAuthenticated == true)
|
|
{
|
|
return LocalRedirect(redirect ?? "/");
|
|
}
|
|
|
|
return Page();
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostAsync(string? redirect)
|
|
{
|
|
_logger.LogInformation($"Got `{redirect}` as redirect param");
|
|
if (!ModelState.IsValid || LoginPerson == null)
|
|
{
|
|
return Page();
|
|
}
|
|
|
|
// login
|
|
var loginCorrect = await AuthenticateUser(LoginPerson.Email, LoginPerson.Password);
|
|
if (!loginCorrect)
|
|
{
|
|
ModelState.AddModelError(string.Empty, "Credenciales inválidos");
|
|
return Page();
|
|
}
|
|
|
|
var claims = new List<Claim>
|
|
{
|
|
new(ClaimTypes.Name, LoginPerson.Email),
|
|
new(ClaimTypes.Role, "User"),
|
|
};
|
|
|
|
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
|
|
|
|
await HttpContext.SignInAsync(
|
|
CookieAuthenticationDefaults.AuthenticationScheme,
|
|
new ClaimsPrincipal(claimsIdentity),
|
|
new AuthenticationProperties { }
|
|
);
|
|
|
|
return LocalRedirect(redirect ?? "/");
|
|
}
|
|
|
|
private async Task<bool> AuthenticateUser(string email, string password)
|
|
{
|
|
_logger.LogInformation("Authenticating: {}", new { Email = email, Password = password });
|
|
|
|
await Task.Delay(1000);
|
|
|
|
if (email == "a@b.c")
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|