user984621
user984621

Reputation: 48443

Rails + Devise: How to catch the event of logging out a user?

I am logging users' activities and when a user logged out, I want to call a method. How do I do that?

In routes, I have the following:

devise_scope :user do
  get 'logout',                             to: 'devise/sessions#destroy'
end

Do I need to create for this purpose a SessionController or is there other way around?

Upvotes: 1

Views: 499

Answers (1)

max
max

Reputation: 101811

You don't really "catch the event" as Rails is a MVC framework and not event oriented.

Instead you subclass the controller and call super with a block to "tap-into" the original implementation:

devise_scope :user, controllers: {
  sessions: 'my_sessions'
}
class MySessionController < ApplicationController
  def destroy
    super do
      # do your thing
      # this block is called after the user is signed out 
      # but before the response is set. 
    end
  end
end

This works since the Devise controllers yield.

Upvotes: 1

Related Questions