I'm working on a Rails apps that uses an app
subdomain. For example, if a user visits my application at www.mysite.com
and logs in, they should be redirected to app.mysite.com/
(which should be the articles#index page as it's the authenticated root). For some reason, when I try to log in, it signs me in ok, but redirects me to my main root route, rather than the authenticated root route. Here is my routes.rb
file:
Rails.application.routes.draw do
devise_for :users, path: '', path_names: {
sign_up: 'signup',
sign_in: 'login',
sign_out: 'logout'
}
authenticated :user do
constraints subdomain: 'app' do
get '/topics', to: 'topics#index'
get '/articles', to: 'articles#index'
root to: 'articles#index', as: :authenticated_root
end
end
root 'home#index'
end
Do I need to adjust the devise set up so that it's aware of the app
subdomain? If so, how?
Any and all help would be greatly appreciated.
Thanks!
By default Devise just redirects to the root path:
def after_sign_in_path_for(resource_or_scope)
stored_location_for(resource_or_scope) || signed_in_root_path(resource_or_scope)
end
signed_in_root_path
will look for user_root_path
, root_path
and then fall back to "/"
.
Since this is a relative path it will always redirect back to the same subdomain that the request came from.
You need to alter the redirect after sign in to use an absolute URL:
def after_sign_in_path_for(resource)
stored_location_for(resource) || root_url(subdomain: 'app')
end
This method can either be overridden in your ApplicationController (since Devise inherits from it) or by subclassing Devise::RegistrationsController.