When using Devise in a Rails application, the default behavior is to not remove the session cookie on log out. This behavior can lead to the appearance of the user still being logged in even after they have logged out.

To resolve this issue, you can manually clear the session cookie on log out. Here's how you can do it:

  1. Customize Devise Sessions Controller: Create a new controller that inherits from Devise's SessionsController to override the log out behavior. You can generate the custom controller using the following command:
rails generate devise:controllers [scope]

Replace [scope] with the scope of your Devise model (e.g., users, admins, etc.).

  1. Modify the Custom Sessions Controller: Open the generated custom sessions controller (app/controllers/[scope]/sessions_controller.rb) and add the log out action. In this action, you'll clear the session cookie manually.
# app/controllers/[scope]/sessions_controller.rb class Users::SessionsController < Devise::SessionsController # ... def destroy # Clear the session cookie cookies.delete("_your_app_name_session") # Continue with Devise's default log out behavior super end # ... end

Replace "_your_app_name_session" with the name of the session cookie used in your application. You can find the actual session cookie name by inspecting the cookies in your browser's Developer Tools.

  1. Update Routes to Use the Custom Controller: In your config/routes.rb file, update the routes for the Devise sessions to use the custom controller instead of the default Devise controller.
# config/routes.rb devise_for :users, controllers: { sessions: 'users/sessions' }

Replace :users with the appropriate scope for your Devise model.

  1. Restart the Server: After making these changes, restart your Rails server to apply the modifications.

Now, when a user logs out, the session cookie should be manually cleared, and the user will no longer appear to be logged in. This ensures that the log out process is complete and the session is properly terminated.

Have questions or queries?
Get in Touch