2024-09-25 09:10:14 +02:00
|
|
|
import axios from "axios";
|
|
|
|
|
2024-10-11 09:15:12 +02:00
|
|
|
// Construct the base API URL based on the environment
|
|
|
|
const apiURL = new URL("http://localhost:5000/interstellar_ai/db");
|
2024-10-07 09:17:31 +02:00
|
|
|
if (typeof window !== 'undefined') {
|
2024-10-11 09:15:12 +02:00
|
|
|
apiURL.hostname = window.location.hostname; // Set hostname for browsers
|
2024-10-07 09:17:31 +02:00
|
|
|
} else {
|
2024-10-11 09:15:12 +02:00
|
|
|
apiURL.hostname = "localhost"; // Default to localhost for non-browser environments
|
2024-10-07 09:17:31 +02:00
|
|
|
}
|
2024-10-03 14:10:21 +02:00
|
|
|
|
2024-10-11 09:15:12 +02:00
|
|
|
// Function to send data to the database and return a success status
|
2024-10-07 08:57:34 +02:00
|
|
|
export const sendToDatabase = async (data: object): Promise<boolean> => {
|
2024-09-30 15:26:31 +02:00
|
|
|
try {
|
2024-10-03 14:10:21 +02:00
|
|
|
const response = await axios.post(apiURL.href, data);
|
2024-09-30 15:26:31 +02:00
|
|
|
const status = response.data.status;
|
|
|
|
const success = response.data.response;
|
2024-10-11 09:15:12 +02:00
|
|
|
postMessage({ status, success }); // Send status back to the main thread
|
|
|
|
return success; // Return success status
|
2024-09-30 15:26:31 +02:00
|
|
|
} catch (error) {
|
2024-10-11 09:15:12 +02:00
|
|
|
postMessage({ status: 500, success: false }); // Handle errors
|
|
|
|
console.log(error);
|
|
|
|
return false; // Return false on error
|
2024-09-30 15:26:31 +02:00
|
|
|
}
|
2024-09-30 10:47:38 +02:00
|
|
|
};
|
|
|
|
|
2024-10-11 09:15:12 +02:00
|
|
|
// Function to send data and get a string response
|
2024-10-07 08:57:34 +02:00
|
|
|
export const sendToDatabaseAndGetString = async (data: object): Promise<string> => {
|
2024-10-01 15:34:43 +02:00
|
|
|
try {
|
2024-10-03 14:10:21 +02:00
|
|
|
const response = await axios.post(apiURL.href, data);
|
2024-10-01 15:34:43 +02:00
|
|
|
const status = response.data.status;
|
|
|
|
const success = response.data.response;
|
|
|
|
postMessage({ status, success });
|
2024-10-11 09:15:12 +02:00
|
|
|
return success; // Return response string
|
2024-10-01 15:34:43 +02:00
|
|
|
} catch (error) {
|
|
|
|
postMessage({ status: 500, success: false });
|
2024-10-11 09:15:12 +02:00
|
|
|
console.log(error);
|
|
|
|
return "false"; // Return "false" on error
|
2024-10-01 15:34:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-30 10:47:38 +02:00
|
|
|
// Functions for each action
|
|
|
|
export const createAccount = async (username: string, email: string, password: string) => {
|
|
|
|
const data = {
|
|
|
|
action: "create_account",
|
2024-09-30 15:26:31 +02:00
|
|
|
username: username,
|
|
|
|
email: email,
|
|
|
|
password: password,
|
2024-09-30 10:47:38 +02:00
|
|
|
};
|
2024-10-11 09:15:12 +02:00
|
|
|
return await sendToDatabase(data); // Send account creation request
|
2024-09-30 10:47:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const changePassword = async (usernameOrEmail: string, password: string, newPassword: string) => {
|
|
|
|
const data = {
|
|
|
|
action: "change_password",
|
|
|
|
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
|
|
|
|
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
|
|
|
password,
|
|
|
|
new_password: newPassword,
|
|
|
|
};
|
2024-10-11 09:15:12 +02:00
|
|
|
return await sendToDatabase(data); // Send password change request
|
2024-09-30 10:47:38 +02:00
|
|
|
};
|
|
|
|
|
2024-10-07 16:41:31 +02:00
|
|
|
export const getSettings = async (usernameOrEmail: string, password: string) => {
|
2024-09-30 10:47:38 +02:00
|
|
|
const data = {
|
2024-10-02 14:14:20 +02:00
|
|
|
action: "get_settings",
|
2024-09-30 10:47:38 +02:00
|
|
|
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
|
|
|
|
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
|
|
|
password,
|
|
|
|
};
|
2024-10-11 09:15:12 +02:00
|
|
|
return await sendToDatabaseAndGetString(data); // Get user settings
|
2024-10-01 15:34:43 +02:00
|
|
|
};
|
|
|
|
|
2024-10-07 16:41:31 +02:00
|
|
|
export const changeSettings = async (usernameOrEmail: string, password: string, newData: object) => {
|
2024-10-01 15:34:43 +02:00
|
|
|
const data = {
|
2024-10-07 16:41:31 +02:00
|
|
|
action: "change_settings",
|
2024-10-01 15:34:43 +02:00
|
|
|
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
|
|
|
|
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
|
|
|
password,
|
2024-10-07 16:41:31 +02:00
|
|
|
data: newData,
|
2024-10-01 15:34:43 +02:00
|
|
|
};
|
2024-10-11 09:15:12 +02:00
|
|
|
return await sendToDatabase(data); // Send settings change request
|
2024-10-01 15:34:43 +02:00
|
|
|
};
|
|
|
|
|
2024-10-07 16:41:31 +02:00
|
|
|
export const getHistory = async (usernameOrEmail: string, password: string) => {
|
2024-10-01 15:34:43 +02:00
|
|
|
const data = {
|
2024-10-07 16:41:31 +02:00
|
|
|
action: "get_history",
|
2024-10-01 15:34:43 +02:00
|
|
|
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
|
|
|
|
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
|
|
|
password,
|
|
|
|
};
|
2024-10-11 09:15:12 +02:00
|
|
|
return await sendToDatabaseAndGetString(data); // Get user history
|
2024-09-30 10:47:38 +02:00
|
|
|
};
|
|
|
|
|
2024-10-07 16:41:31 +02:00
|
|
|
export const changeHistory = async (usernameOrEmail: string, password: string, newData: object) => {
|
2024-09-30 10:47:38 +02:00
|
|
|
const data = {
|
2024-10-07 16:41:31 +02:00
|
|
|
action: "change_history",
|
2024-09-30 10:47:38 +02:00
|
|
|
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
|
|
|
|
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
|
|
|
password,
|
|
|
|
data: newData,
|
|
|
|
};
|
2024-10-11 09:15:12 +02:00
|
|
|
return await sendToDatabase(data); // Send history change request
|
2024-09-30 10:47:38 +02:00
|
|
|
};
|
|
|
|
|
2024-10-07 16:41:31 +02:00
|
|
|
export const getEmail = async (usernameOrEmail: string, password: string): Promise<string> => {
|
|
|
|
const data = {
|
|
|
|
action: "get_email",
|
|
|
|
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
|
|
|
|
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
|
|
|
password,
|
|
|
|
};
|
2024-10-11 09:15:12 +02:00
|
|
|
return await sendToDatabaseAndGetString(data); // Get user email
|
2024-10-07 16:41:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getName = async (usernameOrEmail: string, password: string): Promise<string> => {
|
|
|
|
const data = {
|
|
|
|
action: "get_name",
|
|
|
|
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
|
|
|
|
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
|
|
|
password,
|
|
|
|
};
|
2024-10-11 09:15:12 +02:00
|
|
|
return await sendToDatabaseAndGetString(data); // Get user name
|
2024-10-07 16:41:31 +02:00
|
|
|
};
|
|
|
|
|
2024-09-30 10:47:38 +02:00
|
|
|
export const checkCredentials = async (usernameOrEmail: string, password: string) => {
|
|
|
|
const data = {
|
|
|
|
action: "check_credentials",
|
|
|
|
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
|
|
|
|
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
|
|
|
password,
|
|
|
|
};
|
2024-10-11 09:15:12 +02:00
|
|
|
const sendBack = await sendToDatabase(data); // Check user credentials
|
2024-10-01 15:34:43 +02:00
|
|
|
if (sendBack) {
|
2024-10-07 11:16:51 +02:00
|
|
|
if (typeof localStorage !== 'undefined') {
|
2024-10-11 09:15:12 +02:00
|
|
|
// Store user data in localStorage if credentials are valid
|
|
|
|
localStorage.setItem("accountEmail", await getEmail(usernameOrEmail, password));
|
|
|
|
localStorage.setItem("accountName", await getName(usernameOrEmail, password));
|
|
|
|
localStorage.setItem("accountPassword", password);
|
2024-10-07 11:16:51 +02:00
|
|
|
}
|
2024-10-01 15:34:43 +02:00
|
|
|
}
|
2024-10-11 09:15:12 +02:00
|
|
|
return sendBack; // Return success status
|
2024-09-30 10:47:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const deleteAccount = async (usernameOrEmail: string, password: string) => {
|
|
|
|
const data = {
|
|
|
|
action: "delete_account",
|
|
|
|
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
|
|
|
|
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
|
|
|
password,
|
|
|
|
};
|
2024-10-11 09:15:12 +02:00
|
|
|
return await sendToDatabase(data); // Send account deletion request
|
2024-09-30 10:47:38 +02:00
|
|
|
};
|