Only create KhojUser on login if doesn't already exist

This commit is contained in:
Debanjum Singh Solanky 2023-11-14 18:02:31 -08:00
parent 3d8d6145f2
commit f8e5e118e1

View file

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