Skip to content

Commit

Permalink
perf: improve cookie handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa authored and pi0 committed Feb 2, 2018
1 parent c5006aa commit c50e68f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ Default:
```js
cookie: {
name: 'token',
params: {
options: {
path: '/'
}
}
Expand All @@ -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
Expand Down
28 changes: 8 additions & 20 deletions lib/auth.js
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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]
}
Expand Down
2 changes: 1 addition & 1 deletion lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
},
cookie: {
name: 'token',
params: {
options: {
path: '/'
}
}
Expand Down

0 comments on commit c50e68f

Please sign in to comment.