2025-01-13 21:36:52 +01:00
|
|
|
/* Copyright (C) 2024-2025 LibreWeddingPlanner contributors*/
|
2024-12-01 15:33:24 +00:00
|
|
|
|
2024-12-07 13:32:08 +01:00
|
|
|
import { asArray, getCsrfToken, getSlug } from '@/app/lib/utils';
|
|
|
|
import { Captcha, StructuredErrors, User } from '@/app/lib/definitions';
|
2024-12-01 16:31:56 +01:00
|
|
|
|
2024-12-01 17:47:42 +01:00
|
|
|
export function login({ email, password, onLogin }: { email: string, password: string, onLogin: (user: User) => void }) {
|
2024-12-01 18:02:25 +01:00
|
|
|
return fetch(`/api/${getSlug()}/users/sign_in`, {
|
2024-12-01 16:31:56 +01:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({ user: { email, password } }),
|
|
|
|
headers: {
|
2024-12-01 17:47:42 +01:00
|
|
|
'Accept': 'application/json',
|
2024-12-01 16:31:56 +01:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-CSRF-TOKEN': getCsrfToken(),
|
|
|
|
}
|
|
|
|
})
|
2024-12-01 17:47:42 +01:00
|
|
|
.then((response) => response.json())
|
|
|
|
.then((data: any) => {
|
|
|
|
console.log(data);
|
|
|
|
onLogin({
|
|
|
|
id: data.id || '',
|
|
|
|
email: data.email || '',
|
|
|
|
});
|
|
|
|
})
|
2024-12-01 16:31:56 +01:00
|
|
|
.catch((error) => console.error(error));
|
2024-12-01 17:29:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function logout({ onLogout }: { onLogout: () => void }) {
|
2024-12-01 18:02:25 +01:00
|
|
|
fetch(`/api/${getSlug()}/users/sign_out`, {
|
2024-12-01 17:29:57 +01:00
|
|
|
method: 'DELETE',
|
|
|
|
headers: {
|
|
|
|
'X-CSRF-TOKEN': getCsrfToken(),
|
|
|
|
}
|
|
|
|
}).then(onLogout)
|
|
|
|
.catch((error) => console.error(error));
|
|
|
|
}
|
2024-12-07 13:06:14 +01:00
|
|
|
|
2024-12-07 13:32:08 +01:00
|
|
|
function flattenErrors(errors: StructuredErrors): string[] {
|
2024-12-08 09:31:31 +01:00
|
|
|
if (errors instanceof Array) {
|
2024-12-07 13:32:08 +01:00
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
return Object.keys(errors).map((key) => {
|
|
|
|
return `${key}: ${asArray(errors[key]).join(', ')}`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-12-08 09:31:31 +01:00
|
|
|
// At this moment we're making an initial request to get a valid CSRF token
|
|
|
|
export function retrieveCSRFToken() {
|
|
|
|
return fetch(`/api/token`, {
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
}
|
|
|
|
}).then((response) => { return null });
|
|
|
|
}
|
|
|
|
|
|
|
|
export function register({ slug, email, password, passwordConfirmation, captcha, onRegister, onError }: {
|
2024-12-07 13:06:14 +01:00
|
|
|
slug: string,
|
|
|
|
email: string,
|
|
|
|
password: string,
|
|
|
|
passwordConfirmation: string,
|
|
|
|
captcha: Captcha,
|
2024-12-07 13:32:08 +01:00
|
|
|
onRegister: () => void,
|
|
|
|
onError: (errors: string[]) => void
|
2024-12-08 09:31:31 +01:00
|
|
|
}) {
|
2024-12-07 13:06:14 +01:00
|
|
|
fetch(`/api/${slug}/users`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(
|
2024-12-08 09:31:31 +01:00
|
|
|
{
|
2024-12-07 13:06:14 +01:00
|
|
|
user: { email, password, password_confirmation: passwordConfirmation },
|
|
|
|
captcha: { id: captcha.id, answer: captcha.answer }
|
|
|
|
}
|
|
|
|
),
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-CSRF-TOKEN': getCsrfToken(),
|
|
|
|
}
|
2024-12-07 13:32:08 +01:00
|
|
|
}).then((response) => {
|
2024-12-08 09:31:31 +01:00
|
|
|
if (response.ok) {
|
2024-12-07 13:32:08 +01:00
|
|
|
response.json().then(onRegister);
|
|
|
|
} else {
|
|
|
|
response.json().then((data: any) => {
|
|
|
|
onError(data.errors && flattenErrors(data.errors) || [data.error]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
2024-12-07 13:06:14 +01:00
|
|
|
}
|