ming 3 years ago
parent
commit
8901bd173a
2 changed files with 14 additions and 6 deletions
  1. 10 5
      api/main.py
  2. 4 1
      api/models.py

+ 10 - 5
api/main.py

@@ -129,12 +129,17 @@ async def make_video_slide(request: Request, response: Response, Authorize: Auth
     current_user = Authorize.get_jwt_subject()
     return templates.TemplateResponse("make_video_slide.html", {"request": request, "response": response})
 
-@app.get('/user_profile', response_class=HTMLResponse)
-def protected(request: Request, Authorize: AuthJWT = Depends()):
+@app.post('/user_profile', response_class=HTMLResponse)
+async def user_profile(token_obj: jwt_token_only):
     db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
-    Authorize.jwt_required()
-    current_user = Authorize.get_jwt_subject()
-    user_obj = first(db.query('SELECT * FROM users where username ="'+str(current_user)+'"'))
+    
+    user_obj = first(db.query('SELECT * FROM users where token ="'+token_obj.token+'"'))
+    if token_obj.token != user_obj['token']:
+        raise HTTPException(
+            status_code=status.HTTP_401_UNAUTHORIZED,
+            detail="Missing token",
+            headers={"WWW-Authenticate": "Bearer"},
+        )
     video_num = str(first(db.query('SELECT COUNT(*) FROM history_input WHERE user_id ='+str(user_obj['id'])))['COUNT(*)'])
     total_sec = str(first(db.query('SELECT SUM(duration) FROM history_input where user_id='+str(user_obj['id'])))['SUM(duration)'])
     left_sec = user_obj['left_time']

+ 4 - 1
api/models.py

@@ -61,4 +61,7 @@ class phone(BaseModel):
 class register_req(BaseModel):
     username: str
     email: str
-    password: str
+    password: str
+
+class jwt_token_only(BaseModel):
+    token: str