fullfill graphql usertype; change db session to dep injection
This commit is contained in:
@@ -8,8 +8,9 @@ import secrets
|
||||
import json
|
||||
from urllib.parse import quote
|
||||
from dotenv import load_dotenv
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import SessionLocal
|
||||
from app.db import SessionLocal, get_db
|
||||
from app.models import User
|
||||
from auth.tokens import create_jwt_token, create_refresh_token, decode_token
|
||||
from app.utils.format import get_image_url
|
||||
@@ -94,7 +95,13 @@ def login_google():
|
||||
return response
|
||||
|
||||
@router.get("/auth/callback")
|
||||
async def auth_callback(request: Request, code: str, state: str, background_tasks: BackgroundTasks):
|
||||
async def auth_callback(
|
||||
request: Request,
|
||||
code: str,
|
||||
state: str,
|
||||
background_tasks: BackgroundTasks,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
cookie_state = request.cookies.get("oauth_state")
|
||||
|
||||
if not cookie_state or cookie_state != state:
|
||||
@@ -121,10 +128,7 @@ async def auth_callback(request: Request, code: str, state: str, background_task
|
||||
)
|
||||
|
||||
user_info = user_res.json()
|
||||
|
||||
db = SessionLocal()
|
||||
|
||||
try:
|
||||
user = db.query(User).filter(User.email == user_info["email"]).first()
|
||||
|
||||
if not user:
|
||||
@@ -166,8 +170,6 @@ async def auth_callback(request: Request, code: str, state: str, background_task
|
||||
|
||||
# 这里不需要再次 refresh,除非后面还要用到更新后的 user 对象
|
||||
# db.refresh(user)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
jwt_token = create_jwt_token({
|
||||
"sub": user.id,
|
||||
|
||||
Reference in New Issue
Block a user