How To: redirect from HTTPS to HTTP on successful sign out

KevinTriplett edited this page Jun 13, 2012 · 1 revision
Clone this wiki locally

If your application requires that signed in users access your site using HTTPS protocol, but you need them to be redirected back to HTTP protocol when they sign out, put this in your application_controller.rb file:

def after_sign_out_path_for(resource_or_scope)
  root_url(:protocol => 'http')
end

If you want them to be re-directed back to some other address, use that instead of root, but you must use the *_url suffix because the *_path helpers ignore the :protocol key.

However, if you have multiple roles, like an admin and client and supervisor, it's a little more complicated, not much:

def after_sign_out_path_for(resource_or_scope)
  [current_supervisor, current_admin, current_client].compact.count <= 1 ? root_url(:protocol => 'http') : root_path
end

When this method is called, current_{resource_name} for the role from which the user has just signed out is non-nil. So you have to check for only 1 role still non-nil, then you know that they are signing out from their last role. You see, Devise is so awesome that a user can be signed in under more than one role!