Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -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)
62 changes: 62 additions & 0 deletions utils/auth.py
Original file line number Diff line number Diff line change
@@ -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"
9 changes: 9 additions & 0 deletions utils/db.py
Original file line number Diff line number Diff line change
@@ -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