2023-11-19 00:47:12 +00:00
|
|
|
from pydantic import BaseModel
|
|
|
|
from typing import List, Union, Optional
|
|
|
|
import time
|
|
|
|
import uuid
|
2023-12-26 05:44:28 +00:00
|
|
|
from peewee import *
|
2023-11-19 00:47:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
from apps.web.models.users import UserModel, Users
|
2023-11-19 05:41:43 +00:00
|
|
|
from utils.utils import (
|
2023-11-19 00:47:12 +00:00
|
|
|
verify_password,
|
|
|
|
get_password_hash,
|
|
|
|
bearer_scheme,
|
|
|
|
create_token,
|
|
|
|
)
|
|
|
|
|
2023-12-26 05:44:28 +00:00
|
|
|
from apps.web.internal.db import DB
|
2023-11-19 00:47:12 +00:00
|
|
|
|
|
|
|
####################
|
|
|
|
# DB MODEL
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
2023-12-26 05:44:28 +00:00
|
|
|
class Auth(Model):
|
|
|
|
id = CharField(unique=True)
|
|
|
|
email = CharField()
|
|
|
|
password = CharField()
|
|
|
|
active = BooleanField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
database = DB
|
|
|
|
|
|
|
|
|
2023-11-19 00:47:12 +00:00
|
|
|
class AuthModel(BaseModel):
|
|
|
|
id: str
|
|
|
|
email: str
|
|
|
|
password: str
|
|
|
|
active: bool = True
|
|
|
|
|
|
|
|
|
|
|
|
####################
|
|
|
|
# Forms
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
|
|
|
class Token(BaseModel):
|
|
|
|
token: str
|
|
|
|
token_type: str
|
|
|
|
|
|
|
|
|
|
|
|
class UserResponse(BaseModel):
|
|
|
|
id: str
|
|
|
|
email: str
|
|
|
|
name: str
|
|
|
|
role: str
|
2023-11-19 05:41:43 +00:00
|
|
|
profile_image_url: str
|
2023-11-19 00:47:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SigninResponse(Token, UserResponse):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class SigninForm(BaseModel):
|
|
|
|
email: str
|
|
|
|
password: str
|
|
|
|
|
|
|
|
|
|
|
|
class SignupForm(BaseModel):
|
|
|
|
name: str
|
|
|
|
email: str
|
|
|
|
password: str
|
|
|
|
|
|
|
|
|
|
|
|
class AuthsTable:
|
|
|
|
def __init__(self, db):
|
|
|
|
self.db = db
|
2023-12-26 05:44:28 +00:00
|
|
|
self.db.create_tables([Auth])
|
2023-11-19 00:47:12 +00:00
|
|
|
|
|
|
|
def insert_new_auth(
|
2023-11-19 05:41:43 +00:00
|
|
|
self, email: str, password: str, name: str, role: str = "pending"
|
2023-11-19 00:47:12 +00:00
|
|
|
) -> Optional[UserModel]:
|
|
|
|
print("insert_new_auth")
|
|
|
|
|
|
|
|
id = str(uuid.uuid4())
|
|
|
|
|
|
|
|
auth = AuthModel(
|
|
|
|
**{"id": id, "email": email, "password": password, "active": True}
|
|
|
|
)
|
2023-12-26 05:44:28 +00:00
|
|
|
result = Auth.create(**auth.model_dump())
|
|
|
|
print(result)
|
|
|
|
|
2023-11-19 00:47:12 +00:00
|
|
|
user = Users.insert_new_user(id, name, email, role)
|
|
|
|
|
|
|
|
print(result, user)
|
|
|
|
if result and user:
|
|
|
|
return user
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def authenticate_user(self, email: str, password: str) -> Optional[UserModel]:
|
2023-12-26 05:44:28 +00:00
|
|
|
print("authenticate_user", email)
|
2023-11-19 00:47:12 +00:00
|
|
|
|
2023-12-26 05:44:28 +00:00
|
|
|
auth = Auth.get(Auth.email == email, Auth.active == True)
|
|
|
|
print(auth.email)
|
2023-11-19 00:47:12 +00:00
|
|
|
|
|
|
|
if auth:
|
2023-12-26 05:44:28 +00:00
|
|
|
print(password, str(auth.password))
|
|
|
|
print(verify_password(password, str(auth.password)))
|
|
|
|
if verify_password(password, auth.password):
|
|
|
|
user = Users.get_user_by_id(auth.id)
|
|
|
|
|
|
|
|
print(user)
|
|
|
|
return user
|
2023-11-19 00:47:12 +00:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
Auths = AuthsTable(DB)
|