To persist Vuex state on page refresh, we can use the vuex-persistedstate package. import { Store } from "vuex"; import createPersistedState from "vuex-persistedstate"; import * as Cookies from "js-cookie"; const store = new Store({ // ...
Vuex stores are reactive. When Vue components retrieve state from it, they will reactively and efficiently update if the store's state changes. You cannot directly mutate the store's state. The only way to change a store's state is by explicitly committing mutations.
This Vuex plugin allows you to sync and share the status of your Vue application across multiple tabs or windows using the local storage.
Vuex mutations are very similar to events: each mutation has a string type and a handler. The handler function is where we perform actual state modifications, and it will receive the state as the first argument: const store = createStore({ state: { count: 1 }, mutations: { increment (state) { // mutate state state.
This is a known use case. There are different solutions.
For example, one can use vuex-persistedstate
. This is a plugin for vuex
to handle and store state between page refreshes.
Sample code:
import { Store } from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import * as Cookies from 'js-cookie'
const store = new Store({
// ...
plugins: [
createPersistedState({
getState: (key) => Cookies.getJSON(key),
setState: (key, state) => Cookies.set(key, state, { expires: 3, secure: true })
})
]
})
What we do here is simple:
js-cookie
getState
we try to load saved state from Cookies
setState
we save our state to Cookies
Docs and installation instructions: https://www.npmjs.com/package/vuex-persistedstate
When creating your VueX state, save it to session storage using the vuex-persistedstate plugin. In this way, the information will be lost when the browser is closed. Avoid use of cookies as these values will travel between client and server.
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex);
export default new Vuex.Store({
plugins: [createPersistedState({
storage: window.sessionStorage,
})],
state: {
//....
}
});
Use sessionStorage.clear();
when user logs out manually.
Vuex state is kept in memory. Page load will purge this current state. This is why the state does not persist on reload.
But the vuex-persistedstate
plugin solves this issue
npm install --save vuex-persistedstate
Now import this into the store.
import Vue from 'vue'
import Vuex from 'vuex'
import account from './modules/account'
import createPersistedState from "vuex-persistedstate";
Vue.use(Vuex);
const store = new Vuex.Store({
modules: {
account,
},
plugins: [createPersistedState()]
});
It worked perfectly with a single line of code: plugins: [createPersistedState()]
I think use cookies/localStorage to save login status might cause some error in some situation.
Firebase already record login information at localStorage for us include expirationTime and refreshToken.
Therefore I will use Vue created hook and Firebase api to check login status.
If token was expired, the api will refresh token for us.
So we can make sure the login status display in our app is equal to Firebase.
new Vue({
el: '#app',
created() {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
log('User is logined');
// update data or vuex state
} else {
log('User is not logged in.');
}
});
},
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With