Nuxt.js: Is there a way to delete a cookie in a middleware on server-side

Created on 30 Mar 2017  路  11Comments  路  Source: nuxt/nuxt.js

I have a middleware checking if there's an auth token provided in a cookie. Basing on this token I perform a request retrieving current user's profile. Chances are, that the token is invalid. So, I want to force logout in this case. Which means I need to delete this auth token cookie too. How can I do that on server-side?

This question is available on Nuxt.js community (#c416)
documentation question

Most helpful comment

'req'? I mean, shouldn't I look for the response instead of the request?

All 11 comments

@gbezyuk you should create a /api/logout route for example which remove this cookie on the server side and call this route in the middleware when the token is invalid.

API lives in a totally separate project, could even be at a different domain thus making this solution impossible. Auth is token-based and uses headers, so the cookie is required only for SSR.
Once again, a bit more broadly then: is there a way to modify SSR server response's headers (cookie, spicifically) using Nuxt?

If you are on the server-side and have access to req, yes you can update the headers, it should do the trick.

'req'? I mean, shouldn't I look for the response instead of the request?

@gbezyuk i got your mean . and i have the same problem. nuxt is very good . one day may be great.

I have this same issue. I need to access headers in the res not req during SSR.

@Atinux any progress on this one?

@Atinux reopening this so I don't forget to add examples following this merge.

Good idea @galvez

Closing as I've added this to my nuxt/docs TODO.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

surmon-china picture surmon-china  路  3Comments

vadimsg picture vadimsg  路  3Comments

uptownhr picture uptownhr  路  3Comments

lazycrazy picture lazycrazy  路  3Comments

vadimsg picture vadimsg  路  3Comments