index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import VuexORM from '@vuex-orm/core'
  2. const cookieparser = process.server ? require('cookieparser') : undefined
  3. export const plugins = [
  4. VuexORM.install()
  5. ];
  6. export const actions = {
  7. /**
  8. * Méthode appelée lors de la phase d'initialisation de Nuxt coté serveur (SSR)
  9. * @param commit
  10. * @param dispatch
  11. * @param req
  12. * @return {Promise<void>}
  13. */
  14. async nuxtServerInit({dispatch}, {req}) {
  15. const initCookie = await dispatch('initCookies', {req: req})
  16. const updateProfile = await dispatch('updateProfile')
  17. },
  18. /**
  19. * Récupère et Store les Cookies Bearer et XAccessId
  20. * @param commit
  21. * @param req
  22. * @return {Promise<void>}
  23. */
  24. async initCookies({commit}, {req}){
  25. let bearer = null
  26. let accessId = null
  27. if (req.headers.cookie) {
  28. const parsed = cookieparser.parse(req.headers.cookie)
  29. try {
  30. bearer = parsed.BEARER
  31. } catch (err) {
  32. // No valid cookie found
  33. }
  34. try {
  35. accessId = parsed.AccessId
  36. } catch (err) {
  37. // No valid Access Id found
  38. }
  39. }
  40. commit('profile/access/setBearer', bearer)
  41. commit('profile/access/setAccessId', accessId)
  42. },
  43. /**
  44. * Récupère les informations du profile connecté et update le Store Profile
  45. * @param dispatch
  46. * @param state
  47. * @return {Promise<void>}
  48. */
  49. async updateProfile({dispatch, state}) {
  50. const my_profile = await this.$http.$get(`/api/my_profile/${state.profile.access.accessId}`)
  51. dispatch('profile/access/setProfile', my_profile)
  52. },
  53. }