Reputation: 51
I am building a multi-tenant Rails 5 app (will be hosted at heroku) at the moment (using apartment gem) which will have clients scoped by subdomain, e.g. client1.example.com, client2.example.com, client3.example.com. All seems pretty straight forward, but I would love to have a second Rails app for the rest of the site e.g. example.com/pricing, example.com/faq. What would be the best solution to achieve this design:
I have a suspicion that sign in/up bit might not be possible due to same URL structure, so I might need to have an extra logic built it for it maybe under app.example.com/sign_up or even under Rails App 1. Any suggestion how to build this in a better way would be appreciated!
Upvotes: 1
Views: 212
Reputation: 14268
A little concerned this will be closed with "overly broad", but a simple solution here is to do heroku domains:add www.example.com
and heroku domains:add example.com
to Rails app 1. Then heroku domains:add *.example.com
on Rails app 2 and to have two entirely separate apps on Heroku.
Upvotes: 1
Reputation: 3870
Don't think you can mount two apps under the root directory. You might be able to achieve this if you're willing to namespace your Devise paths under a subdirectory, eg. /session/sign_up
and /session/sign_in
This will require an extra app (App 3) which just runs Devise loaded and deployed under the /sessions
subdirectory. You'll need to configure the relative url root for this app.
This will make authentication more complex as your session authentication cookie will be under your root domain instead of your individual subdomains. You'll probably need to use the domain: :all
or domain: .<domain>.com
option in initializers/session_store.rb
to share the cookie with subdomains. secret_key_base
also needs to be identical across all your apps.
You'll also need to play with Devise's session creation to redirect to the correct subdomain for the user upon signing in. Fun fact to think about - what if the user has accounts on multiple subdomains with the same email? Devise/Warden scopes might offer a way to handle that.
On the Heroku side:
Heroku can only route requests for one domain to one app (App 1). The Rack reverse proxy posted above will allow you to route authentication requests to App 3. For individual subdomain requests to be routed to App 2, you'll need to add each subdomain to App 2 as a separate custom domain.
Upvotes: 0