-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.py
123 lines (100 loc) · 3.94 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
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import hashlib
from wtforms import Form, validators, \
EmailField, PasswordField, StringField, IntegerField
import json
import flask
import userDB
import sqlite3
from flask_login import UserMixin
class UserRegisterForm(Form):
email = EmailField('Email',
[validators.DataRequired(),
validators.Email()])
password = PasswordField('Password',
[validators.DataRequired(),
validators.length(min=8, max=30)])
name = StringField('Username',
[validators.DataRequired(),
validators.length(min=3, max=30)])
mobile_phone = StringField('Mobile',
[validators.DataRequired(),
validators.length(min=10, max=10)])
zipcode = IntegerField('Zipcode', [validators.DataRequired()])
class UserLoginForm(Form):
email = EmailField('Email',
[validators.DataRequired(),
validators.Email()])
password = PasswordField('Password',
[validators.DataRequired(),
validators.length(min=8, max=30)])
def register(raw_form):
form = UserRegisterForm(raw_form)
if not form.validate():
return json.dumps({"Input error": form.errors}), 400
hash_object = hashlib.sha256(str(form.password.data).encode('utf-8'))
user = (form.email.data, hash_object.hexdigest(),
form.name.data, form.zipcode.data, form.mobile_phone.data)
conn = None
try:
conn = sqlite3.connect("sqlite_db")
cur = conn.cursor()
sql = "INSERT INTO User" \
"(email, password, name, zipcode, phone_number) " \
"VALUES(?, ?, ?, ?, ?) "
cur.execute(sql, user)
conn.commit()
except sqlite3.Error as e:
return json.dumps({"error": f"db error: {str(e)}"}), 500
finally:
if conn:
conn.close()
return json.dumps({"error": ""}), 201
class UserLoginObj(UserMixin):
def __init__(self, saved_user: userDB.User):
self.username = saved_user.get_name()
self.password_hash = saved_user.get_password()
self.email = saved_user.get_email()
self.user = saved_user
def get_id(self): # the primary key is email, so return email
# get_id overwrite parent class method
return self.email
def get_email(self):
return self.email
@staticmethod
def get(email):
if not email:
return None
saved_user = userDB.select_user_by_email(email)
print(saved_user)
if not saved_user:
return None
return UserLoginObj(saved_user)
def set_user_cookie(email: str, resp):
resp.set_cookie('user', email)
def need_login_response():
return json.dumps({"error": "Please login first"}), 400
def user_login(raw_form):
try:
form = UserLoginForm(raw_form)
if not form.validate():
return json.dumps({"error": "invalid input"}), 400, None
password = hashlib.sha256(
str(form.password.data).encode('utf-8')).hexdigest()
email = form.email.data
print(email, password)
saved_user = userDB.select_user_by_email(email)
print(saved_user)
if not saved_user:
return json.dumps({"error": f"No such email {email}"}), 400, None
if not password == saved_user.get_password():
return json.dumps({"error": f"wrong password {email}"}), 400, None
resp = flask.make_response(json.dumps({"error": ""}))
return resp, 200, UserLoginObj(saved_user)
except Exception as e:
print(e)
return json.dumps({"error": "Internal error"}), 500, None
def user_logout_resp():
return flask.make_response(json.dumps({"error": ""})), 200
def need_login():
return flask.make_response(
json.dumps({"error": "You need to login to visit this page"})), 401