Skip to content

Commit

Permalink
refactor: use string for user id
Browse files Browse the repository at this point in the history
  • Loading branch information
deini committed Apr 8, 2024
1 parent 0cc399a commit a7424ad
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 29 deletions.
7 changes: 7 additions & 0 deletions .changeset/short-cycles-learn.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { useRouter } from '~/navigation';
import { submitChangePasswordForm } from '../_actions/submit-change-password-form';

interface Props {
customerId: number;
customerId: string;
customerToken: string;
}

Expand Down
2 changes: 1 addition & 1 deletion apps/core/app/[locale]/(default)/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default async function Login({ params: { locale }, searchParams }: Props)
<div className="mx-auto my-6 max-w-4xl">
<h2 className="mb-8 text-4xl font-black lg:text-5xl">{t('changePasswordHeading')}</h2>
<NextIntlClientProvider locale={locale} messages={{ Account }}>
<ChangePasswordForm customerId={Number(customerId)} customerToken={customerToken} />
<ChangePasswordForm customerId={customerId} customerToken={customerToken} />
</NextIntlClientProvider>
</div>
);
Expand Down
26 changes: 7 additions & 19 deletions apps/core/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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;
},
Expand All @@ -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) {
Expand Down Expand Up @@ -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 };
2 changes: 1 addition & 1 deletion apps/core/client/mutations/apply-checkout-coupon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const applyCheckoutCoupon = async (checkoutEntityId: string, couponCode:
},
},
},
customerId: Number(customerId),
customerId,
fetchOptions: { cache: 'no-store' },
});

Expand Down
2 changes: 1 addition & 1 deletion apps/core/client/mutations/assign-cart-to-customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const assignCartToCustomer = async (customerId: string, cartEntityId: Car
cartEntityId,
},
},
customerId: Number(customerId),
customerId,
fetchOptions: { cache: 'no-store' },
});

Expand Down
2 changes: 1 addition & 1 deletion apps/core/client/mutations/unapply-checkout-coupon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const unapplyCheckoutCoupon = async (checkoutEntityId: string, couponCode
},
},
},
customerId: Number(customerId),
customerId,
fetchOptions: { cache: 'no-store' },
});

Expand Down
2 changes: 1 addition & 1 deletion apps/core/client/mutations/unassign-cart-from-customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const unassignCartFromCustomer = async (
cartEntityId,
},
},
customerId: Number(customerId),
customerId,
fetchOptions: { cache: 'no-store' },
});

Expand Down
8 changes: 4 additions & 4 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ class Client<FetcherRequestInit extends RequestInit = RequestInit> {
async fetch<TResult, TVariables extends Record<string, unknown>>(config: {
document: DocumentDecoration<TResult, TVariables>;
variables: TVariables;
customerId?: number;
customerId?: string;
fetchOptions?: FetcherRequestInit;
}): Promise<BigCommerceResponse<TResult>>;

// Overload for documents that do not require variables
async fetch<TResult>(config: {
document: DocumentDecoration<TResult, Record<string, never>>;
variables?: undefined;
customerId?: number;
customerId?: string;
fetchOptions?: FetcherRequestInit;
}): Promise<BigCommerceResponse<TResult>>;

Expand All @@ -57,7 +57,7 @@ class Client<FetcherRequestInit extends RequestInit = RequestInit> {
}: {
document: DocumentDecoration<TResult, TVariables>;
variables?: TVariables;
customerId?: number;
customerId?: string;
fetchOptions?: FetcherRequestInit;
}): Promise<BigCommerceResponse<TResult>> {
const { cache, headers = {}, ...rest } = fetchOptions;
Expand All @@ -70,7 +70,7 @@ class Client<FetcherRequestInit extends RequestInit = RequestInit> {
'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({
Expand Down

0 comments on commit a7424ad

Please sign in to comment.