Rails 3 custom domains for users on Heroku - ruby-on-rails

I'm looking to set up custom domains for users. Much like Tumblr does.
I understand that the user must point their A record to an IP address. I found some information here: Custom domains in a Rails App
Can someone give me an example of this with a Heroku/Rails 3 setup? Is it even possible?

If you setup a wildcard DNS to point to your main app, you can use the :subdomain in config/routes.rb to handle your business logic.

Related

How do I set up a Rails page as a subdomain of another site?

We're trying to create pages in our Rails app that will eventually live on a subdomain of another partnering site. This would be like StatusPage, which allows users to create a status page with their account on the StatusPage site and then attach it to their own subdomain (e.g. status.usersite.com).
For example, if we wanted one of our pages (www.oursite.com/users/bobsplumbing) to be a subdomain on another site (ourservice.bobsplumbing.com), how would we go about it?
If it's useful info, we use Heroku to host the Rails app and we also utilize Route 53 and Cloudflare.
From your example I understand that you want to have multiple web apps since that would be your customer domain and your page will redirect to that page.
You will be better off to do NGINX (or whatever you use) redirects since they are faster and will take less time, being cached by the browser after the initial load.
To answer your question you can add this code to your routes:
sites = %w(bobsplumbing catsandboots)
sites.each do |name|
match "users/#{name}" => redirect("https://ourservice.#{name}.com")
end
You can also have a look at apartment gem.

Ember 2 Routing Subdomains

I have a Ember 2 application (ember-cli) that uses a Rails API as the back end. For this application, I have enabled Wildcard DNS with my DNS Provider (Cloudflare). When a user signs up with my website, I want them to be able to use their subdomain to access their public home page.
For example:
A user named Steve signs up for my site located at awesome.com. So Steve browses to steve.awesome.com, which internally would translate to awesome.com/users/steve. How do I setup my Ember routes such that it can route based off of the subdomain?
I have come to a solution, but it isn't exactly what I was initially looking for. I realized there really isn't a reason why the URL has to be awesome.com/users/steve, and instead have decided that their subdomain (or custom domain) will act as their identifier. So let's say Steve browses to steve.awesome.com, I will figure out the host via window.location.hostname, and use that as a lookup key to pass to my Rails API and retrieve user data.
Not exactly the solution I originally was seeking, but it solves my issue!

How to make own domain point to specific profile on my app

I currently give the ability for my users to have a subdomain for their account so:
subdomain.myaddress.com
will point to the users profile. I want to be able to give the user the ability to point their own domains as well so that both will work and point to the user profile.
How to do that?
Also, if you happen to know about Heroku. There is a plugin to enable custom domains but I have to call: heroku domains:add *.yourdomain.com (or use GUI)
Is there a way to make my app call that command instead?
Your application will need to listen to whatever domain the user enters, therefore you would have to use the heroku gem inside your application to add the custom domain the user enters to your application.
Using subdomains is obviously the simpler option here as you can just have the wildcard domain setup on your domain *.mydomain.com and let the choose whatever subdomain they want and then have your application work out the account to use from the subdomain.
You can use DNS CNAME records to alias one domain name to another. There are restrictions: you can't use a CNAME on a top-level domain (subdomains only). I don't know about automating it, but CNAME's are commonly used on heroku.

Multiple domains pointing to single Heroku Rails app via nameservers displaying different content?

We have a Rails 3 app which allows users to create a profile and access it with a subdomain. So for example:
user1.app.com
user2.app.com
user3.app.com
Now, suppose they wanted to point their own domain name to their profile, so www.user1.com shows user1.app.com, and www.user1.com/my-content/ shows user1.app.com/my-content/. Is it possible for them to simply change their nameservers to point to us, and we handle the routing? I'm afraid it would be a deal breaker if the user had to do any DNS configuration beyond just changing their nameservers.
Thanks!
This is really two questions.
So I guess I'll answer it like that.
How can I allow customers to add a custom domain to my heroku-based app?
So, in your example, their domain name is www.user1.com
Two things need to happen
www.user1.com needs to be added to your heroku domains for the app
the dns for www.user1.com needs to be set to the heroku servers
In order to add this new domain, I guess you need to store the custom domain in the app,
and when it is changed, or created, ensure a queued task is setup to push this config change to heroku.
In order to do the dns correctly, I hope it is enough that they add a CNAME for user1.app.com.
Therefore, if you have a wildcard CNAME linking to heroku's dns, you can avoid the type of downtime that can occur if Heroku gets DOS-ed again.
How can I show content based on domain, rather than just subdomain?
Well, somehow you already differentiate user1.app.com from user2.app.com
Maybe you just sniffed request.host.
Now you just have to try the custom domain first.
Maybe this'll work.
before_filter :set_client
def set_client
#client = Client.find_by_custom_domain(request.host) || Client.find_by_subdomain(request.host.split(".").first)
unless #client
raise "unrecognised domain #{request.host}"
end
end
You need to get them first of all have them set www.user1.com to point to proxy.heroku.com (or setup your own cname entry to proxy.heroku.com and get them to cname their domains to that) and then you need to register their custom domain with your heroku app so the correct application responds at Heroku stored against their account?? in your application so it can be matched on incoming requests.
This technique is employed in the Locomotive Rails CMS so I'd suggest looking at that - it uses the Heroku gem INSIDE the application so custom domains can be added via the site administrator which in turn talks to the Heroku API to add the custom domain to the application.

Rails - Multiple top level domains and a single session/cookie

I've been struggling with this for quite awhile and haven't been able to
find a solution. I need a user to be able to view multiple top level
domains with a single login.
My understanding is that this needs to be set in environment.rb and
called with before_dispatch. This is what I've come up with:
require 'activesupport'
require 'dispatcher'
module ActionController
class Dispatcher
def set_session_domain
ActionController::Base.session_options.update :session_domain => "#{#request.host}"
end
before_dispatch :set_session_domain
end
end
However, this does not seem to be working when I try and pull the values
from session[:session_domain].
Any help is greatly appreciated.
This one is a bit tricky. Since cookies can only be assigned to (and retrieved from) the current domain ("forms.example.com", say) and parent domains (".example.com", but not ".com"), but NOT to other domains ("othersite.com"), you'll have to find yourself another solution. This has nothing to do with Rails, but with how cookies work.
EDIT: Sessions rely on a client-specific handle, stored in a cookie, which is why sessions also don't work cross-domain.
This site has one possible solution for creating a cross-domain cookie, and it's the cleanest way I know of, although it may have some security implications. A more complicated version would have the servers communicate directly through some secure channel.
If you're looking for a more general-purpose single-login service, try implementing some form of OpenID.
For sub-domains in Rails 2.3
ActionController::Base.session = { :domain => ".mydomain.com" }
For top-level domains try this middleware.
I've been playing with the above middleware at the moment and it does not quite work as expected. If you do use the middleware you do not need the above code as it handles sub-domains as well.
You will probably need something like RubyCAS if you want authentication across domains regardless of whether they're top-level or subdomains.
Your question is not really precise enough IMHO. Do you want a single cookie for all Rails apps you have or is it within the context of a single one? If the former, you want to look at solutions using database-backed sessions or something along the line of RubyCAS to implement the CAS protocol.
Both Keltia and zuk are right, Answer is rubyCAS, We have do that integration and it allows
SSI - Single sign -in
You sign to one site and you are automatically signed to the other
SSO - Single Sign Out
You sign out from one site and automatically you signed out from the other
For us this is a proven solution and not a hard one to implement
we are using it in
http://www.cabslk.com and www.ticketslk.com
cheers,
Sameera

Resources