javascript - Easiest way of making rails sessions persist between browser restarts -
what easiest way of implementing cookies such session persists between browser restarts?
the best thing use what's called remember me cookie. since session cookie not persist between browser restarts, need use other kind of cookie indicates application user are. commonly remember me functionality implemented having user select application remember them. happens need create cookie act password user. typically want following:
- select attribute related user user not able access or view. example, time account created, or randomly generated string store them.
- hash attribute cookie value not recognizable.
here example:
identifier = current_user.id value = digest::sha1.hexdigest(current_user.created_at)[6,10] cookies['remember_me_id'] = {:value => identifier, :expires => 30.days.from_now} cookies['remember_me_key'] = {:value => value, :expires => 30.days.from_now}
finally, when checking if user logged in, you'll need check if logged in using remember cookie well. example:
def current_user current_user ||= login_from_session || login_from_cookie end def login_from_session current_user = user.find(session[:id]) unless session[:id].nil? end def login_from_cookie user = user.find(cookies['remember_me_id']) if cookies['remember_me_key'] == digest::sha1.hexdigest(user.created_at)[6,10] current_user = user else nil end end
this should started on way implementing cookies persist outside of browser restart.
Comments
Post a Comment