0

I want to destroy user session and expire cookies whenever i close the browser. To expire cookies i try to create cookies without expiration time but not work.

EDIT :

cookies[:test] = {value: "test123", domain: :all}

I create this cookie and i want when i close the broswer , the is not available there . and when user log in i set current user with session (session[:user_id= @user.id) and I want when I close the browser , session[:user_id] should be null.

Random
  • 3,158
  • 1
  • 15
  • 25
Pardeep Saini
  • 2,084
  • 1
  • 18
  • 29
  • Please post some code to show your efforts first. – Surya Jun 10 '15 at 09:22
  • cookies[:test] = {value: "test123", domain: :all} . i create this cookie and i want when i close the broswer , the is not available there . and when user log in i set current user with session (session[:user_id= @user.id) and i want when i close the browser , session[:user_id] should be nil. – Pardeep Saini Jun 10 '15 at 09:30
  • @PardeepSaini I edited your answer. Please edit your answer when giving such informations, don't post code in comments. detail as much as you can your question. – Random Jun 10 '15 at 09:34
  • @PardeepSaini : setting cookies without an expire time will be deleted once the **browser** (**not** the **tab** or **window**) is closed. Did you try to close the browser and checked for the existence of cookie in next request? – Surya Jun 10 '15 at 09:39
  • yes, checked everything. I close the browser and reopen but cookies are still there. – Pardeep Saini Jun 10 '15 at 09:46
  • Usually browsers (Firefox and Chrome for sure) will delete such cookies (without an expire date) only if you fully close the browser (eg all tabs and windows). So solution proposed by @mr-h is a better option IMHO – evgeny.myasishchev Jun 10 '15 at 15:36

1 Answers1

1

Just a quick suggestion:

I think if you use something like AngularJS or even jQuery, then you can fire a trigger when browser about to close to trigger a function to remove the cookies for you.

Regarding the Sessions you can use the same theory only you need to trigger Ajax as Session variables can be removed by Rails.

Mr H
  • 5,254
  • 3
  • 38
  • 43