Log a user into their subdomain after registration with Rails and Devise

前端 未结 2 1065
温柔的废话
温柔的废话 2020-12-13 15:42

I\'m using Devise for authentication in my Rails 3 app. The application uses PostgreSQL schemas and the Apartment gem to facilitate multi-tenancy.

Logging in and ou

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 16:07

    You could use domain: :all option in your config.session_store and just have a before_action just as suggested by some in the comments.

    So you'll still have the code in config/initializers/session_store.rb or in config/application.rb:

    config.session_store :cookie_store, :key => '_domain_session', :domain => :all
    

    Then in your application_controller add the following code:

    #app/controllers/application_controller.rb
    before_action :check_subdomain
    
    def check_subdomain
      unless request.subdomain == current_user.account.subdomain
        redirect_to root_path, alert: "You are not authorized to access that subdomain."
      end
    end
    

提交回复
热议问题