File size: 4,527 Bytes
bf2bf0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from typing import List

from fastapi import APIRouter, HTTPException, Depends

from api.sources.user.data_sources.auth import AuthDataSource
from api.sources.user.request_params.auth import LoginRequest, RegisterRequest, VerifyRequest, UpdatePromptsRequest, RefreshRequest, \
    LoginPasswordlessRequest, VerifyPasswordlessRequest, UserExistsRequest
from core.depends.oauth import get_current_user
from core.schemas.prompt import PromptModel

router = APIRouter(prefix='/auth')


@router.post("/fetch-prompts")
async def fetch_prompts(current_user: dict = Depends(get_current_user)):
    prompts: List[PromptModel] = await AuthDataSource.fetch_prompts()
    return {"result": prompts}


@router.post("/update-prompts")
async def update_prompts(request: UpdatePromptsRequest, current_user: dict = Depends(get_current_user)):
    uuid = current_user['id']
    default_amount = 50
    try:
        await AuthDataSource.update_prompts(uuid=uuid, prompts=request.prompts['prompts'])
        await AuthDataSource.update_wallet_amount(uuid=uuid, amount=default_amount)
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error: {e}")
    return {"result": default_amount}


@router.post("/refresh")
async def refresh_auth_state(request: RefreshRequest):
    try:
        res = await AuthDataSource.refresh_auth_state(request.refresh_token)
        return {"result": res}
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error: {e}")


@router.post("/user-exists")
async def user_exists(request: UserExistsRequest):
    print(request.email)
    print(request.phone)
    if not request.email and not request.phone:
        raise HTTPException(status_code=500, detail=f"Pass either `email` or `phone`")
    try:
        user, is_new_user = await AuthDataSource.does_user_exists(email=request.email, phone=request.phone)
        return {"result": {
            "user_data": user,
            "user_exists": is_new_user,
        }}
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error: {e}")


@router.post("/login")
async def login(request: LoginRequest):
    try:
        auth_data = await AuthDataSource.login(email=request.email, password=request.password)
        user_data = await AuthDataSource.verify(uuid=auth_data['uuid'])
        return {"result": {
            "user_data": user_data,
            "auth_data": auth_data
        }}
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Unable to login with following credentials.")


@router.post("/login-passwordless")
async def login_passwordless(request: LoginPasswordlessRequest):
    try:
        await AuthDataSource.login_passwordless(phone=request.phone)
        return {"result": True}
    except Exception as e:
        print(e)
        raise HTTPException(status_code=500, detail=f"Unable to login with following credentials.")


@router.post("/verify-passwordless")
async def verify_passwordless(request: VerifyPasswordlessRequest):
    try:
        auth_data, is_new_user = await AuthDataSource.verify_passwordless(phone=request.phone, token=request.token)
        return {"result": {
            "auth_data": auth_data,
            "is_new_user": is_new_user
        }}
    except Exception as e:
        print(e)
        raise HTTPException(status_code=500, detail=f"Unable to login with following credentials.")


@router.post("/register-user-info")
async def register_user_info(request: RegisterRequest, current_user: dict = Depends(get_current_user)):
    try:
        uuid = current_user['id']
        data = request.dict()
        data['uuid'] = uuid
        await AuthDataSource.register_in_db(**data)
        return {"result": uuid}
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error: {e}")


@router.post("/register")
async def register(request: RegisterRequest):
    try:
        uuid = await AuthDataSource.register(email=request.email,
                                             password=request.password_hash)
        print(uuid)
        await AuthDataSource.register_in_db(uuid, **request.dict())
        return {"result": uuid}
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error: {e}")


@router.post("/verify")
async def verify(request: VerifyRequest):
    try:
        data = await AuthDataSource.verify(uuid=request.uuid)
        return {"result": data}
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error: {e}")