Skip to content

Commit

Permalink
Merge pull request #442 from seanmorley15/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
seanmorley15 authored Jan 14, 2025
2 parents 609f68b + ef44836 commit 187f4c0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
14 changes: 9 additions & 5 deletions backend/server/adventures/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ class OverrideHostMiddleware:
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request: HttpRequest):
# Override the host with the PUBLIC_URL environment variable
def __call__(self, request):
public_url = os.getenv('PUBLIC_URL', None)
if public_url:
# Split the public URL to extract the host and port (if available)
host = public_url.split("//")[-1].split("/")[0]
request.META['HTTP_HOST'] = host # Override the HTTP_HOST header
# Extract host and scheme
scheme, host = public_url.split("://")
request.META['HTTP_HOST'] = host
request.META['wsgi.url_scheme'] = scheme

# Set X-Forwarded-Proto for Django
request.META['HTTP_X_FORWARDED_PROTO'] = scheme

response = self.get_response(request)
return response
2 changes: 2 additions & 0 deletions backend/server/main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')


BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_ROOT = BASE_DIR / "staticfiles"
Expand Down
32 changes: 28 additions & 4 deletions frontend/src/routes/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,37 @@ export const actions: Actions = {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
Cookie: `sessionid=${sessionId}; csrftoken=${csrfToken}`,
'X-CSRFToken': csrfToken
'X-CSRFToken': csrfToken, // Ensure CSRF token is in header
Referer: event.url.origin, // Include Referer header
Cookie: `sessionid=${sessionId}; csrftoken=${csrfToken}`
},
credentials: 'include'
});
if (res.status == 401) {
event.cookies.delete('sessionid', { path: '/', secure: event.url.protocol === 'https:' });

// Determine the proper cookie domain
const hostname = event.url.hostname;
const domainParts = hostname.split('.');
let cookieDomain: string | undefined = undefined;

if (domainParts.length > 2) {
// For subdomains like app.mydomain.com -> .mydomain.com
cookieDomain = '.' + domainParts.slice(-2).join('.');
} else if (domainParts.length === 2) {
// For root domains like mydomain.com -> .mydomain.com
cookieDomain = '.' + hostname;
} else {
// For localhost or single-part domains (e.g., "localhost")
cookieDomain = undefined; // Do not set the domain
}

// Delete the session cookie
event.cookies.delete('sessionid', {
path: '/',
secure: event.url.protocol === 'https:',
domain: cookieDomain
});

if (res.status === 401) {
return redirect(302, '/login');
} else {
return redirect(302, '/');
Expand Down

0 comments on commit 187f4c0

Please sign in to comment.