auths.py 2.18 KB
Newer Older
1
2
3
4
from pydantic import BaseModel
from typing import List, Union, Optional
import time
import uuid
Timothy J. Baek's avatar
Timothy J. Baek committed
5
from peewee import *
6
7
8


from apps.web.models.users import UserModel, Users
Timothy J. Baek's avatar
Timothy J. Baek committed
9
from utils.utils import (
10
11
12
13
14
15
    verify_password,
    get_password_hash,
    bearer_scheme,
    create_token,
)

Timothy J. Baek's avatar
Timothy J. Baek committed
16
from apps.web.internal.db import DB
17
18
19
20
21
22

####################
# DB MODEL
####################


Timothy J. Baek's avatar
Timothy J. Baek committed
23
24
25
26
27
28
29
30
31
32
class Auth(Model):
    id = CharField(unique=True)
    email = CharField()
    password = CharField()
    active = BooleanField()

    class Meta:
        database = DB


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
Timothy J. Baek's avatar
Timothy J. Baek committed
55
    profile_image_url: str
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75


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
Timothy J. Baek's avatar
Timothy J. Baek committed
76
        self.db.create_tables([Auth])
77
78

    def insert_new_auth(
Timothy J. Baek's avatar
Timothy J. Baek committed
79
        self, email: str, password: str, name: str, role: str = "pending"
80
81
82
83
84
85
86
87
    ) -> Optional[UserModel]:
        print("insert_new_auth")

        id = str(uuid.uuid4())

        auth = AuthModel(
            **{"id": id, "email": email, "password": password, "active": True}
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
88
89
        result = Auth.create(**auth.model_dump())

90
91
92
93
94
95
96
97
        user = Users.insert_new_user(id, name, email, role)

        if result and user:
            return user
        else:
            return None

    def authenticate_user(self, email: str, password: str) -> Optional[UserModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
98
        print("authenticate_user", email)
Timothy J. Baek's avatar
Timothy J. Baek committed
99
100
101
102
103
104
105
106
        try:
            auth = Auth.get(Auth.email == email, Auth.active == True)
            if auth:
                if verify_password(password, auth.password):
                    user = Users.get_user_by_id(auth.id)
                    return user
                else:
                    return None
107
108
            else:
                return None
Timothy J. Baek's avatar
Timothy J. Baek committed
109
        except:
110
111
112
113
            return None


Auths = AuthsTable(DB)