diff --git a/backend/apps/authentication/__init__.py b/backend/apps/authentication/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/apps/authentication/admin.py b/backend/apps/authentication/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/backend/apps/authentication/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/backend/apps/authentication/apps.py b/backend/apps/authentication/apps.py new file mode 100644 index 0000000..d87fe5a --- /dev/null +++ b/backend/apps/authentication/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class AuthenticationConfig(AppConfig): + name = 'apps.authentication' diff --git a/backend/apps/authentication/migrations/__init__.py b/backend/apps/authentication/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/apps/authentication/models.py b/backend/apps/authentication/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/backend/apps/authentication/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/backend/apps/authentication/tests.py b/backend/apps/authentication/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/backend/apps/authentication/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/backend/apps/authentication/urls.py b/backend/apps/authentication/urls.py new file mode 100644 index 0000000..532b54c --- /dev/null +++ b/backend/apps/authentication/urls.py @@ -0,0 +1,14 @@ +from django.urls import include, path +from rest_framework_simplejwt.views import ( + TokenObtainPairView, + TokenRefreshView, + TokenVerifyView +) + +urlpatterns = [ + path('login/', TokenObtainPairView.as_view(), name='token_obtain_pair'), + #path('signup/', ), + + path('refresh/', TokenRefreshView.as_view(), name='token_refresh'), + path('verify/', TokenVerifyView.as_view(), name='token_verify'), +] \ No newline at end of file diff --git a/backend/apps/authentication/views.py b/backend/apps/authentication/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/backend/apps/authentication/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/backend/apps/shelves/serializers.py b/backend/apps/shelves/serializers.py index 1ee777e..75be0ce 100644 --- a/backend/apps/shelves/serializers.py +++ b/backend/apps/shelves/serializers.py @@ -5,14 +5,14 @@ from apps.user.serializers import UserSerializer from apps.user.models import User class ShelfSerializer(serializers.ModelSerializer): - user = UserSerializer(read_only=True) - class Meta: model = Shelf - fields = ["id", "name", "user"] + fields = ["id", "name"] read_only_fields = ["id"] + def create(self, validated_data): - # Pobieramy user_id z tokena + request = self.context["request"] + validated_data["user"] = request.user return super().create(validated_data) \ No newline at end of file diff --git a/backend/apps/shelves/views.py b/backend/apps/shelves/views.py index 8c61f4c..4efacc5 100644 --- a/backend/apps/shelves/views.py +++ b/backend/apps/shelves/views.py @@ -3,7 +3,17 @@ from django.shortcuts import render from apps.shelves.models import Shelf from apps.shelves.serializers import ShelfSerializer from rest_framework import viewsets +from rest_framework.permissions import IsAuthenticated +from rest_framework_simplejwt.authentication import JWTAuthentication class ShelvesViewSet(viewsets.ModelViewSet): queryset = Shelf.objects.select_related("user") - serializer_class = ShelfSerializer \ No newline at end of file + serializer_class = ShelfSerializer + + authentication_classes = [JWTAuthentication] + permission_classes = [IsAuthenticated] + + def get_queryset(self): + return Shelf.objects.filter(user_id=self.request.user.pk) + + diff --git a/backend/apps/urls.py b/backend/apps/urls.py index 40fbfd4..90cce89 100644 --- a/backend/apps/urls.py +++ b/backend/apps/urls.py @@ -1,5 +1,11 @@ +from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView from django.urls import include, path urlpatterns = [ - path("shelves", include("apps.shelves.urls")) + path("auth", include("apps.authentication.urls")), + path("shelves", include("apps.shelves.urls")), + + path('schema/', SpectacularAPIView.as_view(), name='schema'), + path('swagger/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'), + ] diff --git a/backend/apps/user/admin.py b/backend/apps/user/admin.py index 8c38f3f..95879ad 100644 --- a/backend/apps/user/admin.py +++ b/backend/apps/user/admin.py @@ -1,3 +1,7 @@ from django.contrib import admin +from apps.user.models import User -# Register your models here. +class UserAdmin(admin.ModelAdmin): + pass + +admin.site.register(User, UserAdmin) \ No newline at end of file diff --git a/backend/config/django/base.py b/backend/config/django/base.py index 3729f0f..557d2a9 100644 --- a/backend/config/django/base.py +++ b/backend/config/django/base.py @@ -16,6 +16,7 @@ ALLOWED_HOSTS = ["*"] # Application definition INSTALLED_APPS = [ + "apps.authentication.apps.AuthenticationConfig", "apps.user.apps.UserConfig", "apps.shelves.apps.ShelvesConfig", "django.contrib.admin", @@ -26,6 +27,8 @@ INSTALLED_APPS = [ "django.contrib.staticfiles", "rest_framework", "django_filters", + "rest_framework_simplejwt", + "drf_spectacular", ] MIDDLEWARE = [ @@ -89,6 +92,26 @@ AUTH_PASSWORD_VALIDATORS = [ AUTH_USER_MODEL = "user.User" +REST_FRAMEWORK = { + 'DEFAULT_RENDERER_CLASSES': [ + 'rest_framework.renderers.JSONRenderer', + ], + 'DEFAULT_PARSER_CLASSES': [ + 'rest_framework.parsers.JSONParser', + ], + 'DEFAULT_AUTHENTICATION_CLASSES': [ + 'rest_framework_simplejwt.authentication.JWTAuthentication', + ], + 'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema', +} + +SPECTACULAR_SETTINGS = { + 'TITLE': 'Library API', + 'DESCRIPTION': 'Library project', + 'VERSION': '1.0.0', + 'SERVE_INCLUDE_SCHEMA': False, +} + # Internationalization # https://docs.djangoproject.com/en/6.0/topics/i18n/ diff --git a/backend/requirements.txt b/backend/requirements.txt index b08c5cb..0a70b1f 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -2,4 +2,6 @@ django==6.0.3 django-environ==0.13.0 djangorestframework==3.16.1 django-filter==25.2 +djangorestframework-simplejwt==5.5.1 +drf-spectacular==0.29.0 pillow==12.1.1 \ No newline at end of file