使用官方登录重定向方案

This commit is contained in:
2025-03-06 09:17:35 +08:00
committed by GitHub
parent 4c2898d9a0
commit 277f5d58cb

View File

@@ -47,14 +47,14 @@ class AuthMiddleware(BaseHTTPMiddleware):
logging.info(f"访问路径: {request.url.path}," logging.info(f"访问路径: {request.url.path},"
f"认证状态: {app.storage.user.get('authenticated')}") f"认证状态: {app.storage.user.get('authenticated')}")
if not app.storage.user.get('authenticated', False): if not app.storage.user.get('authenticated', False):
# 如果请求的路径在Client.page_routes.values()中并且不在unrestricted_page_routes中 # 如果请求的路径不是nicegui的静态文件并且不在unrestricted_page_routes中
if request.url.path in Client.page_routes.values() \ if not request.url.path.startwith('/_nicegui') \
and request.url.path in AUTH_CONFIG["restricted_routes"]: and request.url.path in AUTH_CONFIG["restricted_routes"]:
logging.warning(f"未认证用户尝试访问: {request.url.path}") logging.warning(f"未认证用户尝试访问: {request.url.path}")
# 记录用户想访问的路径 Record the user's intended path # 记录用户想访问的路径 Record the user's intended path
app.storage.user['referrer_path'] = request.url.path app.storage.user['referrer_path'] = request.url.path
# 重定向到登录页面 Redirect to the login page # 重定向到登录页面 Redirect to the login page
return RedirectResponse(AUTH_CONFIG["login_url"]) return RedirectResponse(f'/login?redirect_to={request.url.path}')
# 否则,继续处理请求 Otherwise, continue processing the request # 否则,继续处理请求 Otherwise, continue processing the request
return await call_next(request) return await call_next(request)