title: Devise - Sign in On Subdomain
body: <p>I am using devise gem in a rails application with multiple subdomains and multiple schema.</p>
  
  <p>Each subdomain is handled by controller, which look like this on <code>application_controller.rb</code> :</p>
  
  <pre><code>def load_subdomain
      @account = Account.find_by_subdomain!(request.subdomain)
  end
  
  def after_sign_in_path_for(resource)
      #admin login in domain
      if resource.is_a?(admin) 
        admin_path
      #user login in subdomain
      elsif resource.is_a?(user)
        subdomain_root_path
      else
        super
      end    
    end
  </code></pre>
  
  <p>on routes.rb</p>
  
  <pre><code>    match '/' =&gt; 'subdomains#index', :constraints =&gt; { :subdomain =&gt; /.+/ }, :as =&gt; :subdomain_root
  
  devise_for :users, :skip =&gt; [:sessions, :registrations]
  
    devise_scope :user do
      get '/login/user' =&gt; 'sesusers#new', :as =&gt; :new_session_user
      post '/signin/user' =&gt; 'sesusers#create', :as =&gt; :user_session
      delete '/logout/user' =&gt; 'sesusers#destroy', :as =&gt; :destroy_user_session
      post '/send/user' =&gt; 'passwordusers#create', :as =&gt; :passwordusers_create
      get '/change_password/user' =&gt; 'passwordusers#edit', :as =&gt; :passwordusers_edit
      put '/change/user' =&gt;  'passwordusers#update', :as =&gt; :passwordusers_update
    end
  </code></pre>
  
  <p>This leads to having multiple sign_in urls for each sub-domains.</p>
  
  <pre><code>subdomain1.lvh.me:3000/login/user
  subdomain2.lvh.me:3000/login/user
  subdomain3.lvh.me:3000/login/user
  ...
  ...
  etc
  </code></pre>
  
  <p>on sesusers_controller.rb</p>
  
  <pre><code>class SesusersController &lt; Devise::SessionsController
    include Tenantable::Schema::Controller
    before_filter :load_subdomain
  
    def new
      super
    end
  
    def create
      logger.info "Logged in"
      super
    end
  
    def destroy
      logger.info "Logout success"
      super
    end
  end
  </code></pre>
  
  <p>When I try login, success login and redirect to subdomain_root_path</p>
  
  <pre><code>Started POST "/signin/user" for 127.0.0.1 at 2013-04-08 20:36:04 +0700
  Processing by SesusersController#create as HTML
    Parameters: {"utf8"=&gt;"Γ£ô", "authenticity_token"=&gt;"DJ/MJnHU5pHwxQQYDSvwyBsawmx
  /9Ms7CXx3me0MBBE=", "user"=&gt;{"email"=&gt;"demouser@domain.com", "password"=&gt;"
  [FILTERED]", "remember_me"=&gt;"0"}, "commit"=&gt;"Login"}
    Account Load (2.0ms)  SELECT "public"."accounts".* FROM "public"."accounts" WH
  ERE "public"."accounts"."subdomain" = 'subdomain1' LIMIT 1
  Logged in
    User Load (2.0ms)  SELECT "users".* FROM "users" WHERE "users"."email" = 'demouser@domain.com' LIMIT 1
     (2.0ms)  BEGIN
     (53.0ms)  UPDATE "users" SET "last_sign_in_at" = '2013-04-08 12:55:28.631212'
  , "current_sign_in_at" = '2013-04-08 13:36:04.615889', "sign_in_count" = 4 WHERE
   "users"."id" = 16
     (7.0ms)  COMMIT
  Redirected to http://subdomain1.lvh.me:3000/
  Completed 302 Found in 1589ms (ActiveRecord: 0.0ms)
  
  
  Started GET "/" for 127.0.0.1 at 2013-04-08 20:36:06 +0700
  Processing by SubdomainsController#index as HTML
    Account Load (2.0ms)  SELECT "public"."accounts".* FROM "public"."accounts" WH
  ERE "public"."accounts"."subdomain" = 'subdomain1' LIMIT 1
    CACHE (0.0ms)  SELECT "public"."accounts".* FROM "public"."accounts" WHERE "pu
  blic"."accounts"."subdomain" = 'subdomain1' LIMIT 1
     (30.0ms)  select sum(pg_total_relation_size(table_schema || '.' || table_name
  )) from information_schema.tables where table_schema in ('subdomain') group by tab
  le_schema
    Rendered subdomains/index.html.erb within layouts/subdomain (81.0ms)
  Completed 200 OK in 170ms (Views: 130.0ms | ActiveRecord: 33.0ms) 
  </code></pre>
  
  <p>look, successful login.. view subdomain</p>
  
  <pre><code>&lt;div class="pull-right"&gt;
       &lt;div class="btn-group"&gt;
           &lt;% if user_signed_in? %&gt;
              &lt;%= link_to current_user.name, '#', { :class =&gt; 'btn btn-danger dropdown-toggle', 'data-toggle' =&gt; 'dropdown' } %&gt;
                  &lt;ul class="dropdown-menu"&gt;
                    &lt;li&gt; &lt;%= link_to "Dashboard", '#', 'style' =&gt;"color:#fff" %&gt;&lt;/li&gt;
                  &lt;/ul&gt;
           &lt;% else %&gt;
              &lt;%= link_to ('&lt;i class="icon-lock icon-white"&gt;&lt;/i&gt;').html_safe + " Login" +(' &lt;span class="caret"&gt;&lt;/span&gt;').html_safe , '#', { :class =&gt; 'btn btn-danger dropdown-toggle', 'data-toggle' =&gt; 'dropdown' } %&gt;
                  &lt;ul class="dropdown-menu"&gt;
                     &lt;li&gt;&lt;%= link_to "User", new_session_user_path, 'style' =&gt;"color:#000" %&gt;&lt;/li&gt;
  
                  &lt;/ul&gt;
           &lt;% end %&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>But on view subdomain <code>current_user.name</code> not display, appears only login button.</p>
  
  <p>can someone help me?</p>
  
  <p>thanks/</p>