From 1c9b8804cec99f5fd9700b422a3fb9739a850045 Mon Sep 17 00:00:00 2001 From: Nathan Booker Date: Tue, 17 Dec 2024 13:28:36 -0600 Subject: [PATCH] Assign cart to customer as part of login mutation (#1765) --- .changeset/proud-taxis-smoke.md | 5 ++++ core/auth.ts | 44 ++++----------------------------- 2 files changed, 10 insertions(+), 39 deletions(-) create mode 100644 .changeset/proud-taxis-smoke.md diff --git a/.changeset/proud-taxis-smoke.md b/.changeset/proud-taxis-smoke.md new file mode 100644 index 0000000000..a8058df40c --- /dev/null +++ b/.changeset/proud-taxis-smoke.md @@ -0,0 +1,5 @@ +--- +"@bigcommerce/catalyst-core": patch +--- + +Assign cart to customer as part of initial login mutation diff --git a/core/auth.ts b/core/auth.ts index 708933de8a..f7fc42a4a1 100644 --- a/core/auth.ts +++ b/core/auth.ts @@ -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 } @@ -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 { @@ -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; @@ -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', },