forked from React-Group/interstellar_ai
commented database
This commit is contained in:
parent
505255d72b
commit
9e762191c7
1 changed files with 29 additions and 44 deletions
|
@ -1,55 +1,40 @@
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
|
||||||
/*
|
// Construct the base API URL based on the environment
|
||||||
This is the guide on how to user this function:
|
const apiURL = new URL("http://localhost:5000/interstellar_ai/db");
|
||||||
|
|
||||||
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.
|
|
||||||
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.
|
|
||||||
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.
|
|
||||||
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.
|
|
||||||
|
|
||||||
|
|
||||||
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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const apiURL = new URL("http://localhost:5000/interstellar_ai/db")
|
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
apiURL.hostname = window.location.hostname;
|
apiURL.hostname = window.location.hostname; // Set hostname for browsers
|
||||||
} else {
|
} else {
|
||||||
apiURL.hostname = "localhost"
|
apiURL.hostname = "localhost"; // Default to localhost for non-browser environments
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to send data to the database and return a success status
|
||||||
export const sendToDatabase = async (data: object): Promise<boolean> => {
|
export const sendToDatabase = async (data: object): Promise<boolean> => {
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(apiURL.href, data);
|
const response = await axios.post(apiURL.href, data);
|
||||||
const status = response.data.status;
|
const status = response.data.status;
|
||||||
const success = response.data.response;
|
const success = response.data.response;
|
||||||
postMessage({ status, success });
|
postMessage({ status, success }); // Send status back to the main thread
|
||||||
return success;
|
return success; // Return success status
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
postMessage({ status: 500, success: false });
|
postMessage({ status: 500, success: false }); // Handle errors
|
||||||
console.log(error)
|
console.log(error);
|
||||||
return false;
|
return false; // Return false on error
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Function to send data and get a string response
|
||||||
export const sendToDatabaseAndGetString = async (data: object): Promise<string> => {
|
export const sendToDatabaseAndGetString = async (data: object): Promise<string> => {
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(apiURL.href, data);
|
const response = await axios.post(apiURL.href, data);
|
||||||
const status = response.data.status;
|
const status = response.data.status;
|
||||||
const success = response.data.response;
|
const success = response.data.response;
|
||||||
postMessage({ status, success });
|
postMessage({ status, success });
|
||||||
return success;
|
return success; // Return response string
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
postMessage({ status: 500, success: false });
|
postMessage({ status: 500, success: false });
|
||||||
console.log(error)
|
console.log(error);
|
||||||
return "false";
|
return "false"; // Return "false" on error
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -61,7 +46,7 @@ export const createAccount = async (username: string, email: string, password: s
|
||||||
email: email,
|
email: email,
|
||||||
password: password,
|
password: password,
|
||||||
};
|
};
|
||||||
return await sendToDatabase(data);
|
return await sendToDatabase(data); // Send account creation request
|
||||||
};
|
};
|
||||||
|
|
||||||
export const changePassword = async (usernameOrEmail: string, password: string, newPassword: string) => {
|
export const changePassword = async (usernameOrEmail: string, password: string, newPassword: string) => {
|
||||||
|
@ -72,7 +57,7 @@ export const changePassword = async (usernameOrEmail: string, password: string,
|
||||||
password,
|
password,
|
||||||
new_password: newPassword,
|
new_password: newPassword,
|
||||||
};
|
};
|
||||||
return await sendToDatabase(data);
|
return await sendToDatabase(data); // Send password change request
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getSettings = async (usernameOrEmail: string, password: string) => {
|
export const getSettings = async (usernameOrEmail: string, password: string) => {
|
||||||
|
@ -82,7 +67,7 @@ export const getSettings = async (usernameOrEmail: string, password: string) =>
|
||||||
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
||||||
password,
|
password,
|
||||||
};
|
};
|
||||||
return await sendToDatabaseAndGetString(data);
|
return await sendToDatabaseAndGetString(data); // Get user settings
|
||||||
};
|
};
|
||||||
|
|
||||||
export const changeSettings = async (usernameOrEmail: string, password: string, newData: object) => {
|
export const changeSettings = async (usernameOrEmail: string, password: string, newData: object) => {
|
||||||
|
@ -93,7 +78,7 @@ export const changeSettings = async (usernameOrEmail: string, password: string,
|
||||||
password,
|
password,
|
||||||
data: newData,
|
data: newData,
|
||||||
};
|
};
|
||||||
return await sendToDatabase(data);
|
return await sendToDatabase(data); // Send settings change request
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getHistory = async (usernameOrEmail: string, password: string) => {
|
export const getHistory = async (usernameOrEmail: string, password: string) => {
|
||||||
|
@ -103,7 +88,7 @@ export const getHistory = async (usernameOrEmail: string, password: string) => {
|
||||||
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
||||||
password,
|
password,
|
||||||
};
|
};
|
||||||
return await sendToDatabaseAndGetString(data);
|
return await sendToDatabaseAndGetString(data); // Get user history
|
||||||
};
|
};
|
||||||
|
|
||||||
export const changeHistory = async (usernameOrEmail: string, password: string, newData: object) => {
|
export const changeHistory = async (usernameOrEmail: string, password: string, newData: object) => {
|
||||||
|
@ -114,7 +99,7 @@ export const changeHistory = async (usernameOrEmail: string, password: string, n
|
||||||
password,
|
password,
|
||||||
data: newData,
|
data: newData,
|
||||||
};
|
};
|
||||||
return await sendToDatabase(data);
|
return await sendToDatabase(data); // Send history change request
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getEmail = async (usernameOrEmail: string, password: string): Promise<string> => {
|
export const getEmail = async (usernameOrEmail: string, password: string): Promise<string> => {
|
||||||
|
@ -124,7 +109,7 @@ export const getEmail = async (usernameOrEmail: string, password: string): Promi
|
||||||
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
||||||
password,
|
password,
|
||||||
};
|
};
|
||||||
return await sendToDatabaseAndGetString(data);
|
return await sendToDatabaseAndGetString(data); // Get user email
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getName = async (usernameOrEmail: string, password: string): Promise<string> => {
|
export const getName = async (usernameOrEmail: string, password: string): Promise<string> => {
|
||||||
|
@ -134,10 +119,9 @@ export const getName = async (usernameOrEmail: string, password: string): Promis
|
||||||
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
||||||
password,
|
password,
|
||||||
};
|
};
|
||||||
return await sendToDatabaseAndGetString(data);
|
return await sendToDatabaseAndGetString(data); // Get user name
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
export const checkCredentials = async (usernameOrEmail: string, password: string) => {
|
export const checkCredentials = async (usernameOrEmail: string, password: string) => {
|
||||||
const data = {
|
const data = {
|
||||||
action: "check_credentials",
|
action: "check_credentials",
|
||||||
|
@ -145,15 +129,16 @@ export const checkCredentials = async (usernameOrEmail: string, password: string
|
||||||
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
||||||
password,
|
password,
|
||||||
};
|
};
|
||||||
const sendBack = await sendToDatabase(data);
|
const sendBack = await sendToDatabase(data); // Check user credentials
|
||||||
if (sendBack) {
|
if (sendBack) {
|
||||||
if (typeof localStorage !== 'undefined') {
|
if (typeof localStorage !== 'undefined') {
|
||||||
localStorage.setItem("accountEmail", await getEmail(usernameOrEmail, password))
|
// Store user data in localStorage if credentials are valid
|
||||||
localStorage.setItem("accountName", await getName(usernameOrEmail, password))
|
localStorage.setItem("accountEmail", await getEmail(usernameOrEmail, password));
|
||||||
localStorage.setItem("accountPassword", password)
|
localStorage.setItem("accountName", await getName(usernameOrEmail, password));
|
||||||
|
localStorage.setItem("accountPassword", password);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sendBack
|
return sendBack; // Return success status
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteAccount = async (usernameOrEmail: string, password: string) => {
|
export const deleteAccount = async (usernameOrEmail: string, password: string) => {
|
||||||
|
@ -163,5 +148,5 @@ export const deleteAccount = async (usernameOrEmail: string, password: string) =
|
||||||
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
||||||
password,
|
password,
|
||||||
};
|
};
|
||||||
return await sendToDatabase(data);
|
return await sendToDatabase(data); // Send account deletion request
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue