-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.py
30 lines (24 loc) · 1.01 KB
/
user.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
from sqlalchemy import text
from werkzeug.security import check_password_hash, generate_password_hash
from db import db
def create_user(username: str, password: str):
password = generate_password_hash(password, method='sha256')
sql = text("INSERT INTO users (username, password) VALUES (:username, :password)")
db.session.execute(sql, {"username":username, "password":password})
db.session.commit()
def username_already_exists(username: str):
sql = text("SELECT username FROM users WHERE username=:username")
name = db.session.execute(sql, {"username": username})
name_fetch = name.fetchone()
if name_fetch:
return True
else:
return False
def login_db(username: str, password: str):
sql = text("SELECT id, password FROM users WHERE username=:username")
user = db.session.execute(sql, {"username":username})
user_fetch = user.fetchone()
if user_fetch and check_password_hash(user_fetch[1], password):
return user_fetch[0]
else:
return None