How do I store a session variable for a specific time?
Or how to reset a variable after a certain time?
I need to use session.delete ('my_key')
15 seconds after the event.
By default, Rails will use CookieStore
to store the session. This means all of sessions data will be stored in the cookie at the Client side. And, by this way, you can do sth like
session['your_key'] = 'Session Content'
session['your_key_expired_at'] = Time.current + 15.minutes
Then, later (for ex: authenticate_user
), you can check
if session['your_key_expired_at'] < Time.current
session.delete('your_key')
end
If you want the data still available after user closes the app, you can use Cookies
, as below
cookies[:your_key] = {:value => "session stuff"}, :expires => 15.minutes.from_now}
You can use signed cookies
to encrypt your cookies with the apps secret_token, as using session
cookies.signed[:login] = {:value => @user.id, :expires => 1.day.from_now}
Another option, in order to make it more secure, you can use ActiveRecordStore
to manage and store session. You just pass the session id to your client, and you can easily manage your session at sever side because you know its created_at