From 72e39e254b00f170c4812e04d908bc8310583368 Mon Sep 17 00:00:00 2001 From: bookernath Date: Mon, 16 Dec 2024 16:15:13 -0600 Subject: [PATCH] Assign cart to customer as part of login mutation --- core/auth.ts | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/core/auth.ts b/core/auth.ts index 708933de8..2f68a8f91 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 } @@ -74,30 +74,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 +102,12 @@ const config = { }, async authorize(credentials) { const { email, password } = Credentials.parse(credentials); + const cookieStore = await cookies(); + const cookieCartId = cookieStore.get('cartId')?.value; const response = await client.fetch({ document: LoginMutation, - variables: { email, password }, + variables: { email, password, cartEntityId: cookieCartId }, fetchOptions: { cache: 'no-store', },