Merge branch 'main' into main
This commit is contained in:
commit
119832ee39
6 changed files with 567 additions and 525 deletions
|
@ -31,6 +31,19 @@ export const sendToDatabase = async (data: any): Promise<boolean> => {
|
|||
}
|
||||
};
|
||||
|
||||
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";
|
||||
}
|
||||
};
|
||||
|
||||
// Functions for each action
|
||||
export const createAccount = async (username: string, email: string, password: string) => {
|
||||
const data = {
|
||||
|
@ -60,7 +73,27 @@ export const getData = async (usernameOrEmail: string, password: string) => {
|
|||
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
||||
password,
|
||||
};
|
||||
return await sendToDatabase(data);
|
||||
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);
|
||||
};
|
||||
|
||||
export const changeData = async (usernameOrEmail: string, password: string, newData: any) => {
|
||||
|
@ -81,7 +114,13 @@ export const checkCredentials = async (usernameOrEmail: string, password: string
|
|||
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
||||
password,
|
||||
};
|
||||
return await sendToDatabase(data);
|
||||
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
|
||||
};
|
||||
|
||||
export const deleteAccount = async (usernameOrEmail: string, password: string) => {
|
||||
|
|
|
@ -50,25 +50,11 @@ const Login: React.FC = () => {
|
|||
|
||||
// Function to handle login
|
||||
const handleLogin = async () => {
|
||||
const savedAccountEmail = localStorage.getItem('accountEmail');
|
||||
const savedAccountPassword = localStorage.getItem('accountPassword');
|
||||
const savedAccountName = localStorage.getItem('accountName');
|
||||
|
||||
// Check if savedAccountName or savedAccountEmail is not null before passing to checkCredentials
|
||||
var accountIdentifier = savedAccountName || savedAccountEmail;
|
||||
if (!accountIdentifier) {
|
||||
accountIdentifier = accountName
|
||||
}
|
||||
|
||||
if (accountIdentifier && password) {
|
||||
const success = await checkCredentials(accountIdentifier, password);
|
||||
if (accountName && password) {
|
||||
const success = await checkCredentials(accountName, password);
|
||||
if (success) {
|
||||
setIsLoggedIn(true); // Successful login
|
||||
setShowLoginPopup(false); // Close the login popup
|
||||
// Save credentials to localStorage (optional in case of changes)
|
||||
localStorage.setItem('accountName', savedAccountName || accountName);
|
||||
localStorage.setItem('accountEmail', savedAccountEmail || email);
|
||||
localStorage.setItem('accountPassword', savedAccountPassword || password);
|
||||
} else {
|
||||
alert('Incorrect credentials');
|
||||
}
|
||||
|
|
|
@ -37,11 +37,8 @@ const PrivacySettings: React.FC<PrivacySettingsProps> = ({ selectedOption, handl
|
|||
>
|
||||
None{openSourceMode ? ' (FOSS)' : ''}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<br />
|
||||
<p>
|
||||
After changing the preferred settings, please reload the website so it can update itself properly.
|
||||
</p>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -97,6 +97,7 @@ class API:
|
|||
@self.app.route('/interstellar_ai/db', methods=['POST'])
|
||||
def db_manipulate():
|
||||
sent_data = request.get_json()
|
||||
print(sent_data)
|
||||
action = sent_data.get('action')
|
||||
if action == "create_account":
|
||||
return jsonify({'status': 200, 'response': self.db.add_user(sent_data)})
|
||||
|
@ -110,6 +111,10 @@ class API:
|
|||
return jsonify({'status': 200, 'response': self.db.check_credentials(sent_data)})
|
||||
elif action == "delete_account":
|
||||
return jsonify({'status': 200, 'response': self.db.delete_user(sent_data)})
|
||||
elif action == "get_email":
|
||||
return jsonify({'status': 200, 'response': self.db.get_email(sent_data)})
|
||||
elif action == "get_name":
|
||||
return jsonify({'status': 200, 'response': self.db.get_name(sent_data)})
|
||||
|
||||
return jsonify({'status': 401, 'response': "Invalid action"})
|
||||
|
||||
|
|
28
py/db.py
28
py/db.py
|
@ -9,13 +9,9 @@ class DB:
|
|||
self.database = {}
|
||||
|
||||
def ensure_username(self, data):
|
||||
print(data)
|
||||
print(self.database)
|
||||
if 'username' in data:
|
||||
print("usr")
|
||||
return data.get('username')
|
||||
elif 'email' in data:
|
||||
print("email")
|
||||
for index, entry in self.database:
|
||||
if entry.get('email') == data.get('email'):
|
||||
return index
|
||||
|
@ -34,15 +30,12 @@ class DB:
|
|||
user_data = {"hashed_password": hashed_password, "email": email, "data": None}
|
||||
if username not in self.database:
|
||||
self.database[username] = user_data
|
||||
print("yes")
|
||||
self.save_database()
|
||||
return True
|
||||
print("fail")
|
||||
return False
|
||||
|
||||
def delete_user(self, data):
|
||||
username = self.ensure_username(data)
|
||||
data = data.get('data')
|
||||
if not self.check_credentials(data):
|
||||
return False
|
||||
|
||||
|
@ -52,11 +45,10 @@ class DB:
|
|||
|
||||
def change_data(self, data):
|
||||
username = self.ensure_username(data)
|
||||
data = data.get('data')
|
||||
if not self.check_credentials(data):
|
||||
return False
|
||||
|
||||
self.database[username]['data'] = data
|
||||
self.database[username]['data'] = data.get('data')
|
||||
self.save_database()
|
||||
return True
|
||||
|
||||
|
@ -75,8 +67,6 @@ class DB:
|
|||
username = self.ensure_username(data)
|
||||
password = data.get('password')
|
||||
if username not in self.database:
|
||||
print("no username")
|
||||
print(username)
|
||||
return False
|
||||
|
||||
stored_hashed_password = self.database[username]["hashed_password"]
|
||||
|
@ -92,6 +82,22 @@ class DB:
|
|||
send_back = self.database[username].get('data')
|
||||
return send_back
|
||||
|
||||
def get_email(self, data):
|
||||
username = self.ensure_username(data)
|
||||
if not self.check_credentials(data):
|
||||
return None
|
||||
|
||||
send_back = self.database[username].get('email')
|
||||
return send_back
|
||||
|
||||
def get_name(self, data):
|
||||
username = self.ensure_username(data)
|
||||
if not self.check_credentials(data):
|
||||
return None
|
||||
|
||||
send_back = self.ensure_username(data)
|
||||
return send_back
|
||||
|
||||
def save_database(self):
|
||||
if os.environ.get('PRODUCTION') == "YES":
|
||||
server = pycouchdb.Server("http://admin:admin@localhost:5984/")
|
||||
|
|
Loading…
Reference in a new issue