-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth_routes.py
44 lines (37 loc) · 1.31 KB
/
auth_routes.py
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
from fastapi import APIRouter, Request, HTTPException
from auth_utils import login_user, reset_password, get_hashed_password
from db_utils import validate_user
from fastapi.responses import JSONResponse
INTERNAL_API_KEY = "DUMMY_KEY"
router = APIRouter()
@router.post("/login")
async def login(request: Request):
params = await request.json()
username = params.get("username", None)
password = params.get("password", None)
if not username:
return {"error": "no user id provided"}
if not password:
return {"error": "no password provided"}
dets = login_user(username, password)
return dets
@router.post("/reset_password")
async def reset_password(request: Request):
params = await request.json()
username = params.get("username", None)
new_password = params.get("password", None)
token = params.get("token", None)
if not validate_user(token, user_type="admin"):
return JSONResponse(
status_code=401,
content={
"error": "unauthorized",
"message": "Invalid username or password",
},
)
if not username:
return {"error": "no user id provided"}
if not new_password:
return {"error": "no password provided"}
dets = reset_password(username, new_password)
return dets