Reputation: 4927
When I am in my admin interface I'd like to see what users are signed in.
like this
Using devise, I added the trackable feature
in my migration
t.integer :sign_in_count, :default => 0
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
Problem is that if a user signs out the :current_sign_in_at
and :current_sign_in_ip
is not set to nil.
I added this to my sessions_controller.rb
def destroy
current_user.current_sign_in_at = nil
current_user.save
signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(current_user))
set_flash_message :notice, :signed_out if signed_out
redirect_to root_url
end
Now the current_sign_in_at
is updated when the users sign out.
Then I added the :timeoutable
mpdule. When the sessions time out the current_sign_in_at
not is updated.
After all this hacking, I begin to wounder: Does not devise have a more elegant way of doing this?
Upvotes: 1
Views: 538
Reputation: 4927
This is a bit hard.
If the user signs out it is easy to nil out the ':current_sign_in_at'
It's a bit harder when the session times out. When the user tries to enter after the time out devise does a warden.logout
on the session and the user have to sign in again.
If the user does not do anything or just closes the browser he/she are still signed in, as far as the server knows.
To make this work we'd have to: A: Ask the clients browser for env['warden'].session.last_request_at all the time B: Create the session table with 'rake db:sessions:create' and put this in the session_store.rb initializer.
Squadlink::Application.config.session_store :active_record_store
Then the sessions will be on the server for better control.
For now i did this
config/initializers/warden.rb
Warden::Manager.before_logout do |record, warden, opts|
if record.respond_to?(:logout_stamp!)
record.logout_stamp!
end
end
models/user.rb
def logout_stamp!
self.current_sign_in_at = nil
self.current_sign_in_ip = nil
self.save
end
And removed the
current_user.current_sign_in_at = nil
current_user.save
lines form the sessions_controller destroy action.
If the user signs out or gets signed out the 'current_sign_in_at' and 'current_sign_in_ip' are set to nil.
Hope it helps someone.
Upvotes: 0