-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
73 lines (53 loc) · 2.36 KB
/
Copy pathmodels.py
File metadata and controls
73 lines (53 loc) · 2.36 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from datetime import datetime, timedelta
import os
import jwt
from app import db
from werkzeug.security import generate_password_hash, check_password_hash
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100), unique=True)
phone = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(100), unique=True, nullable=False)
nin = db.Column(db.String(30), unique=True, nullable=False)
password_hash = db.Column(db.String(100), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
def __repr__(self):
return f'<User {self.email}>'
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
def generate_auth_token(self):
expiration_time = datetime.utcnow() + timedelta(days=10)
payload = {
'id' : self.id,
'exp' : expiration_time
}
token = jwt.encode(payload, os.environ.get('SECRET_KEY'), algorithm='HS256')
return token
@staticmethod
def verify_auth_token(token):
if not token:
return None
try:
active_token = StoredjwtToken.query.filter_by(jwt_token=token).first()
if active_token:
payload = jwt.decode(token, os.environ.get('SECRET_KEY'), algorithms=['HS256'])
user = User.query.get(payload['id'])
return user
except jwt.ExpiredSignatureError:
print("Token has expired")
return None
except jwt.DecodeError:
print("Token is invalid")
return None
class StoredjwtToken(db.Model):
id = db.Column(db.Integer, primary_key=True)
jwt_token = db.Column(db.String(255), unique=True, nullable=True)
user_id = db.Column(db.Integer, nullable=True)
class PasswordResetToken(db.Model):
id = db.Column(db.Integer, primary_key=True)
token = db.Column(db.String(10), nullable=False)
user_id = db.Column(db.Integer, nullable=False)
used = db.Column(db.Boolean, nullable=False, default=False)
generated_at = db.Column(db.DateTime, nullable=False, default=datetime.now)