Storing cookies


#1

I am trying to set cookies on my adonis app using
response.cookie('cartTotal', 20)
and accessing it on another controller using
response.cookie('cartTotal')

the later returns undefined value, what am doing wrong

here is my code

async setcookie({params, response}){
    try {
        const {id} = params

        response.cookie('pageViews', 'pageViews')

        return response.json({
           completed: true
        })

    } catch (error) {
        return response.json({
          alert: {
                type: 'info',
                title: 'Internal Error!',
                message: 'Please try again later.',
                ButtonText: 'Ok'
            }
        })
    }
}

and accessing it like this

async showcookie({auth,response,view,session, request}) {
    console.log(request.cookie('pageViews'))
}

my session.js file in config folder looks like this

cookie: {
    httpOnly: true,
    domain: 'dev.local',
    sameSite: false,
    path: '/'
  },

#2

hello,

I see a typo in your code:

async showcookie({auth,response,view,session, request}) {
    console.log(request.cookie('pageViews)) // <- you are missing a ' sign
}

it should be:

async showcookie({auth,response,view,session, request}) {
    console.log(request.cookie('pageViews'))
}

correct this and try again to see if everything is ok


#3

my bad that is just a typing error. still wont work.


#4

It’s hard to tell, since it’s not clear when showCookie and setCookie methods are called.

Can you share the routes for them too?


#5

@virk the cookies is being set via post method and shown via get method.