-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
59 lines (55 loc) · 1.7 KB
/
auth.py
File metadata and controls
59 lines (55 loc) · 1.7 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import sqlite3
import hashlib
def hash_password(password):
return hashlib.sha256(password.encode()).hexdigest()
def init_db():
conn = sqlite3.connect('users.db')
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
role TEXT NOT NULL DEFAULT 'user',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
c.execute("SELECT * FROM users WHERE username='admin'")
if not c.fetchone():
hashed_password = hash_password("admin123")
c.execute(
"INSERT INTO users (username, password, role) VALUES (?, ?, ?)",
("admin", hashed_password, "admin")
)
conn.commit()
conn.close()
def register_user(username, password, role="user"):
init_db()
conn = sqlite3.connect('users.db')
c = conn.cursor()
try:
hashed_password = hash_password(password)
c.execute(
"INSERT INTO users (username, password, role) VALUES (?, ?, ?)",
(username, hashed_password, role)
)
conn.commit()
return True, "Registrasi berhasil"
except sqlite3.IntegrityError:
return False, "Username sudah terdaftar"
finally:
conn.close()
def login_user(username, password):
init_db()
conn = sqlite3.connect('users.db')
c = conn.cursor()
c.execute(
"SELECT password, role FROM users WHERE username=?",
(username,)
)
result = c.fetchone()
conn.close()
if result and result[0] == hash_password(password):
return True, result[1]
else:
return False, None