🐛 Fixed OAuth in specific cases
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
import uuid
|
import uuid
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
import asyncpg
|
||||||
import authlib.integrations.base_client
|
import authlib.integrations.base_client
|
||||||
from fastapi import APIRouter, Request, HTTPException, Response
|
from fastapi import APIRouter, Request, HTTPException, Response
|
||||||
from classquiz.config import settings
|
from classquiz.config import settings
|
||||||
@@ -114,9 +115,26 @@ async def auth(request: Request, response: Response):
|
|||||||
auth_type=UserAuthTypes.GITHUB,
|
auth_type=UserAuthTypes.GITHUB,
|
||||||
avatar=gzipped_user_avatar(),
|
avatar=gzipped_user_avatar(),
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
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(
|
user = await User.objects.get_or_none(
|
||||||
email=user_data.email, username=user_data.login, auth_type=UserAuthTypes.GITHUB, verified=True
|
email=user_data.email, username=user_data.login, auth_type=UserAuthTypes.GITHUB, verified=True
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
import asyncpg
|
||||||
from fastapi import APIRouter, Request, HTTPException, Response
|
from fastapi import APIRouter, Request, HTTPException, Response
|
||||||
from classquiz.config import settings
|
from classquiz.config import settings
|
||||||
|
|
||||||
@@ -80,7 +81,7 @@ async def auth(request: Request, response: Response):
|
|||||||
if user_in_db is None:
|
if user_in_db is None:
|
||||||
# REGISTER USER
|
# REGISTER USER
|
||||||
try:
|
try:
|
||||||
await User.object.create(
|
await User.objects.create(
|
||||||
id=uuid.uuid4(),
|
id=uuid.uuid4(),
|
||||||
email=user_data.email,
|
email=user_data.email,
|
||||||
username=user_data.name,
|
username=user_data.name,
|
||||||
@@ -90,7 +91,26 @@ async def auth(request: Request, response: Response):
|
|||||||
avatar=gzipped_user_avatar(),
|
avatar=gzipped_user_avatar(),
|
||||||
)
|
)
|
||||||
except Exception as e:
|
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=f"{user_data.name}{counter}",
|
||||||
|
verified=user_data.email_verified,
|
||||||
|
auth_type=UserAuthTypes.GOOGLE,
|
||||||
|
google_uid=user_data.sub,
|
||||||
|
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(
|
user = await User.objects.get_or_none(
|
||||||
email=user_data.email, google_uid=user_data.sub, auth_type=UserAuthTypes.GOOGLE, verified=True
|
email=user_data.email, google_uid=user_data.sub, auth_type=UserAuthTypes.GOOGLE, verified=True
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user