Skip to content

Commit

Permalink
fix(storage): improve setCookie to prevent duplicated cookies (#718)
Browse files Browse the repository at this point in the history
Search for cookies with the same name and remove the cookie with old value.
  • Loading branch information
JoaoPedroAS51 authored Jun 13, 2020
1 parent 73fd1fe commit 3f0073b
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/core/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,10 @@ export default class Storage {
document.cookie = serializedCookie
} else if (process.server && this.ctx.res) {
// Send Set-Cookie header from server side
const prevCookies = this.ctx.res.getHeader('Set-Cookie')
this.ctx.res.setHeader('Set-Cookie', [].concat(prevCookies, serializedCookie).filter(v => v))
const cookies = this.ctx.res.getHeader('Set-Cookie') || []
cookies.unshift(serializedCookie)
this.ctx.res.setHeader('Set-Cookie', cookies
.filter((v, i, arr) => arr.findIndex(val => val.startsWith(v.substr(0, v.indexOf('=')))) === i))
}

return value
Expand Down

0 comments on commit 3f0073b

Please sign in to comment.