Created
May 18, 2020 18:07
-
-
Save codenamezjames/b5b2033747c0946b324fcbcb9961bffd to your computer and use it in GitHub Desktop.
Vuex persist ssr auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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