How do I avoid a redirect when the user signs out in Devise?

Viewed 337

For some reason I want to avoid being redirected after a user signs out.

here is devise's sessions controller.

I have already tried:

  • to override after_sign_out_path_for but I get either No route matches [DELETE] "/" or Cannot redirect to nil!
  • to skip_before_action :verify_signed_out_user in my Sessions controller; but it still tries to redirect.

How can I just destroy the users's session without being redirect ?

1 Answers

You can change the behaviour by overriding Devise::SessionsController and the #destroy method:

class MySessionsController < Devise::SessionsController
  def destroy
    signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
    # the `now: true` option sets the flash for this request 
    set_flash_message! :notice, :signed_out, now: true if signed_out
    respond_to do |format|
      format.all { head :no_content }
      format.any(*navigational_formats) { render 'something' }
    end
  end
end

You also need to tell the router to route to your custom controller:

# config/routes.rb
devise_for :users, controllers: { sessions: "my_sessions" }
Related