Skip to content

Commit 1c9b880

Browse files
authored
Assign cart to customer as part of login mutation (bigcommerce#1765)
1 parent f6161c5 commit 1c9b880

File tree

2 files changed

+10
-39
lines changed

2 files changed

+10
-39
lines changed

.changeset/proud-taxis-smoke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@bigcommerce/catalyst-core": patch
3+
---
4+
5+
Assign cart to customer as part of initial login mutation

core/auth.ts

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { client } from './client';
88
import { graphql } from './client/graphql';
99

1010
const LoginMutation = graphql(`
11-
mutation Login($email: String!, $password: String!) {
12-
login(email: $email, password: $password) {
11+
mutation Login($email: String!, $password: String!, $cartEntityId: String) {
12+
login(email: $email, password: $password, guestCartEntityId: $cartEntityId) {
1313
customerAccessToken {
1414
value
1515
}
@@ -23,18 +23,6 @@ const LoginMutation = graphql(`
2323
}
2424
`);
2525

26-
const AssignCartToCustomerMutation = graphql(`
27-
mutation AssignCartToCustomer($assignCartToCustomerInput: AssignCartToCustomerInput!) {
28-
cart {
29-
assignCartToCustomer(input: $assignCartToCustomerInput) {
30-
cart {
31-
entityId
32-
}
33-
}
34-
}
35-
}
36-
`);
37-
3826
const LogoutMutation = graphql(`
3927
mutation LogoutMutation {
4028
logout {
@@ -74,30 +62,6 @@ const config = {
7462
},
7563
},
7664
events: {
77-
async signIn({ user: { customerAccessToken } }) {
78-
const cookieStore = await cookies();
79-
const cookieCartId = cookieStore.get('cartId')?.value;
80-
81-
if (cookieCartId) {
82-
try {
83-
await client.fetch({
84-
document: AssignCartToCustomerMutation,
85-
variables: {
86-
assignCartToCustomerInput: {
87-
cartEntityId: cookieCartId,
88-
},
89-
},
90-
customerAccessToken,
91-
fetchOptions: {
92-
cache: 'no-store',
93-
},
94-
});
95-
} catch (error) {
96-
// eslint-disable-next-line no-console
97-
console.error(error);
98-
}
99-
}
100-
},
10165
async signOut(message) {
10266
const customerAccessToken = 'token' in message ? message.token?.customerAccessToken : null;
10367

@@ -126,10 +90,12 @@ const config = {
12690
},
12791
async authorize(credentials) {
12892
const { email, password } = Credentials.parse(credentials);
93+
const cookieStore = await cookies();
94+
const cartEntityId = cookieStore.get('cartId')?.value;
12995

13096
const response = await client.fetch({
13197
document: LoginMutation,
132-
variables: { email, password },
98+
variables: { email, password, cartEntityId },
13399
fetchOptions: {
134100
cache: 'no-store',
135101
},

0 commit comments

Comments
 (0)