fix jwt token decoding; correctly import google auth routes; adjust app/auth/ package
This commit is contained in:
+1
-1
@@ -18,7 +18,7 @@ preset:
|
||||
db_model:
|
||||
- "app/models/*"
|
||||
- "app/db.py"
|
||||
- "app/auth.py"
|
||||
# - "app/auth.py"
|
||||
- "app/main.py"
|
||||
|
||||
graphql:
|
||||
|
||||
+44
-5
@@ -1,11 +1,50 @@
|
||||
from fastapi import APIRouter
|
||||
from .deps import get_current_user
|
||||
from .jwt import create_jwt_token, create_refresh_token, verify_access_token
|
||||
import os
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
from app.utils.format import get_image_url
|
||||
|
||||
# 统一创建并导出路由器,满足 main.py 的 include_router 需求
|
||||
from .deps import get_current_user
|
||||
from .jwt import create_jwt_token, create_refresh_token, verify_access_token, decode_token
|
||||
from .oauth import oauth_router
|
||||
|
||||
# 创建全局 auth 路由器
|
||||
router = APIRouter(tags=["Authentication"])
|
||||
|
||||
# 显式声明这个包对外暴露的接口
|
||||
# 挂载第三方 OAuth 的路由器
|
||||
router.include_router(oauth_router)
|
||||
|
||||
@router.get("/me")
|
||||
def me(user_id = Depends(get_current_user)):
|
||||
# 注意:根据上一轮修改,get_current_user 现在直接返回 user_id
|
||||
from app.db import SessionLocal
|
||||
from app.models import User
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
user = db.query(User).filter(User.id == user_id).first()
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
return {
|
||||
"email": user.email,
|
||||
"name": user.name,
|
||||
"avatar": get_image_url(user.avatar),
|
||||
}
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@router.post("/refresh")
|
||||
def refresh_token(refresh_token: str):
|
||||
payload = decode_token(refresh_token)
|
||||
if payload.get("type") != "refresh":
|
||||
raise HTTPException(status_code=401)
|
||||
user_id = payload.get("sub")
|
||||
new_access = create_jwt_token({"sub": user_id})
|
||||
return {"access_token": new_access}
|
||||
|
||||
@router.post("/logout")
|
||||
def logout():
|
||||
return {"message": "ok"}
|
||||
|
||||
# 显式声明暴露的接口
|
||||
__all__ = [
|
||||
"router",
|
||||
"get_current_user",
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
from fastapi import Request, HTTPException
|
||||
import os
|
||||
from . import jwt
|
||||
from .jwt import decode_token
|
||||
|
||||
JWT_SECRET = os.getenv("JWT_SECRET")
|
||||
JWT_ALGORITHM = os.getenv("JWT_ALGORITHM", "HS256")
|
||||
@@ -13,7 +13,7 @@ def get_current_user(request: Request):
|
||||
raise HTTPException(status_code=401, detail="Not authenticated")
|
||||
|
||||
try:
|
||||
payload = jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM])
|
||||
return payload["user_id"]
|
||||
payload = decode_token(token, JWT_SECRET, algorithms=[JWT_ALGORITHM])
|
||||
return payload["sub"]
|
||||
except:
|
||||
raise HTTPException(status_code=401, detail="Invalid token")
|
||||
@@ -0,0 +1,12 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
# 导入各个渠道的路由
|
||||
from .google import router as google_router
|
||||
# from .github import router as github_router
|
||||
|
||||
# 创建一个总的 oauth 路由器
|
||||
oauth_router = APIRouter()
|
||||
|
||||
# 包含 Google 的路由
|
||||
oauth_router.include_router(google_router)
|
||||
# oauth_router.include_router(github_router)
|
||||
@@ -171,27 +171,27 @@ async def auth_callback(request: Request, code: str, state: str, background_task
|
||||
# }
|
||||
}
|
||||
|
||||
@router.get("/me")
|
||||
def me(user = Depends(get_current_user)):
|
||||
return {
|
||||
"email": user.email,
|
||||
"name": user.name,
|
||||
"avatar": get_image_url(user.avatar),
|
||||
}
|
||||
# @router.get("/me")
|
||||
# def me(user = Depends(get_current_user)):
|
||||
# return {
|
||||
# "email": user.email,
|
||||
# "name": user.name,
|
||||
# "avatar": get_image_url(user.avatar),
|
||||
# }
|
||||
|
||||
@router.post("/refresh")
|
||||
def refresh_token(refresh_token: str):
|
||||
payload = decode_token(refresh_token)
|
||||
# @router.post("/refresh")
|
||||
# def refresh_token(refresh_token: str):
|
||||
# payload = decode_token(refresh_token)
|
||||
|
||||
if payload.get("type") != "refresh":
|
||||
raise HTTPException(status_code=401)
|
||||
# if payload.get("type") != "refresh":
|
||||
# raise HTTPException(status_code=401)
|
||||
|
||||
user_id = payload.get("sub")
|
||||
# user_id = payload.get("sub")
|
||||
|
||||
new_access = create_jwt_token({"sub": user_id})
|
||||
# new_access = create_jwt_token({"sub": user_id})
|
||||
|
||||
return {"access_token": new_access}
|
||||
# return {"access_token": new_access}
|
||||
|
||||
@router.post("/logout")
|
||||
def logout():
|
||||
return {"message": "ok"}
|
||||
# @router.post("/logout")
|
||||
# def logout():
|
||||
# return {"message": "ok"}
|
||||
@@ -26,3 +26,8 @@ graphql_router = GraphQLRouter(schema, context_getter=get_context)
|
||||
# 挂载路由
|
||||
app.include_router(auth_router) # 保留现有的 auth 路由供 Google OAuth 回调使用
|
||||
app.include_router(graphql_router, prefix="/graphql") # 挂载 GraphQL 终点,前端以后只需要请求 /graphql
|
||||
|
||||
# test code
|
||||
for route in app.routes:
|
||||
methods = getattr(route, "methods", "N/A")
|
||||
print(f"Path: {route.path} | Name: {route.name} | Methods: {methods}")
|
||||
Reference in New Issue
Block a user