diff --git a/CHANGELOG.md b/CHANGELOG.md index c39e46c4c1..143247a262 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Updated to Vue 2.6.6 - @filrak (#2456) - Null sidebar menu data on static page fixed - @filrak (#2456) - Fix cannot edit previous steps in checkout - @filrak (#2457) +- Fixed route guard ssr problem - @vue-kacper (#2364) - Fix links in footer to static pages bug - @filrak (#2464) - Improve images loading on category page, corrected alt view and blinking problem - @patzick (#2465) - Improve tsconfig for better IDE paths support - @patzick, @filrak (#2474) diff --git a/core/modules/user/router/beforeEach.ts b/core/modules/user/router/beforeEach.ts index 272c7be561..49f8bb681f 100644 --- a/core/modules/user/router/beforeEach.ts +++ b/core/modules/user/router/beforeEach.ts @@ -1,20 +1,32 @@ import { Route } from 'vue-router' import rootStore from '@vue-storefront/core/store' import i18n from '@vue-storefront/i18n' +import { isServer } from '@vue-storefront/core/helpers' +import { router } from '@vue-storefront/core/app' export function beforeEach(to: Route, from: Route, next) { const requiresAuth = to.matched.some(route => route.meta.requiresAuth) - if (requiresAuth) { - if (!rootStore.getters['user/isLoggedIn']) { + if (isServer) { next('/') - rootStore.dispatch('notification/spawnNotification', { - type: 'error', - message: i18n.t('You need to be logged in to see this page'), - action1: { label: i18n.t('OK') } - }) } else { - next() + if (!rootStore.getters['user/isLoggedIn']) { + next('/') + rootStore.dispatch('notification/spawnNotification', { + type: 'error', + message: i18n.t('You need to be logged in to see this page'), + action1: { label: i18n.t('OK') } + }) + } else { + if (!from.name) { + next('/') + setTimeout(()=> { + router.push(to.path) + }, 0) + } else { + next() + } + } } } else { next() diff --git a/core/modules/user/store/actions.ts b/core/modules/user/store/actions.ts index 62b5bb34cd..56a9c7da4d 100644 --- a/core/modules/user/store/actions.ts +++ b/core/modules/user/store/actions.ts @@ -8,11 +8,19 @@ import RootState from '@vue-storefront/core/types/RootState' import UserState from '../types/UserState' import { Logger } from '@vue-storefront/core/lib/logger' import { TaskQueue } from '@vue-storefront/core/lib/sync' -import { UserProfile } from '../types/UserProfile'; +import { UserProfile } from '../types/UserProfile' +import { currentStoreView } from '@vue-storefront/core/lib/multistore' // import router from '@vue-storefront/core/router' const actions: ActionTree = { startSession (context) { + const storeView = currentStoreView() + const dbNamePrefix = storeView.storeCode ? storeView.storeCode + '-' : '' + const user = localStorage.getItem(`${dbNamePrefix}shop/user/current-user`); + if (user) { + context.commit(types.USER_INFO_LOADED, JSON.parse(user)) + } + context.commit(types.USER_START_SESSION) const cache = Vue.prototype.$db.usersCollection cache.getItem('current-token', (err, res) => {