diff --git a/src/database/adapters/__init__.py b/src/database/adapters/__init__.py index 9aa7eb5c..39ac290b 100644 --- a/src/database/adapters/__init__.py +++ b/src/database/adapters/__init__.py @@ -75,13 +75,16 @@ async def delete_khoj_token(user: KhojUser, token: str): async def get_or_create_user(token: dict) -> KhojUser: user = await get_user_by_token(token) if not user: - user = await create_google_user(token) + user = await create_user_by_token(token) return user -async def create_google_user(token: dict) -> KhojUser: - user = await KhojUser.objects.acreate(username=token.get("email"), email=token.get("email")) - await user.asave() +async def create_user_by_token(token: dict) -> KhojUser: + user = await KhojUser.objects.filter(email=token.get("email")).aget_or_none() + if not user: + user = await KhojUser.objects.acreate(username=token.get("email"), email=token.get("email")) + await user.asave() + await GoogleUser.objects.acreate( sub=token.get("sub"), azp=token.get("azp"),