所以我在 FastAPI 中有这个小项目,如果用户未登录,它实际上是一个将用户重定向到登录页面的代码。
问题是它在请求时成功重定向:请求未通过。当我通过它们时,它不会重定向而是返回 RedirectResponse 对象。我需要请求,因为我正在使用 HTML。
def loginRequired(token: str = Depends(oauth2_scheme)):
credentials_exception = RedirectResponse(url='/login')
credentials_exception.delete_cookie(key="access_token")
if token =='REDIRECT':
return credentials_exception
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub")
if username is None:
return credentials_exception
token_data = TokenData(username=username)
except JWTError:
return credentials_exception
user = USER.getUserDetails(username=token_data.username) | {'token':token}
if user is None:
return credentials_exception
return user
@app.get("/profile")
def DashBoard( request:Request,current_user : User =Depends(loginRequired)):
data= current_user
summary={'total':0,'average':0,'withdrawn':0}
return data