Skip to content

Commit

Permalink
[IMP] user auth + settings + urls
Browse files Browse the repository at this point in the history
  • Loading branch information
C2dricLeroy committed Sep 9, 2024
1 parent ea8da69 commit 72eaac9
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 31 deletions.
2 changes: 1 addition & 1 deletion vocab_dictionnary/app_params/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.0.4 on 2024-09-08 19:19
# Generated by Django 5.0.4 on 2024-09-09 19:58

import django.db.models.deletion
from django.conf import settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['username', 'email', 'password']
extra_kwargs = {'password': {'write_only': True}}
fields = ['username', 'password', 'email', 'first_name', 'last_name']

def create(self, validated_data):
user = User.objects.create_user(
username=validated_data['username'],
password=validated_data['password'],
email=validated_data['email'],
password=validated_data['password']
first_name=validated_data.get('first_name', ''),
last_name=validated_data.get('last_name', '')
)
return user
return user
1 change: 1 addition & 0 deletions vocab_dictionnary/authentication/serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .UserSerializer import UserSerializer
Empty file.
11 changes: 6 additions & 5 deletions vocab_dictionnary/authentication/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.urls import path
from .views.SignUp import SignUpView
from rest_framework.routers import DefaultRouter
from .views import UserViewSet

urlpatterns = [
path('signup/', SignUpView.as_view(), name='signup'),
]
router = DefaultRouter()
router.register(r'register', UserViewSet, basename='user')

urlpatterns = router.urls
17 changes: 0 additions & 17 deletions vocab_dictionnary/authentication/views/SignUp.py

This file was deleted.

23 changes: 23 additions & 0 deletions vocab_dictionnary/authentication/views/UserViewSet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from rest_framework import viewsets, status
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from django.contrib.auth.models import User
from ..serializers import UserSerializer

class UserViewSet(viewsets.ModelViewSet):
"""
A viewset that provides `create` for user registration.
"""
queryset = User.objects.all()
serializer_class = UserSerializer
permission_classes = [AllowAny]

def create(self, request, *args, **kwargs):
"""
Custom create method for user registration.
"""
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
1 change: 1 addition & 0 deletions vocab_dictionnary/authentication/views/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .UserViewSet import UserViewSet
2 changes: 1 addition & 1 deletion vocab_dictionnary/dictionary/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.0.4 on 2024-09-08 19:19
# Generated by Django 5.0.4 on 2024-09-09 19:53

import django.db.models.deletion
from django.db import migrations, models
Expand Down
4 changes: 3 additions & 1 deletion vocab_dictionnary/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ django-filter
psycopg2-binary
django-rest-swagger
Pillow
djangorestframework-simplejwt
djangorestframework-simplejwt
social-auth-app-django
python-dotenv
20 changes: 19 additions & 1 deletion vocab_dictionnary/vocab_dictionnary/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
from pathlib import Path
import os
from dotenv import load_dotenv

load_dotenv()

# Google Authentication

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = os.getenv('SOCIAL_AUTH_GOOGLE_OAUTH2_KEY')
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = os.getenv('SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET')
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'
SITE_ID = 1
SOCIAL_AUTH_URL_NAMESPACE = 'social'


BASE_DIR = Path(__file__).resolve().parent.parent

Expand All @@ -20,6 +34,9 @@
'dictionary',
'app_params',
'rest_framework.authtoken',
'social_django',
'django.contrib.sites',
'authentication',
]

MIDDLEWARE = [
Expand All @@ -30,6 +47,7 @@
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'social_django.middleware.SocialAuthExceptionMiddleware',
]

ROOT_URLCONF = 'vocab_dictionnary.urls'
Expand Down Expand Up @@ -98,6 +116,6 @@
'rest_framework.permissions.IsAuthenticated',
],
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication', # Authentification par jetons
'rest_framework_simplejwt.authentication.JWTAuthentication',
],
}
11 changes: 10 additions & 1 deletion vocab_dictionnary/vocab_dictionnary/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
from django.contrib import admin
from django.urls import path, include
from rest_framework.authtoken import views
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
)


urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('dictionary.urls')),
path('api/auth/', views.obtain_auth_token, name='api_token_auth'),
path('api/auth/', include('authentication.urls')),
path('api/auth/login/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('auth/', include('social_django.urls', namespace='social')),
path('accounts/', include('django.contrib.auth.urls')),
]

0 comments on commit 72eaac9

Please sign in to comment.