Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to set cookie when process.server is true #450

Open
usb248 opened this issue Feb 2, 2020 · 2 comments
Open

Unable to set cookie when process.server is true #450

usb248 opened this issue Feb 2, 2020 · 2 comments

Comments

@usb248
Copy link

usb248 commented Feb 2, 2020

What problem does this feature solve?

I have a serverMiddleware with a cookie parser with an internal API (with express).
// Export the server middleware

export default {
  path: '/api',
  handler: app
}

All work fine client side but when axios request is trigerred server side, axios doesn't add set-cookie. I have to do:

$axios.onResponse((resp) => {
        if(resp.headers['set-cookie'] && process.server){
            try{
                res.setHeader('Set-Cookie', [ resp.headers['set-cookie'] ])
            }catch(e) {}
        }
        return resp
    })

Why ?

This feature request is available on Nuxt community (#c131)
@ghost ghost added the cmty:feature-request label Feb 2, 2020
@renoirb
Copy link
Collaborator

renoirb commented Feb 3, 2020

Where are you writing this?

Do you have Nuxt with a proxy?

Set-Cookie is coming from server to client, so that client can add Cookie header from what it previously received.

What you're doing feels like you're transferring between resp that's visible in $axios.onResponse((resp) => {}), where inside you make reference to res (that we can't see, where is it?)

Can you give more info about what you're trying to achieve, and where this is done?

@usb248
Copy link
Author

usb248 commented Feb 3, 2020

Hi @renoirb ,

I write this in a plugin (which use @nuxt/axios module)
res is the node serverResponse (of nuxt) :

export default function ({ $axios, store, res }) {
 ....
$axios.onResponse((resp) => { ... })
})

I have several serverMiddlewares :

serverMiddleware: [
    bodyParser.json(),
    bodyParser.text(),
    '~/servermiddleware/cookie-session', // all routes
    '~/servermiddleware/csrf', // all routes
    '~/api' // prefix api/** (express api)
  ],

~/servermiddleware/cookie-session is a cookie parser which give me access to req.session.something = 'something' to do a set-cookie from server to client.
~/servermiddleware/csrf contain this :

const Tokens = require('csrf')
const tokens = new Tokens()
module.exports = async(req, res, next) => {

	const secret = req.session.csrfSecret || await tokens.secret()
	req.csrfToken = tokens.create(secret)
        // here writing cookie (init the session)
	req.session.csrfSecret = secret
	
  	next()
}

and ~/api contain express api with all routes and check the csrf token of incoming requests

problem scenario :
I'm a user (with any cookie), it's my first visit, the req.session.csrfSecret = secret is set (nuxt node request), in asyncData i'm trying to do a POST request (server-side) with axios (second request).
Axios ignore set-cookie header from first request and trying to do again req.session.csrfSecret = secret with another value instead of to retrieve the cookie of the first request (req.session.csrfSecret is still undefined because axios doesn't include the cookie)

When i do :

$axios.onResponse((resp) => {
        if(resp.headers['set-cookie'] && process.server){
            try{
                res.setHeader('Set-Cookie', [ resp.headers['set-cookie'] ])
            }catch(e) {}
        }
        return resp
    })

i trying to detect if the server axios response contain set-cookie to add this in nuxt client response (for the view). But when i have no cookie, on the first request, set-cookie header is duplicated for example ... and it's impossible to check csrf token because cookie is not sended by axios in server side. I find this ugly.

I tried cookiejar or this https://gist.github.com/nzvtrk/ebf494441e36200312faf82ce89de9f2 but it doesn't seem to work...or not compatible with nuxt/axios mobule anyway.

There is something that i haven't understand ?
How to "transport" cookie of one request to another request server side with axios ?
Thanks for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants