Skip to content

Authentication and subdomains

Harvey Ico edited this page Nov 1, 2018 · 3 revisions

If you are using subdomains in your application you'll have to set some configurations for them to work properly with Clearance. Unless you do so, you'll notice that sessions get lost when navigating across different subdomains.

In this example we'll be using .test domain for development, which is provided by Puma-dev, which is a convenient way to get subdomains to work on macOS without further configuration. We'll also be using lvh.me for acceptance tests. You can learn more about lvh.me here.

First, you want to make sure that Rails is set to share cookies across subdomains:

Rails.application.config.session_store :cookie_store, key: '_myapp_session', domain: {
  production: '.myapp.com',
  development: '.myapp.test',
  test: '.lvh.me',
}.fetch(Rails.env.to_sym, :all), tld_length: 2

Next, you also want to make sure that Clearance uses the right domain. Don't miss the test environment, or sessions will be lost across subdomains in acceptance (aka system) tests.

Clearance.configure do |config|
  config.cookie_domain = {
    production: ".myapp.com",
    development: ".myapp.test",
    test: ".lvh.me",
  }.fetch(Rails.env.to_sym, nil)
  # Other configurations
end

If you're using Capybara for acceptance tests, make sure that host is set to lvh.me as in the examples above.

Capybara.configure do |config|
  config.app_host = "http://lvh.me"
end