wedding-planner-frontend/app/api/authentication.tsx

38 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-12-01 15:33:24 +00:00
/* Copyright (C) 2024 Manuel Bustillo*/
import { getCsrfToken } from '@/app/lib/utils';
2024-12-01 17:29:57 +01:00
import { User } from '@/app/lib/definitions';
export function login({ email, password, onLogin }: { email: string, password: string, onLogin: () => void }) {
console.log(email, password);
return fetch("/api/default/users/sign_in", {
method: 'POST',
body: JSON.stringify({ user: { email, password } }),
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': getCsrfToken(),
}
})
.then(onLogin)
.catch((error) => console.error(error));
2024-12-01 17:29:57 +01:00
}
export function logout({ onLogout }: { onLogout: () => void }) {
fetch("/api/default/users/sign_out", {
method: 'DELETE',
headers: {
'X-CSRF-TOKEN': getCsrfToken(),
}
}).then(onLogout)
.catch((error) => console.error(error));
}
export function getCurrentUser({ onLoad }: { onLoad: (user: User) => void }) {
fetch("/api/default/users/current")
.then((response) => response.json())
.then((data) => {
onLoad(data);
}, (error) => {
return null;
});
}