🐛 Fixed OAuth in specific cases

This commit is contained in:
Mawoka
2022-12-01 16:07:07 +01:00
parent b3960db2e6
commit 352e26e6d6
2 changed files with 42 additions and 4 deletions
+20 -2
View File
@@ -4,6 +4,7 @@
import uuid
from typing import Optional
import asyncpg
import authlib.integrations.base_client
from fastapi import APIRouter, Request, HTTPException, Response
from classquiz.config import settings
@@ -114,9 +115,26 @@ async def auth(request: Request, response: Response):
auth_type=UserAuthTypes.GITHUB,
avatar=gzipped_user_avatar(),
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if type(e) == asyncpg.exceptions.UniqueViolationError:
error = True
counter = 1
while error:
try:
await User.objects.create(
id=uuid.uuid4(),
email=user_data.email,
username=user_data.login,
verified=True,
auth_type=UserAuthTypes.GITHUB,
avatar=gzipped_user_avatar(),
)
error = False
except asyncpg.exceptions.UniqueViolationError:
counter += 1
error = True
else:
raise HTTPException(status_code=500, detail=str(e))
user = await User.objects.get_or_none(
email=user_data.email, username=user_data.login, auth_type=UserAuthTypes.GITHUB, verified=True
)