53 lines
2.1 KiB
Python
53 lines
2.1 KiB
Python
from rest_framework import status
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
from django.contrib.auth import authenticate
|
|
from rest_framework_simplejwt.tokens import RefreshToken
|
|
from rest_framework import generics
|
|
from django.contrib.auth.models import User
|
|
|
|
from rest_framework.decorators import api_view, permission_classes
|
|
from rest_framework.permissions import AllowAny
|
|
from userProfile.serializers import ProfileSerializer
|
|
from loginApi.serializers import UserSerializer
|
|
|
|
@api_view(['POST'])
|
|
@permission_classes([AllowAny])
|
|
def login(request):
|
|
def post(self, request):
|
|
username = request.data.get('username')
|
|
password = request.data.get('password')
|
|
user = authenticate(username=username, password=password)
|
|
if user is not None:
|
|
refresh = RefreshToken.for_user(user)
|
|
return Response({
|
|
'refresh': str(refresh),
|
|
'access': str(refresh.access_token),
|
|
})
|
|
return Response({'error': 'Invalid Credentials'}, status=status.HTTP_401_UNAUTHORIZED)
|
|
|
|
|
|
|
|
|
|
@api_view(['POST'])
|
|
@permission_classes([AllowAny])
|
|
def register(request):
|
|
user_serializer = UserSerializer(data=request.data)
|
|
if user_serializer.is_valid():
|
|
user = user_serializer.save()
|
|
profile_data = request.data.get('profile', {})
|
|
profile_data['user'] = user.id # Ensure the user ID is included in the profile data
|
|
|
|
profile_serializer = ProfileSerializer(data=profile_data)
|
|
if profile_serializer.is_valid():
|
|
profile_serializer.save()
|
|
refresh = RefreshToken.for_user(user)
|
|
return Response({
|
|
'user': user_serializer.data,
|
|
'profile': profile_serializer.data,
|
|
'refresh': str(refresh),
|
|
'access': str(refresh.access_token),
|
|
}, status=status.HTTP_201_CREATED)
|
|
else:
|
|
return Response(profile_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
return Response(user_serializer.errors, status=status.HTTP_400_BAD_REQUEST) |