Private
Public Access
37 lines
847 B
TypeScript
37 lines
847 B
TypeScript
import { invalidate } from '$app/navigation';
|
|
import { API_URL } from '$lib/env';
|
|
import { getToken } from './token';
|
|
|
|
export async function cambiarContraseña(dto: {
|
|
user_id: number;
|
|
old_password: string;
|
|
new_password: string;
|
|
}) {
|
|
if (
|
|
!dto.old_password ||
|
|
!dto.new_password ||
|
|
dto.old_password.length < 8 ||
|
|
dto.new_password.length < 8
|
|
)
|
|
return 'Las contraseñas no alcanzan el minimo de 8 caracteres';
|
|
try {
|
|
const req = await fetch(`${API_URL}/auth/cambiar-contraseña`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Authorization: `Bearer ${getToken()}`
|
|
},
|
|
body: JSON.stringify(dto)
|
|
});
|
|
|
|
let data = await req.json();
|
|
if (!req.ok) {
|
|
return data.error || 'Error';
|
|
}
|
|
invalidate('usuarios:load');
|
|
return 'Ok';
|
|
} catch {
|
|
return 'No se pudo conectar con el servidor';
|
|
}
|
|
}
|