Remove A Shared Cookie

What's the worst part of making cookies? Cleaning up.

Initially, I had set up code to set a cookie in the simplest fashion possible:

cookies[:my_cookie] = {:value => 'test', :expires => 1.year.from_now}

Cleanup was a snap:

cookies.delete :my_cookie

That was working great but to add some usability we decided that we wanted the cookie to be shared between multiple sub domains on our site. This was simple as well, as we could just add a single arg to set the domain to something a bit more general:

cookies[:my_cookie] = {:domain => '.thedomain.com', :value => 'test', :expires => 1.year.from_now}

Awesome. Immediately, you can sign into foo.thedomain.com and bar.thedomain.com would know you as soon as you came in, instead of making you re-authenticate… but there seems to be an issue with the cleanup now. Looks like the explicit change in domain is causing a problem.

Not a big deal either… instead of using delete, I am just expiring the cookie now. This allows me to specify the domain while still accomplishing the goal of removing the cookie.

cookies[:my_cookie] = {:domain => '.thedomain.com', :expires => Time.now}

Advertisements

Leave a comment

Filed under Cookies, rails, Ruby

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s