Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cookie plugin, issues with path

Tags:

grails

So, I'm trying to use the cookie plugin (seen here: http://grails.org/plugin/cookie), and overall, it's working pretty well. However I'm running into issues accessing my cookie from different parts of the app, due to path problems.

I'm getting identical cookies stored in different places depending on where in the app I am.

I've added this to my Config.groovy, as suggested by the plugin page:

com.studentuniverse.grails.plugins.cookie.services.CookieService.metaClass.setCookie = { response, name, value, maxAge ->
def cookie = new javax.servlet.http.Cookie(name, value)
cookie.setMaxAge(maxAge)
cookie.setPath("/")
response.addCookie(cookie)

}

But that doesn't seem to change anything. I still get two cookies, one stored at /[AppName]/[Controller1] and the other at /[AppName]/[Controller2]. Ideally, I'd like both to just be stored at /[AppName]. However, any changes I make to the setPath variable seem to have no effect. I've even tried changing it to

cookie.setPath("/cookies")

anticipating that it would be stored at /[AppName]/[Controller1]/cookies , for example, but no change was made. Do I need to pass in the path variable somewhere else, or modify that path in a different way?

like image 884
lashiel Avatar asked Jun 24 '26 09:06

lashiel


1 Answers

Have you tried adding domain.

Here is my code and it works fine

def myCookie = new Cookie(cookieName, cookieValue)
        myCookie.setMaxAge(cookieExpTime)
        myCookie.setDomain(cookieDomain)
        myCookie.setPath("/")
        // Force all cookie to only be able to be transmitted over SSL if true.
        myCookie.setSecure(isSecure)
        response.addCookie(myCookie)
like image 164
allthenutsandbolts Avatar answered Jun 26 '26 23:06

allthenutsandbolts