Skip to content

Instantly share code, notes, and snippets.

@codenamezjames
Created May 18, 2020 18:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codenamezjames/b5b2033747c0946b324fcbcb9961bffd to your computer and use it in GitHub Desktop.
Save codenamezjames/b5b2033747c0946b324fcbcb9961bffd to your computer and use it in GitHub Desktop.
Vuex persist ssr auth
import VuexPersist from 'vuex-persist'
import Cookies from 'js-cookie'
export default ({ store, ssrContext }) => {
// Cookie Store for user/auth (ssr ajax auth)
const cookieModules = ['user']
const startsWithCookieRegex = new RegExp(`^(${cookieModules.join('|')})/`)
new VuexPersist({
filter: mutation => startsWithCookieRegex.test(mutation.type),
restoreState: key => {
try {
return ssrContext
? JSON.parse(
require('cookie').parse(ssrContext.req.headers.cookie || '')[key]
)
: Cookies.getJSON(key)
} catch (e) {
return {}
}
},
saveState: (key, state) => Cookies.set(key, state, { expires: 365 }),
modules: cookieModules
}).plugin(store)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment