import VuexORM from '@vuex-orm/core' import { QUERY_TYPE } from '~/types/enums' const cookieparser = process.server ? require('cookieparser') : undefined export const plugins = [ VuexORM.install() ] export const actions = { /** * Méthode appelée lors de la phase d'initialisation de Nuxt coté serveur (SSR) * @param commit * @param dispatch * @param req * @return {Promise} */ async nuxtServerInit ({ dispatch }, { req }) { await dispatch('initCookies', { req }) await dispatch('getUserProfile') }, /** * Récupère et Store les Cookies Bearer et XAccessId * @param commit * @param req * @return {Promise} */ async initCookies ({ commit }, { req }) { if (req.headers.cookie) { const parsed = cookieparser.parse(req.headers.cookie) try { await commit('profile/access/setSwitchId', parsed.SwitchAccessId) } catch (err) {} await commit('profile/access/setBearer', parsed.BEARER) await commit('profile/access/setId', parsed.AccessId) } }, /** * Récupère les informations du profil connecté et update le Store Profile * @param dispatch * @param state * @return {Promise} */ async getUserProfile ({ dispatch }) { const myProfile = await this.app.context.$dataProvider.invoke({ type: QUERY_TYPE.DEFAULT, url: '/api/my_profile' }) await dispatch('profile/access/setProfile', myProfile.data) } }