Skip to content

Commit c50e68f

Browse files
Pooya Parsapi0
authored andcommitted
perf: improve cookie handling
1 parent c5006aa commit c50e68f

File tree

3 files changed

+12
-24
lines changed

3 files changed

+12
-24
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ Default:
199199
```js
200200
cookie: {
201201
name: 'token',
202-
params: {
202+
options: {
203203
path: '/'
204204
}
205205
}
@@ -210,8 +210,8 @@ Using cookies is **required** for SSR requests to work with JWT tokens.
210210
It can be disabled by setting `cookie` to `false`.
211211
212212
* **name** - Cookie name,
213-
* **params** - Cookie params.
214-
* `params.expires` can be used to speficy cookie lifetime in seconds. Default is session only.
213+
* **options** - Cookie options.
214+
* `options.expires` can be used to speficy cookie lifetime in seconds. Default is session only.
215215
216216
## License
217217

lib/auth.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import Cookie from 'cookie'
21
import Cookies from 'js-cookie'
2+
import { parse as parseCookie } from 'cookie'
33
import getProp from 'dotprop'
44
import Vue from 'vue'
55

@@ -153,29 +153,17 @@ export default class Auth {
153153
}
154154
}
155155

156-
setCookie (name, value, params = {}) {
157-
if (!this.options.cookie) {
156+
setCookie (name, value, options = {}) {
157+
if (!this.options.cookie || !process.browser) {
158158
return
159159
}
160160

161-
const _params = Object.assign({}, this.options.cookie.params, params)
161+
const _options = Object.assign({}, this.options.cookie.options, options)
162162

163-
if (!value) {
164-
let date = new Date()
165-
date.setDate(date.getDate() - 1)
166-
_params.expires = date
167-
}
168-
169-
if (process.browser) {
170-
Cookies.set(name, value, _params)
163+
if (value) {
164+
Cookies.set(name, value, _options)
171165
} else {
172-
// Don't send duplicate token via Set-Cookie
173-
if (!value) {
174-
this.$res.setHeader(
175-
'Set-Cookie',
176-
Cookie.serialize(name, value, _params)
177-
)
178-
}
166+
Cookies.remove(name, _options)
179167
}
180168
}
181169

@@ -184,7 +172,7 @@ export default class Auth {
184172
? document.cookie
185173
: this.$req.headers.cookie
186174

187-
const cookies = Cookie.parse(cookieStr || '') || {}
175+
const cookies = parseCookie(cookieStr || '') || {}
188176

189177
return cookies[name]
190178
}

lib/defaults.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = {
1717
},
1818
cookie: {
1919
name: 'token',
20-
params: {
20+
options: {
2121
path: '/'
2222
}
2323
}

0 commit comments

Comments
 (0)