Skip to content

Commit

Permalink
startSession action refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacper committed Feb 22, 2019
1 parent 6bece8d commit 6a81be5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
1 change: 0 additions & 1 deletion core/modules/user/hooks/afterRegistration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
2 changes: 1 addition & 1 deletion core/modules/user/router/beforeEach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 7 additions & 11 deletions core/modules/user/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import { currentStoreView } from '@vue-storefront/core/lib/multistore'

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 Expand Up @@ -44,17 +51,6 @@ const actions: ActionTree<UserState, RootState> = {
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
*/
Expand Down

0 comments on commit 6a81be5

Please sign in to comment.