Skip to content

Commit

Permalink
Assign cart to customer as part of login mutation (#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
bookernath authored Dec 17, 2024
1 parent f6161c5 commit 1c9b880
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-taxis-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": patch
---

Assign cart to customer as part of initial login mutation
44 changes: 5 additions & 39 deletions core/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { client } from './client';
import { graphql } from './client/graphql';

const LoginMutation = graphql(`
mutation Login($email: String!, $password: String!) {
login(email: $email, password: $password) {
mutation Login($email: String!, $password: String!, $cartEntityId: String) {
login(email: $email, password: $password, guestCartEntityId: $cartEntityId) {
customerAccessToken {
value
}
Expand All @@ -23,18 +23,6 @@ const LoginMutation = graphql(`
}
`);

const AssignCartToCustomerMutation = graphql(`
mutation AssignCartToCustomer($assignCartToCustomerInput: AssignCartToCustomerInput!) {
cart {
assignCartToCustomer(input: $assignCartToCustomerInput) {
cart {
entityId
}
}
}
}
`);

const LogoutMutation = graphql(`
mutation LogoutMutation {
logout {
Expand Down Expand Up @@ -74,30 +62,6 @@ const config = {
},
},
events: {
async signIn({ user: { customerAccessToken } }) {
const cookieStore = await cookies();
const cookieCartId = cookieStore.get('cartId')?.value;

if (cookieCartId) {
try {
await client.fetch({
document: AssignCartToCustomerMutation,
variables: {
assignCartToCustomerInput: {
cartEntityId: cookieCartId,
},
},
customerAccessToken,
fetchOptions: {
cache: 'no-store',
},
});
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
}
}
},
async signOut(message) {
const customerAccessToken = 'token' in message ? message.token?.customerAccessToken : null;

Expand Down Expand Up @@ -126,10 +90,12 @@ const config = {
},
async authorize(credentials) {
const { email, password } = Credentials.parse(credentials);
const cookieStore = await cookies();
const cartEntityId = cookieStore.get('cartId')?.value;

const response = await client.fetch({
document: LoginMutation,
variables: { email, password },
variables: { email, password, cartEntityId },
fetchOptions: {
cache: 'no-store',
},
Expand Down

0 comments on commit 1c9b880

Please sign in to comment.