2024-09-25 09:10:14 +02:00
|
|
|
import axios from "axios";
|
|
|
|
|
2024-09-30 09:35:58 +02:00
|
|
|
/*
|
|
|
|
This is the guide on how to user this function:
|
|
|
|
|
|
|
|
data should be the json containing everything relevant, the json can contain the following keys:
|
|
|
|
|
|
|
|
action -> contains the action you want to do, there are: create_account, change_password, get_data, change_data, check_credentials, delete_account
|
|
|
|
username -> contains the current username, required for create_account, but can be omitted in favor of email in other requests. Preffered over email authentication.
|
2024-09-30 10:47:38 +02:00
|
|
|
email -> contains the current email, required for create_account, but just like the username, it can be omitted, in favor of the other, sending both is possible too.
|
2024-09-30 09:35:58 +02:00
|
|
|
password -> contains the password, required for all requests.
|
|
|
|
new_password -> in the case you are changing your password, you will need to use this in addition to password, to specify the new password.
|
2024-09-30 10:47:38 +02:00
|
|
|
data -> data contains all the data you want to store, you have to always give the entire data, because the data you give here overwrites the data in the database,
|
|
|
|
so if you only give the chat history for example, all settings will be deleted, and if you only give settings, all chat histories will get deleted.
|
2024-09-30 09:35:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
if all went well, you will get the status 200 in response.data.status
|
|
|
|
to check if the request was accepted or declined, check response.data.response, it will be either true or false depending on if it worked, or not.
|
|
|
|
*/
|
|
|
|
|
2024-09-30 15:26:31 +02:00
|
|
|
export const sendToDatabase = async (data: any): Promise<boolean> => {
|
|
|
|
try {
|
|
|
|
const response = await axios.post("http://localhost:5000/interstellar_ai/db", data);
|
|
|
|
const status = response.data.status;
|
|
|
|
const success = response.data.response;
|
|
|
|
postMessage({ status, success });
|
|
|
|
return success;
|
|
|
|
} catch (error) {
|
|
|
|
postMessage({ status: 500, success: false });
|
|
|
|
return false;
|
|
|
|
}
|
2024-09-30 10:47:38 +02:00
|
|
|
};
|
|
|
|
|
2024-10-01 15:34:43 +02:00
|
|
|
export const sendToDatabaseAndGetString = async (data: any): Promise<string> => {
|
|
|
|
try {
|
|
|
|
const response = await axios.post("http://localhost:5000/interstellar_ai/db", data);
|
|
|
|
const status = response.data.status;
|
|
|
|
const success = response.data.response;
|
|
|
|
postMessage({ status, success });
|
|
|
|
return success;
|
|
|
|
} catch (error) {
|
|
|
|
postMessage({ status: 500, success: false });
|
|
|
|
return "false";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
return await sendToDatabase(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
return await sendToDatabase(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getData = async (usernameOrEmail: string, password: string) => {
|
|
|
|
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-01 15:34:43 +02:00
|
|
|
return await sendToDatabaseAndGetString(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
return await sendToDatabaseAndGetString(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
return await sendToDatabaseAndGetString(data);
|
2024-09-30 10:47:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const changeData = async (usernameOrEmail: string, password: string, newData: any) => {
|
|
|
|
const data = {
|
2024-10-02 14:14:20 +02:00
|
|
|
action: "change_settings",
|
2024-09-30 10:47:38 +02:00
|
|
|
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
|
|
|
|
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
|
|
|
password,
|
|
|
|
data: newData,
|
|
|
|
};
|
|
|
|
return await sendToDatabase(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
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-01 15:34:43 +02:00
|
|
|
var sendBack = await sendToDatabase(data);
|
|
|
|
if (sendBack) {
|
|
|
|
localStorage.setItem("accountEmail", await getEmail(usernameOrEmail, password))
|
|
|
|
localStorage.setItem("accountName", await getName(usernameOrEmail, password))
|
|
|
|
localStorage.setItem("accountPassword", password)
|
|
|
|
}
|
|
|
|
return sendBack
|
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,
|
|
|
|
};
|
|
|
|
return await sendToDatabase(data);
|
|
|
|
};
|