Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't figure out how to access tenant based on domain #259

Open
DavidGeismarLtd opened this issue Mar 30, 2021 · 4 comments
Open

Can't figure out how to access tenant based on domain #259

DavidGeismarLtd opened this issue Mar 30, 2021 · 4 comments

Comments

@DavidGeismarLtd
Copy link

I am using acts_as_tenant to build a multitenancy rails app that deploys customizable websites. In my application controller I have :

class ApplicationController < ActionController::Base
  set_current_tenant_by_subdomain_or_domain(:account, :subdomain, :domain)

Let's say my app is under platformdomain.io. When I create a new app, I can set a subdomain for it (let's say "me") and the new website will be accessible under me.platformdomain.io and this works perfectly fine.
However I would also like my users to be able to deploy their app on their own custom domain like customdomain.io or shop.customdomain.io.

  • I have then setup a new field on my tenant table (account) called domain. For testing purpose I bought a random domain (mycustomdomain.io) and added it on heroku shop.mycustomdomain.io, heroku then gives you a DNS target : randomDnsTarget and I set up a CNAME to point the shop subdomain for mycustomdomain to randomDnasTarget.
  • I then created an account with a subdomain of shop and a domain of mycustomdomain.io.

However when I try then to access the custom app at shop.mycustomdomain.io, I can see in the logs that my app is trying to load based on the subdomain shop.mycustomdomain:

Account Load (4.8ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."subdomain" = $1 LIMIT $2 [["subdomain", "shop.mycustomdomain"], ["LIMIT", 1]]

This can't work, as I guess the app should be trying to load the account with the domain of mycustomdomain. Am I missing something here ? Is this not how acts_as_tenant is supposed to work ? How can I achieve the described behavior ?

@excid3
Copy link
Collaborator

excid3 commented Mar 30, 2021

You're using a subdomain AND a custom domain combined for shop.mycustomdomain.io. ActsAsTenant will only lookup using a subdomain OR a domain. Not both.

See: https://github.com/ErwinM/acts_as_tenant/blob/master/lib/acts_as_tenant/controller_extensions/subdomain_or_domain.rb#L15

I recommend you use set_current_tenant_through_filter. That way you can check the domain first before you decide to use a subdomain or not.

@DavidGeismarLtd
Copy link
Author

Thanks for your answer @excid3. Following what you said I tried it with a naked domain (no subdomain) just mycustomdomain.co.uk. I would expect then acts_as_tenant to catch the right tenant based on the naked domain, right ?
However the logs still show a search perform on the subdomain column :
DEBUG -- : [69237831-ac24-491f-94a9-a575d0537844] Account Load (1.3ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."subdomain" = $1 LIMIT $2 [["subdomain", "mycustomdomain"], ["LIMIT", 1]]

@excid3
Copy link
Collaborator

excid3 commented Mar 30, 2021

Because it thinks you have a subdomain since there are 2 periods. You have to configure Rails for a tld_length of 2 if you're using a TLD like .co.uk otherwise it thinks your domain is co.uk

config.action_dispatch.tld_length = 2

@DavidGeismarLtd
Copy link
Author

@excid3 thanks for the info, before doing that I wanted to check if things would work with a domain with one period. So I used mycustomdomain.uk. However the search still performs on the subdomain column, on an empty string this time :
SELECT "accounts".* FROM "accounts" WHERE "accounts"."subdomain" = $1 LIMIT $2 [["subdomain", ""], ["LIMIT", 1]]
I checked and indeed Rails.application.action_dispatch.tld_length is equal to 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants