Rails/Devise: determine user's total time logged in
Any suggestions for efficiently storing the total time a user is logged in using Devise? Other than tracking the number of sign ins, it doesn't look like Devise has an existing process for accomplishing this.
Thanks!
class User < ActiveRecord::Ba开发者_如何学Gose
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :timeoutable
end
The question that needs answering is "How do you know when a user has logged out?".
In the simple case, user clicks a log out link. You can then grab a timestamp of that event during the destruction of the user session. A simple compare of the trackable timestamp with the timestamp of the user session destruction gives you total time.
However, what happens if the user just closes the tab/window/browser with your app running? Is that user automatically logged out? Is there a cookie set that lets said user back in without logging in? If so, does reopening a page in the app mean the user is still logged in, 3 days later?
Once you come up with answers to these questions, I think that an approach will make itself visible to you.
Hope this helps.
精彩评论