Bharat Ahluwalia
Bharat Ahluwalia

Reputation: 446

Cant get devise sign_out to work properly

I am developing a rails app with devise for user authentication. I am running into issues when the user tries to log out. The desired behavior is that when the user clicks the logout button, they are logged out and are taken to the login page. However, what actually happens right now is that the page they are currently on stays. they need to hit the refresh button or try to navigate to another page, to show the login screen.

I tried overriding the after_sign_out_path_for(resource_or_scope) method in the application controller to redirect it to another page which would cause the login page to show. This call was never triggered.

I added an after_filter as well to my sessions controller and in that I tried to redirect to another page. However this caused doublerender errors to show up.

I have spent a lot of time on stackoverflow and the interwebs trying to find a solution but I am unable to. Any help will be appreciated.

Logout link code is

<%= link_to('Logout', destroy_user_session_path, :method => 'get',:class=>'btn btn-sm btn-default') %> 

I have changed the devise initializer to support "get" rather than delete as well.

Any ideas?

edit: registration_controller.rb

class RegistrationsController < Devise::RegistrationsController  
    layout "devise_layout"
    respond_to :json
end 

routes.rb

devise_for :users, :controllers => {sessions: 'sessions', registrations: 'registrations'}  
  scope "/admin" do
    resources :users
  end

Upvotes: 0

Views: 1755

Answers (4)

Apoorv
Apoorv

Reputation: 1389

Just add the method :delete and you are done.

Upvotes: 0

Bharat Ahluwalia
Bharat Ahluwalia

Reputation: 446

Alright so after some more digging around, I discovered the following helper method

sign_out_and_redirect(current_user)

using this causes my after_sign_out_path_for function to be triggered and now I have it working.

Upvotes: 1

Tashows
Tashows

Reputation: 558

Sorry I cannot post a comment due to reputation. What I would do:

  1. Run rake routes to see in which controller#action your get destroy_user_session leads to
  2. Go to that controller#action and after destroying the user session, add a redirect_to login_path or call the after_sign_out_path method.

Having said that, could you show us your sessions controller?

Upvotes: 1

Nitin
Nitin

Reputation: 7366

If you are redirect back to login screen after refresh or navigate to any other page, then it clearly indicating your logout action already run.

So seems like your action trigger like a ajax call. Try use 'data-no-turbolink' => true in your link_to. And if this is not case update your console log.

Upvotes: 0

Related Questions