4

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.

Codelove
  • 68
  • 1
  • 9

1 Answers1

4

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

Marcel
  • 84
  • 4
  • Your user account creation method is very much like what I have, I noticed that your login is on the subdomain as in subdomain.happyhours.io, however, I would like the login to be on http://happyhours.io/users/sign_in – Codelove Feb 21 '15 at 18:08
  • Than you have to ask for that subdomain there, and in your method to let the user sign in you have to switch to the right apartment with `Apartment::Tenant.switch(params[:subdomain])` and then use the code to sign the user in and send him to the right route – Marcel Feb 23 '15 at 08:48
  • Marcel, thanks for the input, however, the issue is not signup and redirecting after Sign_up. The account creation works fine and redirecting, so if I redirect to: new_user_session_url(subdomain: @account.subdomain), the user can login fine, however If I redirect to the root which is actually from the HomeController the user can enter their email and password, but they get an incorrect email and password error. – Codelove Feb 23 '15 at 10:08