Sam 山
Sam 山

Reputation: 42863

Devise call backs

Does devise have call backs when a user signs in and out?

This is what I came up with:

Warden::Manager.after_authentication do |user,auth,opts|
  user.update_attribute(:currently_signed_in, true)
end

Warden::Manager.before_logout do |user,auth,opts|
  user.update_attribute(:currently_signed_in, false)
end

This is what I came with to track users that are currently signed in.

Upvotes: 24

Views: 7247

Answers (2)

Moustafa Samir
Moustafa Samir

Reputation: 2268

You can overwrite sign_in in your application controller like this

 def sign_in(*args)
   super(*args)
    # do whatever you want here
    token = current_user.authentications.where(provider: "facebook").first.token
    facebook = Koala::Facebook::API.new(token)
    session[:facebook] = facebook
end

Upvotes: 5

srboisvert
srboisvert

Reputation: 12759

I'm no expert but I believe the callbacks (hooks) are at the Warden level (Devise is built on top of Warden).

after_set_user and before_logout in Warden should do the trick for you but there are other options listed in Warden::Hooks

Upvotes: 17

Related Questions