问题描述
所以我在 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
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)