From 6a81be54dc088654b717d0cc624a90acf487a489 Mon Sep 17 00:00:00 2001 From: Kacper Date: Fri, 22 Feb 2019 21:52:17 +0100 Subject: [PATCH] startSession action refactor --- core/modules/user/hooks/afterRegistration.ts | 1 - core/modules/user/router/beforeEach.ts | 2 +- core/modules/user/store/actions.ts | 18 +++++++----------- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/core/modules/user/hooks/afterRegistration.ts b/core/modules/user/hooks/afterRegistration.ts index 8406cc85d7..465db31f58 100644 --- a/core/modules/user/hooks/afterRegistration.ts +++ b/core/modules/user/hooks/afterRegistration.ts @@ -4,7 +4,6 @@ import * as types from './../store/mutation-types' export function afterRegistration({ Vue, config, store, isServer }){ if (!isServer) { store.dispatch('user/startSession') - store.dispatch('user/loadFromCache') Vue.prototype.$bus.$on('user-before-logout', () => { store.dispatch('user/logout', { silent: false }) diff --git a/core/modules/user/router/beforeEach.ts b/core/modules/user/router/beforeEach.ts index b55273f610..ba810984e9 100644 --- a/core/modules/user/router/beforeEach.ts +++ b/core/modules/user/router/beforeEach.ts @@ -18,7 +18,7 @@ export function beforeEach(to: Route, from: Route, next) { action1: { label: i18n.t('OK') } }) } else { - if (!from.matched.length) { + if (!from.name) { next('/') setTimeout(()=> { router.push(to.path) diff --git a/core/modules/user/store/actions.ts b/core/modules/user/store/actions.ts index fd94e13392..4acd1db129 100644 --- a/core/modules/user/store/actions.ts +++ b/core/modules/user/store/actions.ts @@ -14,6 +14,13 @@ import { currentStoreView } from '@vue-storefront/core/lib/multistore' 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) => { @@ -44,17 +51,6 @@ const actions: ActionTree = { Vue.prototype.$bus.$emit('session-after-started') }) }, - /** - * Retrieve authenticated user from browser cache synchronously - */ - loadFromCache (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 */