2013-04-08 28 views
5

Tôi đang sử dụng đá quý kiến ​​tạo trong ứng dụng đường ray với nhiều tên miền phụ và nhiều lược đồ.Devise - Đăng nhập trên Subdomain

Mỗi tên miền phụ được xử lý bởi bộ điều khiển, mà trông như thế này trên application_controller.rb:

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 

trên routes.rb

match '/' => 'subdomains#index', :constraints => { :subdomain => /.+/ }, :as => :subdomain_root 

devise_for :users, :skip => [:sessions, :registrations] 

    devise_scope :user do 
    get '/login/user' => 'sesusers#new', :as => :new_session_user 
    post '/signin/user' => 'sesusers#create', :as => :user_session 
    delete '/logout/user' => 'sesusers#destroy', :as => :destroy_user_session 
    post '/send/user' => 'passwordusers#create', :as => :passwordusers_create 
    get '/change_password/user' => 'passwordusers#edit', :as => :passwordusers_edit 
    put '/change/user' => 'passwordusers#update', :as => :passwordusers_update 
    end 

Điều này dẫn đến việc có nhiều url sign_in cho mỗi tên miền phụ.

subdomain1.lvh.me:3000/login/user 
subdomain2.lvh.me:3000/login/user 
subdomain3.lvh.me:3000/login/user 
... 
... 
etc 

trên sesusers_controller.rb

class SesusersController < 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 

Khi tôi cố gắng đăng nhập, thành công đăng nhập và chuyển hướng đến subdomain_root_path

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"=>"Γ£ô", "authenticity_token"=>"DJ/MJnHU5pHwxQQYDSvwyBsawmx 
/9Ms7CXx3me0MBBE=", "user"=>{"email"=>"[email protected]", "password"=>" 
[FILTERED]", "remember_me"=>"0"}, "commit"=>"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" = '[email protected]' 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) 

nhìn, đăng nhập thành công .. xem subdomain

<div class="pull-right"> 
    <div class="btn-group"> 
     <% if user_signed_in? %> 
      <%= link_to current_user.name, '#', { :class => 'btn btn-danger dropdown-toggle', 'data-toggle' => 'dropdown' } %> 
       <ul class="dropdown-menu"> 
        <li> <%= link_to "Dashboard", '#', 'style' =>"color:#fff" %></li> 
       </ul> 
     <% else %> 
      <%= link_to ('<i class="icon-lock icon-white"></i>').html_safe + " Login" +(' <span class="caret"></span>').html_safe , '#', { :class => 'btn btn-danger dropdown-toggle', 'data-toggle' => 'dropdown' } %> 
       <ul class="dropdown-menu"> 
        <li><%= link_to "User", new_session_user_path, 'style' =>"color:#000" %></li> 

       </ul> 
     <% end %> 
    </div> 
</div> 

Nhưng trên tên miền phụ xem current_user.name không hiển thị, chỉ xuất hiện nút đăng nhập.

ai đó có thể giúp tôi không?

nhờ/

Trả lời

5

Bạn chỉ cần chia sẻ user session giữa các tên miền phụ cho điều này, do đó một phiên có thể được tiếp tục với nhiều tên miền phụ.

Bạn nên sửa đổi các tập tin session_store.rb trong khởi như,

DemoApp::Application.config.session_store :cookie_store, key: '_jcrop_app_session', domain: ".maindomain.com" 

Thêm lĩnh vực sẽ làm việc và cẩn thận với những hàng đầu cuối cùng "." (thời gian) đó là cần thiết cho tên miền phụ.

+0

oopss, tôi đã cấu hình sai như '* .lvh.me' .. cảm ơn bro. –

+0

Điều tương tự thực sự là: 'DemoApp :: Application.config.session_store: cookie_store, key: '_jcrop_app_session', tên miền:" .maindomain.com "' –

Các vấn đề liên quan