From 50dc0424a9caf58a3a8b123a0245ff9dddb398ed Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Sun, 1 Dec 2024 09:52:04 -0500 Subject: [PATCH] Refactor user serializers, update Docker configurations, and remove unused Nginx files --- backend/server/users/serializers.py | 16 +++------------- docker-compose.yml | 12 ++++++------ frontend/src/routes/+page.server.ts | 22 ++++++++++++---------- proxy/Dockerfile.nginx | 4 ---- proxy/nginx.conf | 8 -------- 5 files changed, 21 insertions(+), 41 deletions(-) delete mode 100644 proxy/Dockerfile.nginx delete mode 100644 proxy/nginx.conf diff --git a/backend/server/users/serializers.py b/backend/server/users/serializers.py index 9fb6a59..b85608c 100644 --- a/backend/server/users/serializers.py +++ b/backend/server/users/serializers.py @@ -2,23 +2,13 @@ from rest_framework import serializers from django.contrib.auth import get_user_model from adventures.models import Collection -from dj_rest_auth.serializers import PasswordResetSerializer User = get_user_model() from django.contrib.auth import get_user_model -from django.core.exceptions import ValidationError as DjangoValidationError from django.utils.translation import gettext_lazy as _ from rest_framework import serializers -try: - from allauth.account import app_settings as allauth_account_settings - from allauth.account.adapter import get_adapter - from allauth.account.utils import setup_user_email - from allauth.socialaccount.models import EmailAddress - from allauth.utils import get_username_max_length -except ImportError: - raise ImportError('allauth needs to be added to INSTALLED_APPS.') class ChangeEmailSerializer(serializers.Serializer): new_email = serializers.EmailField(required=True) @@ -37,7 +27,7 @@ from django.contrib.auth import get_user_model from django.utils.translation import gettext_lazy as _ from rest_framework import serializers UserModel = get_user_model() -from dj_rest_auth.serializers import UserDetailsSerializer +# from dj_rest_auth.serializers import UserDetailsSerializer from .models import CustomUser from rest_framework import serializers @@ -77,9 +67,9 @@ class UserDetailsSerializer(serializers.ModelSerializer): if hasattr(UserModel, 'public_profile'): extra_fields.append('public_profile') - class Meta(UserDetailsSerializer.Meta): + class Meta: model = CustomUser - fields = UserDetailsSerializer.Meta.fields + ('profile_pic', 'uuid', 'public_profile') + fields = ('profile_pic', 'uuid', 'public_profile', 'email', 'date_joined', 'is_staff', 'is_superuser', 'is_active', 'pk') model = UserModel fields = ('pk', *extra_fields) diff --git a/docker-compose.yml b/docker-compose.yml index 9ffdfdc..3f84851 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,7 @@ services: web: - #build: ./frontend/ - image: ghcr.io/seanmorley15/adventurelog-frontend:latest + build: ./frontend/ + #image: ghcr.io/seanmorley15/adventurelog-frontend:latest container_name: adventurelog-frontend restart: unless-stopped environment: @@ -14,7 +14,7 @@ services: - server db: - image: postgis/postgis:15-3.3 + image: postgis/postgis:16-3.4 container_name: adventurelog-db restart: unless-stopped environment: @@ -25,8 +25,8 @@ services: - postgres_data:/var/lib/postgresql/data/ server: - #build: ./backend/ - image: ghcr.io/seanmorley15/adventurelog-backend:latest + build: ./backend/ + #image: ghcr.io/seanmorley15/adventurelog-backend:latest container_name: adventurelog-backend restart: unless-stopped environment: @@ -39,7 +39,7 @@ services: - DJANGO_ADMIN_PASSWORD=admin - DJANGO_ADMIN_EMAIL=admin@example.com - PUBLIC_URL='http://localhost:8016' # Match the outward port, used for the creation of image urls - - CSRF_TRUSTED_ORIGINS=http://localhost:8016 # Comma separated list of trusted origins for CSRF + - CSRF_TRUSTED_ORIGINS=http://localhost:8016,http://localhost:8015 # Comma separated list of trusted origins for CSRF - DEBUG=False - FRONTEND_URL='http://localhost:8015' # Used for email generation. This should be the url of the frontend ports: diff --git a/frontend/src/routes/+page.server.ts b/frontend/src/routes/+page.server.ts index c45b004..0b80801 100644 --- a/frontend/src/routes/+page.server.ts +++ b/frontend/src/routes/+page.server.ts @@ -1,6 +1,7 @@ const PUBLIC_SERVER_URL = process.env['PUBLIC_SERVER_URL']; import { redirect, type Actions } from '@sveltejs/kit'; import { themes } from '$lib'; +import { fetchCSRFToken } from '$lib/index.server'; const serverEndpoint = PUBLIC_SERVER_URL || 'http://localhost:8000'; @@ -16,23 +17,24 @@ export const actions: Actions = { }); } }, - logout: async ({ cookies }: { cookies: any }) => { - const cookie = cookies.get('auth') || null; + logout: async (event) => { + let sessionId = event.cookies.get('sessionid'); + let csrfToken = await fetchCSRFToken(); - if (!cookie) { + if (!sessionId) { return; } - const res = await fetch(`${serverEndpoint}/auth/logout/`, { - method: 'POST', + const res = await fetch(`${serverEndpoint}/_allauth/browser/v1/auth/session`, { + method: 'DELETE', headers: { 'Content-Type': 'application/json', - Cookie: cookies.get('auth') - } + Cookie: `sessionid=${sessionId}; csrftoken=${csrfToken}`, + 'X-CSRFToken': csrfToken + }, + credentials: 'include' }); - if (res.ok) { - cookies.delete('auth', { path: '/', secure: false }); - cookies.delete('refresh', { path: '/', secure: false }); + if (res.status == 401) { return redirect(302, '/login'); } else { return redirect(302, '/'); diff --git a/proxy/Dockerfile.nginx b/proxy/Dockerfile.nginx deleted file mode 100644 index 4c49d2e..0000000 --- a/proxy/Dockerfile.nginx +++ /dev/null @@ -1,4 +0,0 @@ -FROM nginx:alpine - -RUN rm /etc/nginx/conf.d/default.conf -COPY nginx.conf /etc/nginx/conf.d \ No newline at end of file diff --git a/proxy/nginx.conf b/proxy/nginx.conf deleted file mode 100644 index 67f5f0d..0000000 --- a/proxy/nginx.conf +++ /dev/null @@ -1,8 +0,0 @@ -server { - listen 80; - server_name localhost; - - location /media/ { - alias /app/media/; - } -} \ No newline at end of file