Permalink
Browse files

perf: improve cookie handling

  • Loading branch information...
pi0 committed Feb 2, 2018
1 parent c5006aa commit c50e68fcf86aa1255a3601f87a074503f059ee17
Showing with 12 additions and 24 deletions.
  1. +3 −3 README.md
  2. +8 −20 lib/auth.js
  3. +1 −1 lib/defaults.js
@@ -199,7 +199,7 @@ Default:
```js
cookie: {
name: 'token',
params: {
options: {
path: '/'
}
}
@@ -210,8 +210,8 @@ Using cookies is **required** for SSR requests to work with JWT tokens.
It can be disabled by setting `cookie` to `false`.
* **name** - Cookie name,
* **params** - Cookie params.
* `params.expires` can be used to speficy cookie lifetime in seconds. Default is session only.
* **options** - Cookie options.
* `options.expires` can be used to speficy cookie lifetime in seconds. Default is session only.
## License
@@ -1,5 +1,5 @@
import Cookie from 'cookie'
import Cookies from 'js-cookie'
import { parse as parseCookie } from 'cookie'
import getProp from 'dotprop'
import Vue from 'vue'
@@ -153,29 +153,17 @@ export default class Auth {
}
}
setCookie (name, value, params = {}) {
if (!this.options.cookie) {
setCookie (name, value, options = {}) {
if (!this.options.cookie || !process.browser) {
return
}
const _params = Object.assign({}, this.options.cookie.params, params)
const _options = Object.assign({}, this.options.cookie.options, options)
if (!value) {
let date = new Date()
date.setDate(date.getDate() - 1)
_params.expires = date
}
if (process.browser) {
Cookies.set(name, value, _params)
if (value) {
Cookies.set(name, value, _options)
} else {
// Don't send duplicate token via Set-Cookie
if (!value) {
this.$res.setHeader(
'Set-Cookie',
Cookie.serialize(name, value, _params)
)
}
Cookies.remove(name, _options)
}
}
@@ -184,7 +172,7 @@ export default class Auth {
? document.cookie
: this.$req.headers.cookie
const cookies = Cookie.parse(cookieStr || '') || {}
const cookies = parseCookie(cookieStr || '') || {}
return cookies[name]
}
@@ -17,7 +17,7 @@ module.exports = {
},
cookie: {
name: 'token',
params: {
options: {
path: '/'
}
}

0 comments on commit c50e68f

Please sign in to comment.