Skip to content

Commit

Permalink
Assign cart to customer as part of login mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
bookernath committed Dec 16, 2024
1 parent f6161c5 commit 72e39e2
Showing 1 changed file with 5 additions and 27 deletions.
32 changes: 5 additions & 27 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 Down Expand Up @@ -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;

Expand Down Expand Up @@ -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',
},
Expand Down

0 comments on commit 72e39e2

Please sign in to comment.