From 5ec8cef53dab1c5fe5ffbff4ff1f43c9d79482be Mon Sep 17 00:00:00 2001 From: Niraj Adhikari Date: Mon, 1 Jul 2024 11:42:54 +0545 Subject: [PATCH] refresh token api --- src/backend/app/users/oauth_routes.py | 12 ++++++++++-- src/backend/app/users/user_routes.py | 4 +++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/backend/app/users/oauth_routes.py b/src/backend/app/users/oauth_routes.py index 733602a3..730251d1 100644 --- a/src/backend/app/users/oauth_routes.py +++ b/src/backend/app/users/oauth_routes.py @@ -6,7 +6,7 @@ from app.db import database from app.users.user_routes import router from app.users.user_deps import init_google_auth, login_required -from app.users.user_schemas import AuthUser +from app.users.user_schemas import AuthUser, Token from app.users import user_crud from app.config import settings @@ -45,7 +45,15 @@ async def callback(request: Request, google_auth=Depends(init_google_auth)): user_data = google_auth.deserialize_access_token(access_token) access_token, refresh_token = user_crud.create_access_token(user_data) - return {"access_token": access_token, "refresh_token": refresh_token} + return Token(access_token=access_token, refresh_token=refresh_token) + + +@router.get("/refresh-token", response_model=Token) +def update_token(user_data: AuthUser = Depends(login_required)): + """Refresh access token""" + + access_token, refresh_token = user_crud.create_access_token(user_data.model_dump()) + return Token(access_token=access_token, refresh_token=refresh_token) @router.get("/my-info/") diff --git a/src/backend/app/users/user_routes.py b/src/backend/app/users/user_routes.py index a1fccfe9..5f4da862 100644 --- a/src/backend/app/users/user_routes.py +++ b/src/backend/app/users/user_routes.py @@ -32,7 +32,9 @@ def login_access_token( elif not user.is_active: raise HTTPException(status_code=400, detail="Inactive user") - access_token, refresh_token = user_crud.create_access_token(user.id) + user_info = {"id": user.id, "email": user.email_address} + + access_token, refresh_token = user_crud.create_access_token(user_info) return Token(access_token=access_token, refresh_token=refresh_token)