From 2f8839230851348b154623c71624c50037604c2f Mon Sep 17 00:00:00 2001 From: Michael McCamy Date: Wed, 15 Oct 2025 12:35:08 -0400 Subject: [PATCH 1/2] Created auth, db, and test file for user authentication --- test.py | 47 ++++++++++++++++++++++++++++++++++++++ utils/auth.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ utils/db.py | 9 ++++++++ 3 files changed, 118 insertions(+) create mode 100644 test.py create mode 100644 utils/auth.py create mode 100644 utils/db.py diff --git a/test.py b/test.py new file mode 100644 index 0000000..1f6db2d --- /dev/null +++ b/test.py @@ -0,0 +1,47 @@ +from utils.auth import create_volunteer, login_volunteer, create_admin, login_admin + +# --- Test data --- +volunteer_test = { + "name": "Alice", + "email": "alice@test.com", + "password": "volpass123" +} + +admin_test = { + "name": "Bob", + "email": "bob@test.com", + "password": "adminpass123" +} + +ADMIN_KEY = "admin123" + +# --- Volunteer Tests --- +print("=== Volunteer Signup Test ===") +success, msg = create_volunteer(volunteer_test['name'], volunteer_test['email'], volunteer_test['password']) +print("Signup:", msg) + +print("\n=== Volunteer Login Test ===") +success, info = login_volunteer(volunteer_test['email'], volunteer_test['password']) +if success: + print("Login success:", info) +else: + print("Login failed:", info) + +# --- Admin Tests --- +print("\n=== Admin Signup Test ===") +success, msg = create_admin(admin_test['name'], admin_test['email'], admin_test['password']) +print("Admin signup:", msg) + +print("\n=== Admin Login Test ===") +success, info = login_admin(admin_test['email'], admin_test['password'], ADMIN_KEY) +if success: + print("Admin login success:", info) +else: + print("Admin login failed:", info) + +print("\n=== Admin Login Fail Test (wrong key) ===") +success, info = login_admin(admin_test['email'], admin_test['password'], "wrongkey") +if success: + print("Admin login successful (should not happen!)", info) +else: + print("Admin login failed as expected:", info) diff --git a/utils/auth.py b/utils/auth.py new file mode 100644 index 0000000..39c9b11 --- /dev/null +++ b/utils/auth.py @@ -0,0 +1,62 @@ +import hashlib +from utils.db import db + +def hash_password(password: str) -> str: + return hashlib.sha256(password.encode()).hexdigest() + +# Volunteer functions +def create_volunteer(name: str, email: str, password: str): + users = db.child("volunteers").get().val() or {} + if any(u['email'].lower() == email.lower() for u in users.values()): + return False, "Email already registered" + + db.child("volunteers").push({ + "name": name, + "email": email, + "password": hash_password(password), + "role": "volunteer" + }) + return True, "Account created successfully" + +def login_volunteer(email: str, password: str): + users = db.child("volunteers").get().val() or {} + for uid, user in users.items(): + if user['email'].lower() == email.lower() and user['password'] == hash_password(password): + return True, { + "user_id": uid, + "name": user['name'], + "email": user['email'], + "role": "volunteer" + } + return False, "Invalid email or password" + +# Admin functions +ADMIN_KEY = "admin123" # For testing purposes + +def create_admin(name: str, email: str, password: str): + users = db.child("admins").get().val() or {} + if any(u['email'].lower() == email.lower() for u in users.values()): + return False, "Email already registered" + + db.child("admins").push({ + "name": name, + "email": email, + "password": hash_password(password), + "role": "admin" + }) + return True, "Admin account created successfully" + +def login_admin(email: str, password: str, key: str): + if key != ADMIN_KEY: + return False, "Invalid admin key" + + users = db.child("admins").get().val() or {} + for uid, user in users.items(): + if user['email'].lower() == email.lower() and user['password'] == hash_password(password): + return True, { + "user_id": uid, + "name": user['name'], + "email": user['email'], + "role": "admin" + } + return False, "Invalid email or password" diff --git a/utils/db.py b/utils/db.py new file mode 100644 index 0000000..68890f2 --- /dev/null +++ b/utils/db.py @@ -0,0 +1,9 @@ +import json +import pyrebase + +with open("firebase_config.json") as f: + firebase_config = json.load(f) + +firebase = pyrebase.initialize_app(firebase_config) +auth = firebase.auth() # For authentication if needed +db = firebase.database() # Real-time database From 2c894ac3c1cf2c482339d017ea3db6e98d99de1b Mon Sep 17 00:00:00 2001 From: Dylan Panganiban Date: Thu, 16 Oct 2025 12:08:58 -0400 Subject: [PATCH 2/2] Created front end for admin login page --- app.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 67 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 9b639a5..860d02f 100644 --- a/app.py +++ b/app.py @@ -2,7 +2,6 @@ st.set_page_config(layout="wide", initial_sidebar_state="collapsed") -# Session state if "page" not in st.session_state: st.session_state.page = "welcome" @@ -141,7 +140,68 @@ left, right = st.columns(2) with left: st.header("Login") - username = st.text_input("Username", key="login_username") + email = st.text_input("Email", key="login_email") + password = st.text_input("Password", type="password", key="login_password") + if st.button("Login"): + if email == "email" and password == "password": + st.success("Login successful!") + st.session_state.page = "userdashboard" + st.experimental_rerun() + else: + st.error("Invalid email or password") + + with right: + st.header("Sign Up") + name = st.text_input("Name", key="name") + username = st.text_input("Email", key="signup_email") + password = st.text_input("Password", type="password", key="signup_password") + if st.button("Sign Up"): + st.success("Sign Up successful! Please login.") + st.session_state.page = "userlogin" + +if st.session_state.page == "adminlogin": + st.markdown(""" + + +
Admin Login / Sign-Up
+ """, unsafe_allow_html=True) + + st.set_page_config(layout="wide") + left, right = st.columns(2) + with left: + st.header("Login") + email = st.text_input("Email", key="login_email") password = st.text_input("Password", type="password", key="login_password") if st.button("Login"): if username == "user" and password == "password": @@ -152,14 +212,17 @@ st.error("Invalid username or password") with right: - st.header("Signup") + st.header("Sign Up") name = st.text_input("Name", key="name") - username = st.text_input("Username", key="signup_username") + email = st.text_input("Email", key="signup_email") password = st.text_input("Password", type="password", key="signup_password") + adminkey = st.text_input("Admin Key", type="password", key="adminkey") if st.button("Sign Up"): st.success("Sign Up successful! Please login.") st.session_state.page = "userlogin" + + \ No newline at end of file