refac: api_key field moved to user

This commit is contained in:
Timothy J. Baek 2024-04-02 09:23:55 -07:00
parent 38c34b4444
commit ba0523cd69
3 changed files with 59 additions and 9 deletions

View file

@ -25,7 +25,6 @@ class Auth(Model):
email = CharField()
password = CharField()
active = BooleanField()
api_key = CharField(null=True, unique=True)
class Meta:
database = DB
@ -36,7 +35,6 @@ class AuthModel(BaseModel):
email: str
password: str
active: bool = True
api_key: Optional[str] = None
####################
@ -136,13 +134,8 @@ class AuthsTable:
return None
try:
auth = Auth.get(Auth.api_key == api_key, Auth.active == True)
if auth:
user = Users.get_user_by_id(auth.id)
return user
else:
return None
user = Users.get_user_by_api_key(api_key)
return user if user else None
except:
return False

View file

@ -20,6 +20,7 @@ class User(Model):
role = CharField()
profile_image_url = CharField()
timestamp = DateField()
api_key = CharField(null=True, unique=True)
class Meta:
database = DB
@ -32,6 +33,7 @@ class UserModel(BaseModel):
role: str = "pending"
profile_image_url: str = "/user.png"
timestamp: int # timestamp in epoch
api_key: Optional[str] = None
####################
@ -82,6 +84,13 @@ class UsersTable:
except:
return None
def get_user_by_api_key(self, api_key: str) -> Optional[UserModel]:
try:
user = User.get(User.api_key == api_key)
return UserModel(**model_to_dict(user))
except:
return None
def get_user_by_email(self, email: str) -> Optional[UserModel]:
try:
user = User.get(User.email == email)