Skip to content

Commit e3abd87

Browse files
authored
Fire login event when logged in customer changes (#1019)
1 parent 45080ca commit e3abd87

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

resources/js/stores/useCart.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { StorageSerializers, asyncComputed, useLocalStorage, useMemoize } from '@vueuse/core'
2-
import { computed, ref, watch } from 'vue'
2+
import { computed, watch } from 'vue'
33
import { GraphQLError } from '../fetch'
44
import { mask, clearMask } from './useMask'
55
import { user } from './useUser'
@@ -110,6 +110,14 @@ export const fetchCart = async function () {
110110
await fetchGuestCart()
111111
}
112112

113+
export const loggedIn = async function () {
114+
if (mask.value) {
115+
await linkUserToCart()
116+
} else {
117+
await fetchCustomerCart()
118+
}
119+
}
120+
113121
export const fetchAttributeValues = async function (attributes = []) {
114122
if (!attributes.length) {
115123
return { data: { customAttributeMetadataV2: { items: null } } }

resources/js/stores/useUser.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useLocalStorage, useSessionStorage, StorageSerializers } from '@vueuse/core'
22
import { useCookies } from '@vueuse/integrations/useCookies'
3-
import { clear as clearCart, fetchCustomerCart, linkUserToCart, cart } from './useCart'
3+
import { clear as clearCart, loggedIn as cartLoginHandler, cart } from './useCart'
44
import { clear as clearOrder } from './useOrder'
55
import { computed, watch } from 'vue'
66
import Jwt from '../jwt'
@@ -61,7 +61,11 @@ export const refresh = async function () {
6161

6262
return (currentRefresh = (async function () {
6363
try {
64+
const oldEmail = userStorage.value?.email
6465
userStorage.value = (await magentoGraphQL(`{ customer { ${config.queries.customer} } }`))?.data?.customer
66+
if (oldEmail !== userStorage.value?.email) {
67+
await loggedIn()
68+
}
6569
} catch (error) {
6670
if (error instanceof SessionExpired) {
6771
await clear()
@@ -141,14 +145,12 @@ export const login = async function (email, password) {
141145

142146
export const loginByToken = async function (customerToken) {
143147
token.value = customerToken
148+
await refresh()
149+
}
144150

145-
if (mask.value) {
146-
await linkUserToCart()
147-
} else {
148-
await fetchCustomerCart()
149-
}
150-
151+
export const loggedIn = async function () {
151152
window.app.$emit('logged-in')
153+
await cartLoginHandler()
152154
}
153155

154156
export const logout = async function () {

0 commit comments

Comments
 (0)