"src/vscode:/vscode.git/clone" did not exist on "637b483cf059bb64a57e8013a9c6bbf9f48dc422"
auths.py 3.33 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

from apps.web.models.users import UserModel, Users
Tim Farrell's avatar
Tim Farrell committed
8
from utils.utils import verify_password
9

Timothy J. Baek's avatar
Timothy J. Baek committed
10
from apps.web.internal.db import DB
11
12
13
14
15
16

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


Timothy J. Baek's avatar
Timothy J. Baek committed
17
18
19
20
21
22
23
24
25
26
class Auth(Model):
    id = CharField(unique=True)
    email = CharField()
    password = CharField()
    active = BooleanField()

    class Meta:
        database = DB


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
49
    profile_image_url: str
50
51
52
53
54
55
56
57
58
59
60


class SigninResponse(Token, UserResponse):
    pass


class SigninForm(BaseModel):
    email: str
    password: str


61
62
63
64
class ProfileImageUrlForm(BaseModel):
    profile_image_url: str


65
66
67
68
69
class UpdateProfileForm(BaseModel):
    profile_image_url: str
    name: str


70
71
72
73
74
class UpdatePasswordForm(BaseModel):
    password: str
    new_password: str


75
76
77
78
79
80
81
82
83
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
84
        self.db.create_tables([Auth])
85

Timothy J. Baek's avatar
Timothy J. Baek committed
86
87
88
    def insert_new_auth(
        self, email: str, password: str, name: str, role: str = "pending"
    ) -> Optional[UserModel]:
89
90
91
92
        print("insert_new_auth")

        id = str(uuid.uuid4())

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

98
99
100
101
102
103
104
        user = Users.insert_new_user(id, name, email, role)

        if result and user:
            return user
        else:
            return None

Timothy J. Baek's avatar
Timothy J. Baek committed
105
    def authenticate_user(self, email: str, password: str) -> Optional[UserModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
106
        print("authenticate_user", email)
Timothy J. Baek's avatar
Timothy J. Baek committed
107
108
109
110
111
112
113
114
        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
115
116
            else:
                return None
Timothy J. Baek's avatar
Timothy J. Baek committed
117
        except:
118
119
            return None

Timothy J. Baek's avatar
Timothy J. Baek committed
120
    def update_user_password_by_id(self, id: str, new_password: str) -> bool:
121
        try:
Timothy J. Baek's avatar
Timothy J. Baek committed
122
123
            query = Auth.update(password=new_password).where(Auth.id == id)
            result = query.execute()
Timothy J. Baek's avatar
Timothy J. Baek committed
124
125

            return True if result == 1 else False
126
127
128
        except:
            return False

Timothy J. Baek's avatar
Timothy J. Baek committed
129
130
131
132
133
134
135
136
137
    def update_email_by_id(self, id: str, email: str) -> bool:
        try:
            query = Auth.update(email=email).where(Auth.id == id)
            result = query.execute()

            return True if result == 1 else False
        except:
            return False

138
    def delete_auth_by_id(self, id: str) -> bool:
Timothy J. Baek's avatar
Timothy J. Baek committed
139
140
141
142
143
144
145
        try:
            # Delete User
            result = Users.delete_user_by_id(id)

            if result:
                # Delete Auth
                query = Auth.delete().where(Auth.id == id)
Timothy J. Baek's avatar
Timothy J. Baek committed
146
                query.execute()  # Remove the rows, return number of rows removed.
Timothy J. Baek's avatar
Timothy J. Baek committed
147
148
149
150
151
152
153

                return True
            else:
                return False
        except:
            return False

154
155

Auths = AuthsTable(DB)