Reputation: 177
i am building a blog using Django, and i have implemented an analysis tool that allows me to plot details about my visitors (country_origin, device, browser, ...).
analysis.localhost:8000 is a subdomain of localhost , defined as a class based view with a custom Mixin SuperuserAccessRequired, that returns a 401 Unauthorized if the user is not staff (i am using django-hosts to handle subdomains & this is my first time working with subdomains.).
My issue:
if i am logged in on localhost:8000 and naviguate to analysis.localhost:8000 i get the 401 response.
You are seeing this as: AnonymousUser # generated by print(You are seeing this as: ', request.user) from SuperuserAccessRequired
Unauthorized: /
[25/Sep/2019 13:14:03] "GET / HTTP/1.1" 401 89
my humble assumption says that localhost:8000 and x.localhost:8000 are not sharing certain variables.
How can i fix this issue, i have read django-hosts documentation like 5 times already and i can't seem to find what i am missing
my code :
project/root/settings.py
...
ALLOWED_HOSTS = ['localhost']
ALLOWED_HOSTS += ['analysis.localhost', 'blog.localhost']
SITE_ID = 1
INSTALLED_APPS += [
# Django Apps
'blog',
'analysis',
]
INSTALLED_APPS += [
# Django Dependencies
'sass_processor',
'django_hosts',
]
MIDDLEWARE = [
'django_hosts.middleware.HostsRequestMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'analysis.middleware.TrackVisitorMiddleware',
'django_hosts.middleware.HostsResponseMiddleware',
]
ROOT_URLCONF = 'root.urls'
ROOT_HOSTCONF = 'root.hosts'
BLOG_URLCONF = 'blog.urls'
ANALYSIS_URLCONF = 'analysis.urls'
DEFAULT_HOST = 'www'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'analysis': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'analysis_db.sqlite3'),
}
}
...
project/root/hosts.py
host_patterns = patterns('',
host(r'localhost:8000', settings.ROOT_URLCONF, name='www'),
host(r'analysis.localhost:8000', settings.ANALYSIS_URLCONF, name='analysis'),
host(r'blog.localhost:8000', settings.BLOG_URLCONF, name='blog'),
)
Custom Mixin:
class SuperuserAccessRequired(AccessMixin):
"""Verify that the current user is staff."""
def dispatch(self, request, *args, **kwargs):
print('You are seeing this as: ', request.user)
if not request.user.is_staff:
return HttpResponse(status=401, content='You are not authorized to view this page.')
return super().dispatch(request, *args, **kwargs)
UPDATE
If i remove the port number from analysis.localhost i get redirected to the Apache default page ... weird. (just dismiss this update i forgot about /etc/hosts, man i am losing it)
UPDATE 2
Digging deeper into the matter it looks like i have to rewrite my SessionMiddleware.
Any help/guidance would be appreciated.
Upvotes: 1
Views: 360
Reputation: 5884
You need to set SESSION_COOKIE_DOMAIN for share cookie between subdomains.
SESSION_COOKIE_DOMAIN = '.localhost'
SESSION_COOKIE_NAME = 'sharesession'
See more info SESSION_COOKIE_DOMAIN
Upvotes: 0