Update format for return type of the generate token method

This commit is contained in:
sabaimran 2023-11-15 17:31:12 -08:00
parent a348f1a6ab
commit 154de8c629
2 changed files with 9 additions and 6 deletions

View file

@ -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):

View file

@ -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")