Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mbu 151 backend add serializer pour register #163

Merged
merged 2 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading