0

I'm building an application using Django in which each customer will have their own domain name (subdomain in our domain, such as customer.example.com or their own domain, such as customer.com). Ignoring the problem of web server configuration, SSL (that'll be fun, thank god for Let's Encrypt), etc, in Django, how can I have users per domain?

The customers would be one type of user and the customer's users would be a different type. The same username should be useable in each of the served domains. Is there anything prebuilt that can help? If not, how do I modify the way auth happens in Django to limit it to the current domain.

My problem is not how to serve each of the domains/subdomains, but how to do authentication (registration, password reset, and all that), which means thisis not a duplicate of Using subdomains in django

I have looked into https://github.com/tomturner/django-tenants and https://github.com/bernardopires/django-tenant-schemas and for my use case, using PostgreSQL schemas to separate the data is not appropriate.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
  • @HåkenLid: that deals with how to serve in subdomains, there isn't a single mention of auth there. – Pablo Fernandez Oct 17 '18 at 14:29
  • @VaibhavVishal: my problem, this question, is about doing authentication per domain, which is not solved by django-subdomains. – Pablo Fernandez Oct 17 '18 at 14:32
  • I see. I don't have any suggestions on how to solve this, but I'm retracting the duplicate suggestion. – Håken Lid Oct 17 '18 at 14:35
  • What you want is a multi-tenant application, which is not trivial. But there are packages and other questions on SO that give you the general direction. Search for "multi-tenant django" – dirkgroten Oct 17 '18 at 14:37
  • Have a look at https://github.com/tomturner/django-tenants and https://github.com/bernardopires/django-tenant-schemas – Umair Mohammad Oct 17 '18 at 14:39
  • @dirkgroten: I'm familiar with the concept and implementation of multi-tenant. I've done it in Ruby and Rails various times, having forked the multi_tenant gem. It's my first time doing it in Django. – Pablo Fernandez Oct 17 '18 at 14:46
  • @MohammadUmair: I looked into those and I don't want to use PostgreSQL to separate tenants. It wouln't work very well for my use case. I'll add this to the question. – Pablo Fernandez Oct 17 '18 at 14:47
  • And how does [this](https://stackoverflow.com/questions/7194341/optimal-architecture-for-multitenant-application-on-django/7194998#7194998) and [this](https://stackoverflow.com/questions/2115911/handling-uniqueness-in-a-multi-tenant-django-setup) not help? – dirkgroten Oct 17 '18 at 14:51

1 Answers1

0

I would use the "Host" header in a custom context processor to know which is the active user domain, and a custom login handler (its very easy, just use auth.authenticate(**kwargs) and auth.login(request, user)).

But good luck, sounds like an interesting problem.

edit: not check_password but auth.authenticate(**kwargs) and auth.login(request, user)

dmoreno
  • 676
  • 6
  • 10