Skip to content
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
Binary file modified apps/tasks/__pycache__/models.cpython-312.pyc
Binary file not shown.
Binary file added apps/tasks/__pycache__/serializers.cpython-312.pyc
Binary file not shown.
Binary file added apps/tasks/__pycache__/urls.cpython-312.pyc
Binary file not shown.
Binary file added apps/tasks/__pycache__/views.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified apps/users/__pycache__/models.cpython-312.pyc
Binary file not shown.
Binary file modified apps/users/__pycache__/urls.cpython-312.pyc
Binary file not shown.
Binary file modified apps/users/__pycache__/views.cpython-312.pyc
Binary file not shown.
11 changes: 4 additions & 7 deletions apps/users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
TokenObtainPairView,
TokenRefreshView,
)
from apps.users.views import protected_view
from apps.users.views import ProtectedView, RegisterView


urlpatterns = [
path('register/', RegisterView.as_view(), name='register'),
path('login/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('protected/', protected_view, name='protected'),
# path('protected/', protected_view, name='protected'),
path('protected/', ProtectedView.as_view(), name='protected'),
]







44 changes: 39 additions & 5 deletions apps/users/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
from rest_framework.response import Response
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.views import APIView
from rest_framework.views import status
from django.contrib.auth.models import User
from rest_framework_simplejwt.tokens import RefreshToken


@api_view(['GET'])
@permission_classes([IsAuthenticated])
def protected_view(request):
return Response({
# @api_view(['GET'])
# @permission_classes([IsAuthenticated])
# def protected_view(request):
# return Response({
# 'message': f"Hello, {request.user.username}, you are authenticated."
# })

class ProtectedView(APIView):
permission_classes = [IsAuthenticated]

def get(self, request):
return Response({
'message': f"Hello, {request.user.username}, you are authenticated."
})


class RegisterView(APIView):
permission_classes = [AllowAny]

def post(self, request):
username = request.data.get('username')
password = request.data.get('password')

if not username or not password:
return Response({"error": "Username and password are required"}, status=status.HTTP_400_BAD_REQUEST)

if User.objects.filter(username=username).exists():
return Response({"error": "Username already exists"}, status=status.HTTP_400_BAD_REQUEST)

user = User.objects.create_user(username=username, password=password)
refresh = RefreshToken.for_user(user)
return Response({
'refresh': str(refresh),
'access': str(refresh.access_token)
}, status=status.HTTP_201_CREATED)


Binary file modified todo/__pycache__/urls.cpython-312.pyc
Binary file not shown.