bernardopires / django-tenant-schemas

Tenant support for Django using PostgreSQL schemas.
https://django-tenant-schemas.readthedocs.org/en/latest/
MIT License
1.46k stars 424 forks source link

How to automatically route tenants to their tenant subdomain #612

Closed 5starkarma closed 4 years ago

5starkarma commented 4 years ago

The application works very nicely but I am wondering about automatic routing tenants to their subdomain from the public schema. e.g. Tenant goes to www.example.com and tries to login, is their any solution in this app that routes them to their subdomain (www.tenant1.example.com)? I am sure this is a rather common question but for some reason I cannot find much on it.

In the docs I see Tenant View-Routing using PUBLIC_SCHEMA_URLCONF but I don't really understand exactly what this does. Could you help me understand?

I have also thought about using sperate projects for the main website and tenants but then I would still need to either somehow grab the IP address of the user and try to route them to the correct subdomain or to ask them which subdomain they want (which seems inefficient).

So what do people do to solve this issue? I am almost positive there is a simple solution I am not seeing or simply not understanding as these seems like it would be a very common issue.

All help is very much appreciated :)

erikvlm commented 4 years ago

Hey how did you solve it?

5starkarma commented 4 years ago

It all depends on how you want to structure your architecture. In my case it is fine to have tenants and users (auth) all in the shared schema which allows for authentication across all the domains and subdomains.

But wait! I didn't want to have users from one tenant logged in at another tenant! This removes the idea of a multi-tenant architecture (at least in my use-case)!

So I created a middleware that had a line that, if the user is authenticated, grabs the request.user.tenant as well as request.tenant and compares them. If they don't match? Redirect them to their tenant subdomain! It works like a charm.

erikvlm commented 4 years ago

Can you share the settings.py file and the signup/login view? I'm been scratching my head for days trying to solve it

5starkarma commented 4 years ago

Here you go..

The settings.py:

SHARED_APPS = [
    'django_tenants',

    'django.contrib.contenttypes',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # more apps
]

TENANT_APPS = [
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    # more apps
]

I am not going to share views because they are project specific. Users create a tenant then you redirect them to that tenant.

erikvlm commented 4 years ago

I’m having issues getting the user logged in. I can successfully create a tenant from maindomain.com and have the user redirected to tenant.maindomain.com ( and the user is in the correct schema) but I can’t figure out how to get the user -automatically logged in. I tried just doing a login(request, user) but it gives me a weird cookie error. Did you find a way around it

5starkarma commented 4 years ago

I didn't have that issue. Fwiw my Tenant registration view and my user registration view are separate. After a tenant is created and the user is redirected to that tenant is where I have them sign up as a user.

Probably worth it to post an issue specific to your problem. People won't see issues here.

erikvlm commented 4 years ago

Did you find a way to have the user logged in from main domain and be redirected to tenant as a logged in user?

5starkarma commented 4 years ago

So I created a middleware that had a line that, if the user is authenticated, grabs the request.user.tenant as well as request.tenant and compares them. If they don't match? Redirect them to their tenant subdomain! It works like a charm.

erikvlm commented 4 years ago

And I suppose you don’t want to share that either..?

peanutcolada commented 2 years ago

i'm stuck on the login part did anyone care to share if they found a workaround?