Skip to content

Commit

Permalink
Merge pull request #2466 from vue-kacper/Bugfix/2364
Browse files Browse the repository at this point in the history
Fixed route guard ssr problem
  • Loading branch information
filrak committed Feb 23, 2019
2 parents 2626f9e + 9cf3586 commit b85efed
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 20 additions & 8 deletions core/modules/user/router/beforeEach.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
10 changes: 9 additions & 1 deletion core/modules/user/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserState, RootState> = {
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) => {
Expand Down

0 comments on commit b85efed

Please sign in to comment.