Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise: call method on sessions#destroy

I was wondering what the best way to add extra functionality to Devise's session#destroy action would be.

To give some context, I'm making a website where Users have Carts, and when the user's session either times out or he logs out, I want his Cart to be labeled as inactive.

I found this but I'm a bit hesitant to override the Devise controller, as it seems a bit messy...

Are there any other ways to set this Cart to inactive when a user's session is destroyed?

like image 880
varatis Avatar asked Dec 20 '25 05:12

varatis


1 Answers

I do suggest you derive your controller from devise and hook onto the action, so you can safely keep away from devise's internals.

# routes.rb
devise_for :users, :controllers => { :sessions => "sessions" } # etc

# sessions_controller.rb
class SessionsController < Devise::SessionsController

  after_filter :set_cart_inactive!, :only => :destroy
  def set_cart_inactive!
    unless user_signed_in? # logout successful?
       # code here
    end
  end

end
like image 73
Viktor Trón Avatar answered Dec 21 '25 21:12

Viktor Trón



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!