From a7424ad91e90188c91f3db5183f45dbbdb23659a Mon Sep 17 00:00:00 2001 From: Daniel Almaguer Date: Mon, 8 Apr 2024 11:41:04 -0500 Subject: [PATCH] refactor: use string for user id --- .changeset/short-cycles-learn.md | 7 +++++ .../_components/change-password-form.tsx | 2 +- .../app/[locale]/(default)/login/page.tsx | 2 +- apps/core/auth.ts | 26 +++++-------------- .../client/mutations/apply-checkout-coupon.ts | 2 +- .../mutations/assign-cart-to-customer.ts | 2 +- .../mutations/unapply-checkout-coupon.ts | 2 +- .../mutations/unassign-cart-from-customer.ts | 2 +- packages/client/src/client.ts | 8 +++--- 9 files changed, 24 insertions(+), 29 deletions(-) create mode 100644 .changeset/short-cycles-learn.md diff --git a/.changeset/short-cycles-learn.md b/.changeset/short-cycles-learn.md new file mode 100644 index 000000000..5771d6688 --- /dev/null +++ b/.changeset/short-cycles-learn.md @@ -0,0 +1,7 @@ +--- +"@bigcommerce/catalyst-client": patch +"@bigcommerce/catalyst-core": patch +"@bigcommerce/create-catalyst": patch +--- + +chore: bump next-auth and use string for user id diff --git a/apps/core/app/[locale]/(default)/login/_components/change-password-form.tsx b/apps/core/app/[locale]/(default)/login/_components/change-password-form.tsx index 61977785d..e6bcae97f 100644 --- a/apps/core/app/[locale]/(default)/login/_components/change-password-form.tsx +++ b/apps/core/app/[locale]/(default)/login/_components/change-password-form.tsx @@ -21,7 +21,7 @@ import { useRouter } from '~/navigation'; import { submitChangePasswordForm } from '../_actions/submit-change-password-form'; interface Props { - customerId: number; + customerId: string; customerToken: string; } diff --git a/apps/core/app/[locale]/(default)/login/page.tsx b/apps/core/app/[locale]/(default)/login/page.tsx index 6d260fc70..f7391dc48 100644 --- a/apps/core/app/[locale]/(default)/login/page.tsx +++ b/apps/core/app/[locale]/(default)/login/page.tsx @@ -39,7 +39,7 @@ export default async function Login({ params: { locale }, searchParams }: Props)

{t('changePasswordHeading')}

- +
); diff --git a/apps/core/auth.ts b/apps/core/auth.ts index 8a23e5361..8132a572f 100644 --- a/apps/core/auth.ts +++ b/apps/core/auth.ts @@ -12,14 +12,6 @@ export const Credentials = z.object({ password: z.string().min(1), }); -declare module 'next-auth' { - interface Session { - user?: { - id?: number; - }; - } -} - const config = { session: { strategy: 'jwt', @@ -29,9 +21,9 @@ const config = { }, callbacks: { session({ session, token }) { - session.user ||= {}; - - session.user.id = token.sub ? parseInt(token.sub, 10) : undefined; + if (token.sub) { + session.user.id = token.sub; + } return session; }, @@ -40,7 +32,7 @@ const config = { async signIn({ user }) { const cookieCartId = cookies().get('cartId')?.value; - if (cookieCartId) { + if (cookieCartId && user.id) { try { await assignCartToCustomer(user.id, cookieCartId); } catch (error) { @@ -85,20 +77,16 @@ const config = { ], } satisfies NextAuthConfig; -const { handlers, auth, signIn, signOut, update } = NextAuth(config); +const { handlers, auth, signIn, signOut } = NextAuth(config); const getSessionCustomerId = async () => { try { const session = await auth(); - if (!session?.user?.id) { - return; - } - - return session.user.id; + return session?.user?.id; } catch { // No empty } }; -export { handlers, auth, signIn, signOut, update, getSessionCustomerId }; +export { handlers, auth, signIn, signOut, getSessionCustomerId }; diff --git a/apps/core/client/mutations/apply-checkout-coupon.ts b/apps/core/client/mutations/apply-checkout-coupon.ts index dfcd96d03..e54cfbab7 100644 --- a/apps/core/client/mutations/apply-checkout-coupon.ts +++ b/apps/core/client/mutations/apply-checkout-coupon.ts @@ -28,7 +28,7 @@ export const applyCheckoutCoupon = async (checkoutEntityId: string, couponCode: }, }, }, - customerId: Number(customerId), + customerId, fetchOptions: { cache: 'no-store' }, }); diff --git a/apps/core/client/mutations/assign-cart-to-customer.ts b/apps/core/client/mutations/assign-cart-to-customer.ts index 48fd882d3..70f3c4dd8 100644 --- a/apps/core/client/mutations/assign-cart-to-customer.ts +++ b/apps/core/client/mutations/assign-cart-to-customer.ts @@ -24,7 +24,7 @@ export const assignCartToCustomer = async (customerId: string, cartEntityId: Car cartEntityId, }, }, - customerId: Number(customerId), + customerId, fetchOptions: { cache: 'no-store' }, }); diff --git a/apps/core/client/mutations/unapply-checkout-coupon.ts b/apps/core/client/mutations/unapply-checkout-coupon.ts index 0f192f561..ba84b143d 100644 --- a/apps/core/client/mutations/unapply-checkout-coupon.ts +++ b/apps/core/client/mutations/unapply-checkout-coupon.ts @@ -28,7 +28,7 @@ export const unapplyCheckoutCoupon = async (checkoutEntityId: string, couponCode }, }, }, - customerId: Number(customerId), + customerId, fetchOptions: { cache: 'no-store' }, }); diff --git a/apps/core/client/mutations/unassign-cart-from-customer.ts b/apps/core/client/mutations/unassign-cart-from-customer.ts index 15729eba3..34d121478 100644 --- a/apps/core/client/mutations/unassign-cart-from-customer.ts +++ b/apps/core/client/mutations/unassign-cart-from-customer.ts @@ -32,7 +32,7 @@ export const unassignCartFromCustomer = async ( cartEntityId, }, }, - customerId: Number(customerId), + customerId, fetchOptions: { cache: 'no-store' }, }); diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index cd906dfe0..4a868e332 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -37,7 +37,7 @@ class Client { async fetch>(config: { document: DocumentDecoration; variables: TVariables; - customerId?: number; + customerId?: string; fetchOptions?: FetcherRequestInit; }): Promise>; @@ -45,7 +45,7 @@ class Client { async fetch(config: { document: DocumentDecoration>; variables?: undefined; - customerId?: number; + customerId?: string; fetchOptions?: FetcherRequestInit; }): Promise>; @@ -57,7 +57,7 @@ class Client { }: { document: DocumentDecoration; variables?: TVariables; - customerId?: number; + customerId?: string; fetchOptions?: FetcherRequestInit; }): Promise> { const { cache, headers = {}, ...rest } = fetchOptions; @@ -70,7 +70,7 @@ class Client { 'Content-Type': 'application/json', Authorization: `Bearer ${this.config.customerImpersonationToken}`, 'User-Agent': this.backendUserAgent, - ...(customerId && { 'X-Bc-Customer-Id': String(customerId) }), + ...(customerId && { 'X-Bc-Customer-Id': customerId }), ...headers, }, body: JSON.stringify({