diff --git a/oauth/views.py b/oauth/views.py index 639c996..5027976 100644 --- a/oauth/views.py +++ b/oauth/views.py @@ -6,6 +6,7 @@ from django.contrib.auth import login from django.http.request import HttpRequest from django.shortcuts import redirect +import users from users.models import CustomUser logger = logging.getLogger(__file__) @@ -28,17 +29,16 @@ def register_callback(req: HttpRequest): csrftoken = req.COOKIES.get('csrftoken') print(csrftoken) response = requests.post(settings.OAUTH["ACCESS_TOKEN_URI"], - data={'code': code, - 'grant_type': 'authorization_code', - 'client_id': settings.OAUTH["CLIENT_ID"], + data={'code': code, + 'grant_type': 'authorization_code', + 'client_id': settings.OAUTH["CLIENT_ID"], 'client_secret': settings.OAUTH["CLIENT_SECRET"], - 'redirect_uri': settings.OAUTH["REDIRECT_URI"]}, + 'redirect_uri': settings.OAUTH["REDIRECT_URI"]}, cookies=None, headers={'Referer': f'{settings.SERVER_URL}/login/zeus/register'}) try: if response.status_code == 200: json: dict = response.json() - csrftoken = response.cookies['csrftoken'] print(response.cookies) # TODO: maybe later do something with the refresh token. user: dict = user_info(json['access_token'], csrftoken) @@ -59,12 +59,13 @@ def register_callback(req: HttpRequest): def validate_user(zeus_id, username) -> CustomUser: - user = CustomUser.objects.get(zeus_id=zeus_id) - if user is None: + try: + user = CustomUser.objects.get(zeus_id=zeus_id) + user.username = username + user.save() + return user + except users.models.CustomUser.DoesNotExist as e: return CustomUser.objects.create_user(zeus_id, username) - user.username = username - user.save() - return user def user_info(access_token, csrftoken):