Skip to content

Commit

Permalink
Fixed route guard ssr problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacper committed Feb 20, 2019
1 parent 269bc55 commit 085c839
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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)

## [1.8.2] - 2019.02.11
- Fixed docker-compose configuration for network_mode and TS build config - @lukeromanowicz (#2415)
Expand Down
1 change: 1 addition & 0 deletions core/modules/user/hooks/afterRegistration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as types from './../store/mutation-types'
export function afterRegistration({ Vue, config, store, isServer }){
if (!isServer) {
store.dispatch('user/startSession')
store.dispatch('user/loadUserEarly')

Vue.prototype.$bus.$on('user-before-logout', () => {
store.dispatch('user/logout', { silent: false })
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/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.matched.length) {
next('/')
setTimeout(()=> {
router.push(to.path)
}, 0)
} else {
next()
}
}
}
} else {
next()
Expand Down
14 changes: 13 additions & 1 deletion core/modules/user/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ 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> = {
Expand Down Expand Up @@ -43,6 +44,17 @@ const actions: ActionTree<UserState, RootState> = {
Vue.prototype.$bus.$emit('session-after-started')
})
},
/**
* Retrieve authenticated user from browser cache synchronously
*/
loadUserEarly (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))
}
},
/**
* Send password reset link for specific e-mail
*/
Expand Down

0 comments on commit 085c839

Please sign in to comment.