Attendance / utils.py
Jacksonnavigator7's picture
Update utils.py
385bd50 verified
from database import get_conn
def get_device_id():
return "SIMULATED_DEVICE_ID" # βœ… Replace with actual device ID in production
def authenticate_user(email, password, device_id):
conn = get_conn()
cursor = conn.cursor()
cursor.execute("SELECT password, device_id, role FROM users WHERE email=?", (email,))
row = cursor.fetchone()
if not row:
return False, "User not found." # βœ… Email doesn't exist
if row[0] != password:
return False, "Incorrect password." # βœ… Wrong password
if row[2] == "student":
if row[1] and row[1] != device_id:
return False, "Access denied: Different device." # βœ… Device restriction check
elif not row[1]:
cursor.execute("UPDATE users SET device_id=? WHERE email=?", (device_id, email))
conn.commit() # βœ… First login, save the device
conn.close()
return True, "Login successful." # βœ… Authenticated
def get_user_role(email):
conn = get_conn()
cursor = conn.cursor()
cursor.execute("SELECT role FROM users WHERE email=?", (email,))
row = cursor.fetchone()
conn.close() # βœ… GOOD: You added this
return row[0] if row else None