Private
Public Access
127 lines
3.1 KiB
Svelte
127 lines
3.1 KiB
Svelte
<script lang="ts">
|
|
import {
|
|
Button,
|
|
InlineLoading,
|
|
InlineNotification,
|
|
PasswordInput
|
|
} from 'carbon-components-svelte';
|
|
import Card from '$lib/components/Card.svelte';
|
|
import Header from '$lib/components/Header.svelte';
|
|
import { API_URL } from '$lib/env';
|
|
import { cambiarPasswordPropia } from '$lib/hooks/tenant';
|
|
import { recargarSesion } from '$lib/hooks/session';
|
|
import { getCurrentUser } from '$lib/hooks/user';
|
|
import { goto } from '$app/navigation';
|
|
|
|
let oldPassword = $state('');
|
|
let newPassword = $state('');
|
|
let confirmPassword = $state('');
|
|
let loading = $state(false);
|
|
let error = $state('');
|
|
let success = $state('');
|
|
|
|
const forzado = $derived(getCurrentUser()?.debe_cambiar_password === true);
|
|
|
|
async function handleSubmit() {
|
|
error = '';
|
|
success = '';
|
|
|
|
if (!oldPassword || !newPassword || !confirmPassword) {
|
|
error = 'Completá todos los campos';
|
|
return;
|
|
}
|
|
if (newPassword !== confirmPassword) {
|
|
error = 'Las contraseñas nuevas no coinciden';
|
|
return;
|
|
}
|
|
if (newPassword.length < 8) {
|
|
error = 'La nueva contraseña debe tener al menos 8 caracteres';
|
|
return;
|
|
}
|
|
|
|
loading = true;
|
|
try {
|
|
await cambiarPasswordPropia(oldPassword, newPassword);
|
|
await recargarSesion(API_URL);
|
|
success = 'Contraseña actualizada correctamente';
|
|
oldPassword = '';
|
|
newPassword = '';
|
|
confirmPassword = '';
|
|
setTimeout(() => {
|
|
void goto('/menu');
|
|
}, 900);
|
|
} catch (e) {
|
|
error = e instanceof Error ? e.message : 'No se pudo cambiar la contraseña';
|
|
} finally {
|
|
loading = false;
|
|
}
|
|
}
|
|
</script>
|
|
|
|
{#if !forzado}
|
|
<Header />
|
|
{/if}
|
|
|
|
<div class="password-container">
|
|
<Card title={forzado ? 'Definí tu contraseña' : 'Cambiar contraseña'}>
|
|
{#if forzado}
|
|
<p class="subtitle">
|
|
Estás usando una contraseña temporal. Para continuar necesitás elegir una nueva.
|
|
</p>
|
|
{:else}
|
|
<p class="subtitle">Ingresá tu contraseña actual y la nueva.</p>
|
|
{/if}
|
|
|
|
{#if error}
|
|
<InlineNotification kind="error" title="Error" subtitle={error} hideCloseButton />
|
|
{/if}
|
|
{#if success}
|
|
<InlineNotification kind="success" title="Listo" subtitle={success} hideCloseButton />
|
|
{/if}
|
|
|
|
<form
|
|
onsubmit={(e) => {
|
|
e.preventDefault();
|
|
handleSubmit();
|
|
}}
|
|
>
|
|
<PasswordInput bind:value={oldPassword} labelText="Contraseña actual" />
|
|
<PasswordInput bind:value={newPassword} labelText="Nueva contraseña" minlength={8} />
|
|
<PasswordInput bind:value={confirmPassword} labelText="Repetir nueva contraseña" />
|
|
<Button type="submit" disabled={loading} class="max-w-full!">
|
|
{#if loading}
|
|
<InlineLoading description="Guardando..." />
|
|
{:else}
|
|
Guardar contraseña
|
|
{/if}
|
|
</Button>
|
|
</form>
|
|
|
|
{#if !forzado}
|
|
<p><a href="/menu">Volver al panel</a></p>
|
|
{/if}
|
|
</Card>
|
|
</div>
|
|
|
|
<style>
|
|
.password-container {
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
min-height: 100vh;
|
|
background: var(--cds-ui-background, #161616);
|
|
padding: 2rem 1rem;
|
|
}
|
|
|
|
.subtitle {
|
|
color: var(--cds-text-secondary, #c6c6c6);
|
|
margin-bottom: 1.5rem;
|
|
}
|
|
|
|
form {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 1rem;
|
|
}
|
|
</style>
|