You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 25, 2021. It is now read-only.
Repository navigation
How To: Redirect to a specific page when the user can not be authenticated
elfassy edited this page Oct 25, 2011
·
6 revisions
If a user is not authenticatable you can redirect to a specific page.
For this example we want to use the entire URL with a particular sub-domain. Devise uses the path not the entire URL by default. The workaround is to use a custom failure app inherited from Devise failure app.
Rails 3
classCustomFailure < Devise::FailureAppdefredirect_url#return super unless [:worker, :employer, :user].include?(scope) #make it specific to a scopenew_user_session_url(:subdomain=>'secure')end# You need to override respond to eliminate recalldefrespondifhttp_auth?http_authelseredirectendendend
And add the following in config/initializers/devise.rb:
If you’re getting an uninitialized constant CustomFailure error, and you’ve put the CustomFailure class under your /lib directory, make sure to autoload your lib files in your application.rb file, like below
config.autoload_paths += %W(#{config.root}/lib)
Rails 2.3.x
classCustomFailure < Devise::FailureAppincludeActionController::UrlWriterdefrespond!options=@env['warden.options']scope=options[:scope]redirect_path=ifmapping=Devise.mappings[scope]eval("new_#{scope}_session_url(:host => 'secure.domain.com', :protocol => 'https')")else"/#{default_url}"endquery_string=query_string_for(options)store_location!(scope)headers={}headers["Location"]=redirect_pathheaders["Location"] << "?" << query_stringunlessquery_string.empty?headers["Content-Type"]='text/plain'[302,headers,["You are being redirected to #{redirect_path}"]]endend
And add the following in config/initializers/devise.rb: