Skip to content

Commit

Permalink
Merge pull request #163 from HE-Arc/mbu-151-backend-add-serializer-po…
Browse files Browse the repository at this point in the history
…ur-register

Mbu 151 backend add serializer pour register
  • Loading branch information
maelys-buhler authored Apr 21, 2024
2 parents 0de8e93 + 11ef6ba commit e5bd6d7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
12 changes: 12 additions & 0 deletions api/masteriqapp/serializers/UserSerializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.contrib.auth import get_user_model
from rest_framework import serializers


class UserSerializer(serializers.ModelSerializer):

class Meta:
model = get_user_model()
fields = ['username', 'password']

def create(self, validated_data):
return get_user_model().objects.create_user(**validated_data)
19 changes: 11 additions & 8 deletions api/masteriqapp/views/AuthenticationView.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.conf import settings

import masteriqapp.models.IQ
from masteriqapp.serializers.UserSerializer import UserSerializer

masteriq = apps.get_app_config("masteriqapp")

Expand All @@ -23,18 +24,20 @@ class AuthenticationView(viewsets.ViewSet, ObtainAuthToken):
@action(detail=False, methods=['POST'], permission_classes=[AllowAny])
def register(self, request):
username = request.data.get('username')
password = request.data.get('password')
if not get_user_model().objects.filter(username=username).exists():
user = get_user_model().objects.create_user(username=username, password=password)
self.create_iq_objects_for_new_user(user)
return Response({'message': 'Register successful'}, status=status.HTTP_201_CREATED)
else:
return Response({'message': 'Username already exists'}, status=status.HTTP_400_BAD_REQUEST)
user_serializer = UserSerializer(data=request.data)
if user_serializer.is_valid():
user = user_serializer.save()
self.create_iq_objects_for_new_user(user)
return Response({'message': 'Register successful'}, status=status.HTTP_201_CREATED)
return Response(data=user_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
return Response({'message': 'Username already exists'}, status=status.HTTP_400_BAD_REQUEST)

@action(detail=False, methods=['POST'], permission_classes=[AllowAny])
def token(self, request):
serializer = self.serializer_class(data=request.data,
context={'request': request})
print(self.serializer_class)
if serializer.is_valid():
token, created = Token.objects.get_or_create(user=serializer.validated_data['user'])
if not created:
Expand All @@ -50,8 +53,8 @@ def token(self, request):
return Response({
'token': token.key,
'expires': expiring_date
})
return Response(serializer.errors, status=status.HTTP_401_UNAUTHORIZED)
}, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def create_iq_objects_for_new_user(self, user):
categories = self.category_model.objects.all()
Expand Down
1 change: 0 additions & 1 deletion frontend/src/api_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ export default
if (response.status === 200) {
// set cookie
let date = new Date(response.data.expires).toUTCString()
console.log(date)
document.cookie = `access_token=Token ${response.data.token};secure;expires=${date};`; // TODO adding expires date from backend

// set axios header
Expand Down

0 comments on commit e5bd6d7

Please sign in to comment.