feat: hcaptcha
This commit is contained in:
+45
-45
@@ -2,14 +2,18 @@ import { unauthRedirectToLogin } from '../router';
|
||||
|
||||
const API_BASE_URL = '/api';
|
||||
|
||||
export async function register(username, password, hCaptchaResponse) {
|
||||
const formData = new FormData();
|
||||
const payload = { username, password };
|
||||
|
||||
formData.append('payload', JSON.stringify(payload));
|
||||
if (hCaptchaResponse) {
|
||||
formData.append('h-captcha-response', hCaptchaResponse);
|
||||
}
|
||||
|
||||
export async function register(username, password) {
|
||||
const response = await fetch(API_BASE_URL + "/register", {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ username, password }),
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -20,13 +24,18 @@ export async function register(username, password) {
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function login(username, password) {
|
||||
export async function login(username, password, hCaptchaResponse) {
|
||||
const formData = new FormData();
|
||||
const payload = { username, password };
|
||||
|
||||
formData.append('payload', JSON.stringify(payload));
|
||||
if (hCaptchaResponse) {
|
||||
formData.append('h-captcha-response', hCaptchaResponse);
|
||||
}
|
||||
|
||||
const response = await fetch(API_BASE_URL + "/login", {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ username, password }),
|
||||
body: formData
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -37,14 +46,21 @@ export async function login(username, password) {
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function postMessage(message, jwt) {
|
||||
export async function postMessage(message, jwt, hCaptchaResponse) {
|
||||
const formData = new FormData();
|
||||
const payload = { message };
|
||||
|
||||
formData.append('payload', JSON.stringify(payload));
|
||||
if (hCaptchaResponse) {
|
||||
formData.append('h-captcha-response', hCaptchaResponse);
|
||||
}
|
||||
|
||||
const response = await fetch(API_BASE_URL + "/messages", {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': "Bearer " + jwt,
|
||||
'Authorization': "Bearer " + jwt
|
||||
},
|
||||
body: JSON.stringify({ message }),
|
||||
body: formData
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -81,7 +97,7 @@ export async function deleteMessage(messageId, jwt) {
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function getMessages(jwt) {
|
||||
export async function getMessages() {
|
||||
const response = await fetch(API_BASE_URL + "/messages", {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
@@ -122,43 +138,20 @@ export async function getProfile(jwt) {
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function me(jwt) {
|
||||
const response = await fetch(API_BASE_URL + "/me", {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': "Bearer " + jwt,
|
||||
},
|
||||
});
|
||||
export async function uploadAvatar(avatar, jwt, hCaptchaResponse) {
|
||||
const formData = new FormData();
|
||||
formData.append('avatar', avatar);
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) {
|
||||
unauthRedirectToLogin();
|
||||
return;
|
||||
}
|
||||
const error = await response.json();
|
||||
throw new Error(error.error || 'User operation failed');
|
||||
if (hCaptchaResponse) {
|
||||
formData.append('h-captcha-response', hCaptchaResponse);
|
||||
}
|
||||
|
||||
const userData = await response.json();
|
||||
const avatarFilename = userData.avatar;
|
||||
|
||||
if (avatarFilename) {
|
||||
return import.meta.env.VITE_R2_BASE_URL + "/" + avatarFilename;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function uploadAvatar(avatar, jwt) {
|
||||
const body = new FormData();
|
||||
body.append('avatar', avatar);
|
||||
|
||||
const response = await fetch(API_BASE_URL + "/avatars", {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Authorization': "Bearer " + jwt,
|
||||
},
|
||||
body: body,
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -174,12 +167,19 @@ export async function uploadAvatar(avatar, jwt) {
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function generateMotto(jwt) {
|
||||
export async function generateMotto(jwt, hCaptchaResponse) {
|
||||
const formData = new FormData();
|
||||
if (hCaptchaResponse) {
|
||||
formData.append('h-captcha-response', hCaptchaResponse);
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/motto', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${jwt}`,
|
||||
},
|
||||
body: formData
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
|
||||
Reference in New Issue
Block a user