-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.py
35 lines (29 loc) · 1.05 KB
/
account.py
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
import secrets
from db import db
from flask import session, request, abort
from werkzeug.security import check_password_hash, generate_password_hash
def login(username, password):
sql = "SELECT id, password FROM accounts WHERE username=:username"
account = db.session.execute(sql, {"username": username}).fetchone()
if not account:
return False
print("acc not in db")
else:
passhash = account[1]
if check_password_hash(passhash, password):
session["account_id"] = account[0]
session["username"] = username
return True
else:
return False
print("passhash wrong")
def logout():
session.clear()
def register(username, password):
passhash = generate_password_hash(password)
sql = "INSERT INTO accounts (username, password) VALUES (:username, :password)"
db.session.execute(sql, {"username": username, "password": passhash})
db.session.commit()
return login(username, password)
def account_id():
return session.get("account_id", 0)