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

Redirect tenant to subdomain after user registration? #615

Open erikvlm opened 4 years ago

erikvlm commented 4 years ago

If a user goes to maindomain.com and registers, I can successfully create a schema for them and redirect them to tenant.maindomain.com. The issue is that I can't seem to find a way to log the user in. (The user does exist in the correct schema). I'm getting this error:

The request's session was deleted before the request completed. The user may have logged out in a concurrent request, for example

when trying to log them in right away after creating a tenant + logging them in.

Below is the user registration logic I'm using

class SignupView(View):
    def post(self, request, *args, **kwargs):
        form = RegistrationForm(request.POST)
        if form.is_valid():

            instance = form.save(commit=False)
            schema_name = slugify(instance.company).replace("-", "")
            if schema_exists(schema_name):
                raise ValidationError("A schema with name %s already exists in the database" % schema_name)
            else:
                company = "".join(name for name in instance.company if name.isalnum())  # removes special characters
                tenant = Client(domain_url=company + ".my-domain.com", schema_name=company, name=company, paid_until="2019-05-10", on_trial=False)
                tenant.save()

                with schema_context(tenant.schema_name):
                    instance.save()

                    # login(request, instance) # if enabled, this is what gives that weird session error 
                    redirect = 'http://' + company + '.my-domain.com:8000/accounts/login'
                    return HttpResponseRedirect(redirect)
        return render(request, "accounts/signup.html", {"form": form})

Any ideas?

damey2011 commented 4 years ago

I know this question is long-standing already. The issue is that the session attached to the request object passed to the django login function does not exist on the tenant schema which you are executing the login in its context. You might want to consider logging in with a token that you sort of pass into the URL. I think there's a library that does something similar which is [https://github.com/aaugustin/django-sesame] or you could implement it yourself. Should not be too hard. Let me know if you get the concept.

naranjaapps commented 4 years ago
       Hola, yo lo hice de esta manera, espero te ayude.

        subdomain = form.cleaned_data["subdomain"]
        host = request.META.get('HTTP_HOST', '')
        scheme_url = request.is_secure() and "https" or "http"
        url = f"{scheme_url}://{subdomain}.{host}"

        return HttpResponseRedirect(url)
naranjaapps commented 2 years ago

Yes of course, this only redirects to the subdomain and the user has to enter their credentials. It is for security reasons, but it could be redirected directly to the subdomain with the user already logged in.

[image: Naranja Apps S.A.C.]

Juan Chávez Reátegui Gerente General | Naranja Apps S.A.C. mobile: (+51) 949-236969 <(+51)+949-236969> site: www.naranja.pe email: @.***

El mar, 22 mar 2022 a las 17:35, Joseph Jeong @.***>) escribió:

   Hola, yo lo hice de esta manera, espero te ayude.

    subdomain = form.cleaned_data["subdomain"]
    host = request.META.get('HTTP_HOST', '')
    scheme_url = request.is_secure() and "https" or "http"
    url = f"{scheme_url}://{subdomain}.{host}"

    return HttpResponseRedirect(url)

seems like this doesn't really logs in the user

— Reply to this email directly, view it on GitHub https://github.com/bernardopires/django-tenant-schemas/issues/615#issuecomment-1075717931, or unsubscribe https://github.com/notifications/unsubscribe-auth/APJE6EH7DFEHYDOAA6WUONLVBJDJRANCNFSM4J6RLK2A . You are receiving this because you commented.Message ID: @.***>

naranjaapps commented 2 years ago

Yo lo hice de otra manera y me funciona bien, existen otras formas de usar los usuarios en los tenants, depende mucho de lo que deseas hacer en tu web,

[image: Naranja Apps S.A.C.]

Juan Chávez Reátegui Gerente General | Naranja Apps S.A.C. mobile: (+51) 949-236969 <(+51)+949-236969> site: www.naranja.pe email: @.***

El mar, 22 mar 2022 a las 17:29, Joseph Jeong @.***>) escribió:

any updates on this? I have a similar issue and tried using the django login() function but got the same error... is there anyway to do this without having a centralized user table for all tenants?

— Reply to this email directly, view it on GitHub https://github.com/bernardopires/django-tenant-schemas/issues/615#issuecomment-1075710470, or unsubscribe https://github.com/notifications/unsubscribe-auth/APJE6EHII5N3HTRDOBSRDYDVBJCTHANCNFSM4J6RLK2A . You are receiving this because you commented.Message ID: @.***>