mise en place du système autapp
This commit is contained in:
@@ -56,6 +56,7 @@ INSTALLED_APPS = [
|
||||
'django.contrib.admin',
|
||||
'carnet_rouge.apps.CarnetRougeConfig',
|
||||
'django_summernote',
|
||||
'authapp',
|
||||
]
|
||||
|
||||
INSTALLED_APPS += ( 'apilog.apps.ApilogConfig',)
|
||||
@@ -70,6 +71,14 @@ INSTALLED_APPS += ('rest_framework', 'rest_framework.authtoken',)
|
||||
),
|
||||
# Autres paramètres de configuration...
|
||||
}'''
|
||||
REST_FRAMEWORK = {
|
||||
'DEFAULT_AUTHENTICATION_CLASSES': [
|
||||
'rest_framework.authentication.TokenAuthentication',
|
||||
],
|
||||
'DEFAULT_PERMISSION_CLASSES': [
|
||||
'rest_framework.permissions.IsAuthenticated',
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
LANGUAGE_CODE = 'fr-CH'
|
||||
|
@@ -34,6 +34,7 @@ urlpatterns = [
|
||||
path('summernote/', include('django_summernote.urls')),
|
||||
path('editor/', include('django_summernote.urls')),
|
||||
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
|
||||
path('api/auth/', include('authapp.urls')),
|
||||
|
||||
]
|
||||
|
||||
|
0
authapp/__init__.py
Normal file
0
authapp/__init__.py
Normal file
3
authapp/admin.py
Normal file
3
authapp/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
6
authapp/apps.py
Normal file
6
authapp/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AuthappConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'authapp'
|
0
authapp/models.py
Normal file
0
authapp/models.py
Normal file
14
authapp/serializers.py
Normal file
14
authapp/serializers.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from rest_framework import serializers
|
||||
from django.contrib.auth.models import User
|
||||
from rest_framework.authtoken.models import Token
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ('id', 'username', 'password')
|
||||
extra_kwargs = {'password': {'write_only': True}}
|
||||
|
||||
def create(self, validated_data):
|
||||
user = User.objects.create_user(**validated_data)
|
||||
Token.objects.create(user=user)
|
||||
return user
|
3
authapp/tests.py
Normal file
3
authapp/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
8
authapp/urls.py
Normal file
8
authapp/urls.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from django.urls import path
|
||||
from .views import CustomAuthToken, RegenerateTokenView, VerifyTokenView
|
||||
|
||||
urlpatterns = [
|
||||
path('login/', CustomAuthToken.as_view(), name='login'),
|
||||
path('regenerate-token/', RegenerateTokenView.as_view(), name='regenerate-token'),
|
||||
path('verify-token/', VerifyTokenView.as_view(), name='verify-token'),
|
||||
]
|
36
authapp/views.py
Normal file
36
authapp/views.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework import generics
|
||||
from rest_framework.authtoken.views import ObtainAuthToken
|
||||
from rest_framework.authtoken.models import Token
|
||||
from rest_framework.response import Response
|
||||
from .serializers import UserSerializer
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework import status
|
||||
|
||||
class CustomAuthToken(ObtainAuthToken):
|
||||
def post(self, request, *args, **kwargs):
|
||||
response = super().post(request, *args, **kwargs)
|
||||
token = Token.objects.get(key=response.data['token'])
|
||||
return Response({'token': token.key, 'user_id': token.user_id})
|
||||
|
||||
class RegenerateTokenView(APIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def post(self, request):
|
||||
user = request.user
|
||||
# Supprimer l'ancien token
|
||||
Token.objects.filter(user=user).delete()
|
||||
# Générer un nouveau token
|
||||
token = Token.objects.create(user=user)
|
||||
return Response({'token': token.key}, status=status.HTTP_201_CREATED)
|
||||
|
||||
class VerifyTokenView(APIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get(self, request):
|
||||
user = request.user
|
||||
return Response({
|
||||
'user_id': user.id,
|
||||
'username': user.username,
|
||||
'email': user.email
|
||||
}, status=status.HTTP_200_OK)
|
BIN
db.sqlite3
BIN
db.sqlite3
Binary file not shown.
Reference in New Issue
Block a user