230 lines
5.8 KiB
JavaScript
230 lines
5.8 KiB
JavaScript
import { unauthRedirectToLogin } from '../router';
|
|
|
|
const API_BASE_URL = '/api';
|
|
|
|
export async function register(username, password, { hCaptchaResponse, recaptchaResponse, turnstileResponse }) {
|
|
const formData = new FormData();
|
|
const payload = { username, password };
|
|
|
|
formData.append('payload', JSON.stringify(payload));
|
|
if (hCaptchaResponse) {
|
|
formData.append('h-captcha-response', hCaptchaResponse);
|
|
}
|
|
if (recaptchaResponse) {
|
|
formData.append('g-recaptcha-response', recaptchaResponse);
|
|
}
|
|
if (turnstileResponse) {
|
|
formData.append('cf-turnstile-response', turnstileResponse);
|
|
}
|
|
|
|
const response = await fetch(API_BASE_URL + "/register", {
|
|
method: 'POST',
|
|
body: formData,
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json();
|
|
throw new Error(error.error || 'Registration failed');
|
|
}
|
|
|
|
return response.json();
|
|
}
|
|
|
|
export async function login(username, password, { hCaptchaResponse, recaptchaResponse, turnstileResponse }) {
|
|
const formData = new FormData();
|
|
const payload = { username, password };
|
|
|
|
formData.append('payload', JSON.stringify(payload));
|
|
if (hCaptchaResponse) {
|
|
formData.append('h-captcha-response', hCaptchaResponse);
|
|
}
|
|
if (recaptchaResponse) {
|
|
formData.append('g-recaptcha-response', recaptchaResponse);
|
|
}
|
|
if (turnstileResponse) {
|
|
formData.append('cf-turnstile-response', turnstileResponse);
|
|
}
|
|
|
|
const response = await fetch(API_BASE_URL + "/login", {
|
|
method: 'POST',
|
|
body: formData
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json();
|
|
throw new Error(error.error || 'Login failed');
|
|
}
|
|
|
|
return response.json();
|
|
}
|
|
|
|
export async function postMessage(message, jwt, { hCaptchaResponse, recaptchaResponse, turnstileResponse }) {
|
|
const formData = new FormData();
|
|
const payload = { message };
|
|
|
|
formData.append('payload', JSON.stringify(payload));
|
|
if (hCaptchaResponse) {
|
|
formData.append('h-captcha-response', hCaptchaResponse);
|
|
}
|
|
if (recaptchaResponse) {
|
|
formData.append('g-recaptcha-response', recaptchaResponse);
|
|
}
|
|
if (turnstileResponse) {
|
|
formData.append('cf-turnstile-response', turnstileResponse);
|
|
}
|
|
|
|
const response = await fetch(API_BASE_URL + "/messages", {
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': "Bearer " + jwt
|
|
},
|
|
body: formData
|
|
});
|
|
|
|
if (!response.ok) {
|
|
if (response.status === 401) {
|
|
unauthRedirectToLogin();
|
|
return;
|
|
}
|
|
const error = await response.json();
|
|
throw new Error(error.error || 'Posting message failed');
|
|
}
|
|
|
|
return response.json();
|
|
}
|
|
|
|
export async function deleteMessage(messageId, jwt) {
|
|
const response = await fetch(API_BASE_URL + "/messages", {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': "Bearer " + jwt,
|
|
},
|
|
body: JSON.stringify({ messageId }),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
if (response.status === 401) {
|
|
unauthRedirectToLogin();
|
|
return;
|
|
}
|
|
const error = await response.json();
|
|
throw new Error(error.error || 'Deleting message failed');
|
|
}
|
|
|
|
return response.json();
|
|
}
|
|
|
|
export async function getMessages() {
|
|
const response = await fetch(API_BASE_URL + "/messages", {
|
|
method: 'GET',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
|
|
if (!response.ok) {
|
|
if (response.status === 401) {
|
|
unauthRedirectToLogin();
|
|
return;
|
|
}
|
|
const error = await response.json();
|
|
throw new Error(error.error || 'Getting messages failed');
|
|
}
|
|
|
|
return response.json();
|
|
}
|
|
|
|
export async function getProfile(jwt) {
|
|
const response = await fetch(API_BASE_URL + "/me", {
|
|
method: 'GET',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': "Bearer " + jwt,
|
|
},
|
|
});
|
|
|
|
if (!response.ok) {
|
|
if (response.status === 401) {
|
|
unauthRedirectToLogin();
|
|
return;
|
|
}
|
|
const error = await response.json();
|
|
throw new Error(error.error || 'Getting profile failed');
|
|
}
|
|
|
|
return response.json();
|
|
}
|
|
|
|
export async function uploadAvatar(avatar, jwt, { hCaptchaResponse, recaptchaResponse, turnstileResponse }) {
|
|
const formData = new FormData();
|
|
formData.append('avatar', avatar);
|
|
if (hCaptchaResponse) {
|
|
formData.append('h-captcha-response', hCaptchaResponse);
|
|
}
|
|
if (recaptchaResponse) {
|
|
formData.append('g-recaptcha-response', recaptchaResponse);
|
|
}
|
|
if (turnstileResponse) {
|
|
formData.append('cf-turnstile-response', turnstileResponse);
|
|
}
|
|
|
|
const response = await fetch(API_BASE_URL + "/avatars", {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Authorization': "Bearer " + jwt,
|
|
},
|
|
body: formData,
|
|
});
|
|
|
|
if (!response.ok) {
|
|
if (response.status === 401) {
|
|
unauthRedirectToLogin();
|
|
return;
|
|
}
|
|
const error = await response.json();
|
|
throw new Error(error.error || 'Avatar upload failed');
|
|
}
|
|
|
|
const data = await response.json();
|
|
return data;
|
|
}
|
|
|
|
export async function generateMotto(jwt, { hCaptchaResponse, recaptchaResponse, turnstileResponse }) {
|
|
const formData = new FormData();
|
|
if (hCaptchaResponse) {
|
|
formData.append('h-captcha-response', hCaptchaResponse);
|
|
}
|
|
if (recaptchaResponse) {
|
|
formData.append('g-recaptcha-response', recaptchaResponse);
|
|
}
|
|
if (turnstileResponse) {
|
|
formData.append('cf-turnstile-response', turnstileResponse);
|
|
}
|
|
|
|
try {
|
|
const response = await fetch('/api/motto', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': `Bearer ${jwt}`,
|
|
},
|
|
body: formData
|
|
});
|
|
|
|
if (!response.ok) {
|
|
if (response.status === 401) {
|
|
unauthRedirectToLogin();
|
|
return;
|
|
}
|
|
const error = await response.json();
|
|
throw new Error(error.error || 'Generating motto failed');
|
|
}
|
|
|
|
const data = await response.json();
|
|
return data.motto;
|
|
} catch {
|
|
console.error('Error generating motto:', error);
|
|
throw new Error('Failed to generate motto');
|
|
}
|
|
}
|