From a078bca22fe24a31e13f4a3a76c7371b7ad4b558 Mon Sep 17 00:00:00 2001 From: yugurlu Date: Sun, 12 May 2024 18:57:49 +0300 Subject: [PATCH] 42 auth fix --- API/Apps/Profile/api/api_42.py | 11 +++++++---- .../migrations/0002_profile_is_42auth.py | 17 +++++++++++++++++ API/static/scripts/register.js | 2 +- API/static/scripts/spa.js | 4 ++-- API/templates/auth/register.html | 4 ++-- 5 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 API/Apps/Profile/migrations/0002_profile_is_42auth.py diff --git a/API/Apps/Profile/api/api_42.py b/API/Apps/Profile/api/api_42.py index 7bcc57ed..2b5a6c55 100644 --- a/API/Apps/Profile/api/api_42.py +++ b/API/Apps/Profile/api/api_42.py @@ -1,9 +1,9 @@ import os +import random import requests from django.contrib.auth.models import User from rest_framework.response import Response - from Apps.Auth.serializers import RegisterWith42Serializer from Apps.Auth.utils import send_email from Apps.Profile.api.Serializers import UserSerializer @@ -28,14 +28,17 @@ def api_42(code): def login_with_42(username, email, image): - if not User.objects.filter(username=username).exists(): + if not User.objects.filter(email=email).exists(): + if User.objects.filter(username=username).exists(): + username = username + "_" + str(random.randint(1000, 9999)) serializer = RegisterWith42Serializer(data={"username": username, "email": email}) if not serializer.is_valid(): return Response(serializer.errors, status=400) serializer.save() - user = User.objects.get(username=username) - if not user.profile.profile_picture: + user = User.objects.get(email=email) + if user.profile.profile_picture == 'profile-pictures/default.svg': user.profile.save_image_from_url(image) + user.profile.save() if user: send_email(user) return Response(data={'user': UserSerializer(user).data}, status=200) diff --git a/API/Apps/Profile/migrations/0002_profile_is_42auth.py b/API/Apps/Profile/migrations/0002_profile_is_42auth.py new file mode 100644 index 00000000..fc52712b --- /dev/null +++ b/API/Apps/Profile/migrations/0002_profile_is_42auth.py @@ -0,0 +1,17 @@ +# Generated by Django 5.0.3 on 2024-05-12 12:35 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ('Profile', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='profile', + name='is_42auth', + field=models.BooleanField(default=False), + ), + ] diff --git a/API/static/scripts/register.js b/API/static/scripts/register.js index 39413485..a441f352 100644 --- a/API/static/scripts/register.js +++ b/API/static/scripts/register.js @@ -16,7 +16,7 @@ const registerSubmit = async (event) => { spinner.render(); button.disabled = true; try{ - const response = await fetch(`register/`, { + const response = await fetch(`${API_URL}/auth/register/`, { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/API/static/scripts/spa.js b/API/static/scripts/spa.js index 759cf8ef..d2a366a9 100644 --- a/API/static/scripts/spa.js +++ b/API/static/scripts/spa.js @@ -108,7 +108,7 @@ const routes = new Map([
REGISTER
REGISTER