Skip to content

Commit

Permalink
feat(init): try to get cookie from session
Browse files Browse the repository at this point in the history
  • Loading branch information
talissonf committed Aug 22, 2019
1 parent 7fbc42e commit dafbeab
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/methods/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default (self, document) => {
// try to get session from cookie
const decodedCookie = decodeURIComponent(document.cookie)
const ca = decodedCookie.split(';')

for (let i = 0; i < ca.length; i++) {
let c = ca[i]
while (c.charAt(0) === ' ') {
c = c.substring(1)
}
if (c.indexOf(self.cookieName) === 0) {
// found cookie
const sessionJson = c.substring(self.cookieName.length, c.length)
try {
self.session = JSON.parse(sessionJson)
if (typeof self.session === 'object' && self.session !== null && !Array.isArray(self.session)) {
// ok
return
}
} catch (e) {
// invalid cookie value
}
// reset
self.session = {}
return
}
}
}

0 comments on commit dafbeab

Please sign in to comment.