From 154de8c629dc5cd6644ce944f4feef246d9a3c17 Mon Sep 17 00:00:00 2001 From: sabaimran Date: Wed, 15 Nov 2023 17:31:12 -0800 Subject: [PATCH] Update format for return type of the generate token method --- src/database/adapters/__init__.py | 4 +--- src/khoj/routers/auth.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/database/adapters/__init__.py b/src/database/adapters/__init__.py index 70d94df3..f381fb42 100644 --- a/src/database/adapters/__init__.py +++ b/src/database/adapters/__init__.py @@ -64,9 +64,7 @@ async def create_khoj_token(user: KhojUser, name=None): "Create Khoj API key for user" token = f"kk-{secrets.token_urlsafe(32)}" name = name or f"{generate_random_name().title()}" - api_config = await KhojApiUser.objects.acreate(token=token, user=user, name=name) - await api_config.asave() - return api_config + return await KhojApiUser.objects.acreate(token=token, user=user, name=name) def get_khoj_tokens(user: KhojUser): diff --git a/src/khoj/routers/auth.py b/src/khoj/routers/auth.py index 4a3cbcef..2ccef80c 100644 --- a/src/khoj/routers/auth.py +++ b/src/khoj/routers/auth.py @@ -16,6 +16,7 @@ from google.auth.transport import requests as google_requests # Internal Packages from database.adapters import get_khoj_tokens, get_or_create_user, create_khoj_token, delete_khoj_token +from database.models import KhojApiUser from khoj.utils import state @@ -50,12 +51,16 @@ async def login(request: Request): @auth_router.post("/token") @requires(["authenticated"], redirect="login_page") -async def generate_token(request: Request, token_name: Optional[str] = None) -> str: +async def generate_token(request: Request, token_name: Optional[str] = None): "Generate API token for given user" if token_name: - return await create_khoj_token(user=request.user.object, name=token_name) + token = await create_khoj_token(user=request.user.object, name=token_name) else: - return await create_khoj_token(user=request.user.object) + token = await create_khoj_token(user=request.user.object) + return { + "token": token.token, + "name": token.name, + } @auth_router.get("/token")