Reputation: 446
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
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
Reputation: 558
Sorry I cannot post a comment due to reputation. What I would do:
Having said that, could you show us your sessions controller?
Upvotes: 1
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