File size: 1,203 Bytes
7064f87
 
 
385bd50
7064f87
 
 
 
 
 
 
385bd50
7064f87
385bd50
7064f87
 
385bd50
7064f87
 
385bd50
 
 
7064f87
 
 
 
 
 
385bd50
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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