Skip to content

Commit

Permalink
42 auth fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuffugurlu committed May 12, 2024
1 parent 413bcd4 commit a078bca
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
11 changes: 7 additions & 4 deletions API/Apps/Profile/api/api_42.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions API/Apps/Profile/migrations/0002_profile_is_42auth.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
2 changes: 1 addition & 1 deletion API/static/scripts/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions API/static/scripts/spa.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const routes = new Map([
<div class="register-input-wrapper">
<label for="password">password</label>
<input
type="text"
type="password"
class="register-input p-2"
placeholder="PASSWORD"
id="password"
Expand All @@ -118,7 +118,7 @@ const routes = new Map([
<div class="register-input-wrapper">
<label for="password">Re enter your password</label>
<input
type="text"
type="password"
class="register-input p-2"
id="password2"
required
Expand Down
4 changes: 2 additions & 2 deletions API/templates/auth/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ <h1 style="color: white">REGISTER</h1>
<div class="register-input-wrapper">
<label for="password">password</label>
<input
type="text"
type="password"
class="register-input p-2"
placeholder="PASSWORD"
id="password"
Expand All @@ -62,7 +62,7 @@ <h1 style="color: white">REGISTER</h1>
<div class="register-input-wrapper">
<label for="password">Re enter your password</label>
<input
type="text"
type="password"
class="register-input p-2"
id="password2"
required
Expand Down

0 comments on commit a078bca

Please sign in to comment.