Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/multiple redirections on my account refresh #2780

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- ESlint throwing errors about undefined jest globals in tests - @lukeromanowicz (#2702)
- Fixed changing the country when entering shipping address in checkout not updating shipping costs - @revlis-x (#2691)
- Infinite loop on multistore page after reload - @patzick (#2713)
- Refreshing MyAccount page on multistore - @patzick (#2780)
- "Toggle password visible" button in password fields works the right way - @lromanowicz (#2772)

## [1.9.0-rc.2] - 2019.04.10
Expand Down
4 changes: 2 additions & 2 deletions core/modules/user/hooks/afterRegistration.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Vue from 'vue'
import * as types from './../store/mutation-types'

export function afterRegistration({ Vue, config, store, isServer }){
export async function afterRegistration({ Vue, config, store, isServer }){
if (!isServer) {
store.dispatch('user/startSession')
await store.dispatch('user/startSession')

Vue.prototype.$bus.$on('user-before-logout', () => {
store.dispatch('user/logout', { silent: false })
Expand Down
15 changes: 4 additions & 11 deletions core/modules/user/router/beforeEach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ 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) {
export async function beforeEach(to: Route, from: Route, next) {
const requiresAuth = to.matched.some(route => route.meta.requiresAuth)
if (requiresAuth) {
if (isServer) {
next('/')
next()
} else {
await rootStore.dispatch('user/startSession')
if (!rootStore.getters['user/isLoggedIn']) {
next('/')
rootStore.dispatch('notification/spawnNotification', {
Expand All @@ -18,14 +18,7 @@ export function beforeEach(to: Route, from: Route, next) {
action1: { label: i18n.t('OK') }
})
} else {
if (!from.name) {
next('/')
setTimeout(()=> {
router.push(to.path)
}, 0)
} else {
next()
}
next()
}
}
} else {
Expand Down
13 changes: 7 additions & 6 deletions core/modules/user/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ 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 { currentStoreView } from '@vue-storefront/core/lib/multistore'
import { isServer } from '@vue-storefront/core/helpers'
// 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`);
async startSession (context) {
if (isServer || context.getters.isLocalSessionReaded) return
context.commit(types.USER_LOCAL_SESSION_READED, true)

const user = localStorage.getItem(`shop/user/current-user`);
if (user) {
context.commit(types.USER_INFO_LOADED, JSON.parse(user))
}
Expand Down Expand Up @@ -171,7 +172,7 @@ const actions: ActionTree<UserState, RootState> = {
/**
* Load current user profile
*/
me (context, { refresh = true, useCache = true }) {
me (context, { refresh = true, useCache = true } = {}) {
return new Promise((resolve, reject) => {
if (!context.state.token) {
Logger.warn('No User token, user unauthorized', 'user')()
Expand Down
1 change: 1 addition & 0 deletions core/modules/user/store/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const getters: GetterTree<UserState, RootState> = {
isLoggedIn (state) {
return state.current !== null
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be named like “local_session_started” or “local_session_loaded”

isLocalSessionReaded: state => state.local_session_readed,
getUserToken (state) {
return state.token
}
Expand Down
3 changes: 2 additions & 1 deletion core/modules/user/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const module: Module<UserState, RootState> = {
current: null,
current_storecode: '',
session_started: new Date(),
orders_history: null
orders_history: null,
local_session_readed: false
},
getters,
actions,
Expand Down
3 changes: 2 additions & 1 deletion core/modules/user/store/mutation-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export const USER_START_SESSION = SN_USER + '/START_SESSION'
export const USER_END_SESSION = SN_USER + '/END_SESSION'
export const USER_UPDATE_PREFERENCES = SN_USER + '/UPDATE_PREFERENCES'
export const USER_GROUP_TOKEN_CHANGED = SN_USER + '/GROUP_TOKEN_CHANGED'
export const USER_GROUP_CHANGED = SN_USER + 'GROUP_ID_CHANGED'
export const USER_GROUP_CHANGED = SN_USER + 'GROUP_ID_CHANGED'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/ - missing char in the mutation name

export const USER_LOCAL_SESSION_READED = SN_USER + '/LOCAL_SESSION_READED'
3 changes: 3 additions & 0 deletions core/modules/user/store/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const mutations: MutationTree<UserState> = {
state.token = ''
state.current = null
state.session_started = null
},
[types.USER_LOCAL_SESSION_READED] (state, readed = false) {
state.local_session_readed = readed
}
}

Expand Down
3 changes: 2 additions & 1 deletion core/modules/user/types/UserState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export default interface UserState {
} | null,
current_storecode: string,
session_started: Date,
orders_history: any
orders_history: any,
local_session_readed: boolean
}
8 changes: 6 additions & 2 deletions src/themes/default/pages/MyAccount.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
</nav>
</div>
<div class="col-md-9">
<component :is="this.$props.activeBlock" />
<no-ssr>
<component :is="this.$props.activeBlock" />
</no-ssr>
</div>
</div>
</div>
Expand All @@ -40,6 +42,7 @@ import MyNewsletter from '../components/core/blocks/MyAccount/MyNewsletter'
import MyOrders from '../components/core/blocks/MyAccount/MyOrders'
import MyOrder from '../components/core/blocks/MyAccount/MyOrder'
import MyRecentlyViewed from '../components/core/blocks/MyAccount/MyRecentlyViewed'
import NoSSR from 'vue-no-ssr'

export default {
data () {
Expand All @@ -62,7 +65,8 @@ export default {
MyNewsletter,
MyOrders,
MyOrder,
MyRecentlyViewed
MyRecentlyViewed,
'no-ssr': NoSSR
},
mixins: [MyAccount],
methods: {
Expand Down