-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
42 lines (39 loc) · 1.49 KB
/
Copy pathauth.py
File metadata and controls
42 lines (39 loc) · 1.49 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
from transport.routes import load_users, save_users, FOLDER
from utils import header, today, hash_pw
import os
def setup(): os.makedirs(FOLDER, exist_ok=True)
def register():
header("CREATE NEW ACCOUNT")
users = load_users()
while True:
u = input("Choose a username: ").strip().lower()
if not u: print("Username cannot be empty.")
elif u in users: print("Username taken. Try another.")
else: break
while True:
pw = input("Choose a password (min 4 chars): ").strip()
if len(pw) < 4: print("Password too short."); continue
if input("Confirm password: ").strip() != pw: print("Passwords do not match.")
else: break
while True:
try:
bal = float(input("Add money to wallet (Rs.): "))
if bal >= 0: break
print("Amount cannot be negative.")
except ValueError: print("Enter a number like 500.")
users[u] = {"password": hash_pw(pw), "wallet": bal, "joined_on": today()}
save_users(users)
print(f"\nAccount created! Welcome, {u}")
return u
def login():
header("LOGIN")
users, attempts = load_users(), 3
while attempts > 0:
u, pw = input("Username: ").strip().lower(), input("Password: ").strip()
if u in users and users[u]["password"] == hash_pw(pw):
print(f"\nWelcome back, {u}!")
return u
attempts -= 1
print(f"Wrong credentials. {attempts} attempt(s) left.")
print("Too many failed attempts.")
return None