Codelove
Codelove

Reputation: 68

Devise and apartment gem multitenant app

My app currently only allows the user to sign_in on their subdomain i.e subdomain.domain.com/users/sign_in. I would like the users to login at domain.com/users/sign_in instead.

I'm a Rails newbie and I got a lot of assistance with regards to subdomains from this project on Github: https://github.com/appcasts/saas-timetracker

In routes.rb I added "devise_for :users" to have the sign_in form show at domain.com/users/sign_in, however I'm unable to authenticate the user to the subdomain as I get an "Invalid email and password" error.

Is there a way that users can login from domain.com and be authenticated and redirected to their correct subdomain?

I have tried: How to: Scope login to subdomain

And I get a column "subdomain" does not exist error. I also tried the Custom_Failure_app solution, which kept giving me an error on all redirect_to paths I add.

Any assistance or suggestions will be appreciated.

Upvotes: 4

Views: 3102

Answers (1)

Marcel
Marcel

Reputation: 84

In our application we create a user with:

def create 
  @user = User.new(user_params)
  @account = Account.new(subdomain: params[:subdomain], owner: @user)
  if @user.valid? && @account.valid?
    Apartment::Tenant.create(@account.subdomain)
    Apartment::Tenant.switch(@account.subdomain)
    @user.save
    @account.save
    redirect_to new_user_session_url(subdomain: @account.subdomain)
  end 
end

In the create method on account controller.

You could check it out here https://github.com/DefactoSoftware/Hours/blob/development/app/controllers/accounts_controller.rb

Upvotes: 4

Related Questions