Skip to content

Commit 811bb55

Browse files
wcole1-godaddyampcode-compbennett1-godaddy
authored
refactor: improve auth handling and type safety in godaddy hooks (#1209)
* Add auth context and update API calls to use authentication Amp-Thread-ID: https://ampcode.com/threads/T-4a30d115-a4ed-4fb6-94c8-a93f2875611d Co-authored-by: Amp <amp@ampcode.com> * refactor: improve auth handling and type safety in godaddy hooks - Remove blocking early returns that prevented JWT-only auth flows - Add explicit type generics to all useMutation calls - Simplify auth pattern to match godaddy.ts overload structure - Use direct ternary for jwt vs session auth selection - Remove unnecessary wrapper functions for cleaner code - Let React Query handle error throwing automatically Amp-Thread-ID: https://ampcode.com/threads/T-2469796c-8d8e-4450-be09-b8dfe01f101a Co-authored-by: Amp <amp@ampcode.com> * Fix JWT session scoping to prevent reuse across different checkout sessions Amp-Thread-ID: https://ampcode.com/threads/T-65c39e0f-21b4-4900-a6a6-35c262cb3ec0 Co-authored-by: Amp <amp@ampcode.com> * Add legacy flow fallback when JWT exchange fails Amp-Thread-ID: https://ampcode.com/threads/T-65c39e0f-21b4-4900-a6a6-35c262cb3ec0 Co-authored-by: Amp <amp@ampcode.com> * Fix JWT type to use undefined instead of null for consistency Amp-Thread-ID: https://ampcode.com/threads/T-65c39e0f-21b4-4900-a6a6-35c262cb3ec0 Co-authored-by: Amp <amp@ampcode.com> * chore: format code with consistent style - Convert double quotes to single quotes - Standardize indentation (2 spaces) - Apply formatting across React components and utilities Amp-Thread-ID: https://ampcode.com/threads/T-1d878f81-bcc4-4acd-a1d8-11f31b88ecee Co-authored-by: Amp <amp@ampcode.com> * fix: handle exchangeFailed state correctly when token exchange fails - Return early after clearing mismatched JWT to avoid race condition - Check for missing JWT before cancelled flag to ensure failure is captured - Guard exchangeFailed state updates with cancelled check to prevent updates after unmount Amp-Thread-ID: https://ampcode.com/threads/T-77963656-ab13-4782-b5fb-efcf2be4696a Co-authored-by: Amp <amp@ampcode.com> * Fix the draft order query * Format * Format changes * update types * use GODADDY_API_HOST to derive all env states * remove unused getEnvVar import * fix: set isLoading to false when JWT already exists for session Amp-Thread-ID: https://ampcode.com/threads/T-36b2320b-3465-4c05-9b20-dc79416c2e8a Co-authored-by: Amp <amp@ampcode.com> * update fulfillment logic for delivery method * Add sessionId to confirmCheckout auth overload Amp-Thread-ID: https://ampcode.com/threads/T-d1b8d98c-ded2-42d5-b59b-25e20b4d1f02 Co-authored-by: Amp <amp@ampcode.com> * add back redirect if order not found * Format files * Update order of accessing session * fix create session input types and fix delivery method logic * add appearance data to checkout session query * modify viewable sections * add changeset for version change --------- Co-authored-by: Amp <amp@ampcode.com> Co-authored-by: Phil Bennett <pbennett1@godaddy.com>
1 parent 141f287 commit 811bb55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+10824
-8864
lines changed

.changeset/loose-taxis-grab.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@godaddy/react": patch
3+
---
4+
5+
Add auth-token exchange
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use client';
2+
3+
import { Checkout, GoDaddyProvider } from '@godaddy/react';
4+
5+
export default function HostedCheckout() {
6+
return (
7+
<div>
8+
<GoDaddyProvider apiHost={process.env.NEXT_PUBLIC_GODADDY_API_HOST}>
9+
<Checkout
10+
godaddyPaymentsConfig={{
11+
businessId: process.env.NEXT_PUBLIC_GODADDY_BUSINESS_ID || '',
12+
appId: process.env.NEXT_PUBLIC_GODADDY_APP_ID || '',
13+
}}
14+
/>
15+
</GoDaddyProvider>
16+
</div>
17+
);
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import HostedCheckout from './HostedCheckout';
2+
3+
export const dynamic = 'force-dynamic';
4+
5+
export default async function HostedCheckoutPage() {
6+
return <HostedCheckout />;
7+
}

examples/nextjs/app/c/page.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const dynamic = 'force-dynamic';
2+
3+
export default async function HostedCheckout() {
4+
return <div>Test</div>;
5+
}

examples/nextjs/app/checkout.tsx

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import type { CheckoutFormSchema, CheckoutSession } from '@godaddy/react';
44
import { Checkout, GoDaddyProvider } from '@godaddy/react';
5-
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
65
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
7-
import { useState } from 'react';
86
import { z } from 'zod';
97

108
/* Override the checkout form schema to make shippingPhone required */
@@ -13,32 +11,27 @@ const customSchema: CheckoutFormSchema = {
1311
};
1412

1513
export function CheckoutPage({ session }: { session: CheckoutSession }) {
16-
const [queryClient] = useState(() => new QueryClient());
17-
1814
return (
19-
<QueryClientProvider client={queryClient}>
20-
<GoDaddyProvider>
21-
<Checkout
22-
session={session}
23-
checkoutFormSchema={customSchema}
24-
squareConfig={{
25-
appId: process.env.NEXT_PUBLIC_SQUARE_APP_ID || '',
26-
locationId: process.env.NEXT_PUBLIC_SQUARE_LOCATION_ID || '',
27-
}}
28-
stripeConfig={{
29-
publishableKey:
30-
process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY || '',
31-
}}
32-
godaddyPaymentsConfig={{
33-
businessId: process.env.NEXT_PUBLIC_GODADDY_BUSINESS_ID || '',
34-
appId: process.env.NEXT_PUBLIC_GODADDY_APP_ID || '',
35-
}}
36-
paypalConfig={{
37-
clientId: process.env.NEXT_PUBLIC_PAYPAL_CLIENT_ID || '',
38-
}}
39-
/>
40-
<ReactQueryDevtools initialIsOpen={false} />
41-
</GoDaddyProvider>
42-
</QueryClientProvider>
15+
<GoDaddyProvider apiHost={process.env.NEXT_PUBLIC_GODADDY_API_HOST}>
16+
<Checkout
17+
session={session}
18+
checkoutFormSchema={customSchema}
19+
squareConfig={{
20+
appId: process.env.NEXT_PUBLIC_SQUARE_APP_ID || '',
21+
locationId: process.env.NEXT_PUBLIC_SQUARE_LOCATION_ID || '',
22+
}}
23+
stripeConfig={{
24+
publishableKey: process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY || '',
25+
}}
26+
godaddyPaymentsConfig={{
27+
businessId: process.env.NEXT_PUBLIC_GODADDY_BUSINESS_ID || '',
28+
appId: process.env.NEXT_PUBLIC_GODADDY_APP_ID || '',
29+
}}
30+
paypalConfig={{
31+
clientId: process.env.NEXT_PUBLIC_PAYPAL_CLIENT_ID || '',
32+
}}
33+
/>
34+
<ReactQueryDevtools initialIsOpen={false} />
35+
</GoDaddyProvider>
4336
);
4437
}

examples/nextjs/app/layout.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Metadata } from 'next';
22
import { Geist, Geist_Mono } from 'next/font/google';
33
import './globals.css';
44
import '@godaddy/react/styles.css';
5+
import { Providers } from './providers';
56

67
const geistSans = Geist({
78
variable: '--font-geist-sans',
@@ -28,7 +29,7 @@ export default function RootLayout({
2829
<body
2930
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
3031
>
31-
{children}
32+
<Providers>{children}</Providers>
3233
</body>
3334
</html>
3435
);

examples/nextjs/app/providers.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use client';
2+
3+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
4+
import { useState } from 'react';
5+
6+
export function Providers({ children }: { children: React.ReactNode }) {
7+
const [queryClient] = useState(() => new QueryClient());
8+
9+
return (
10+
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
11+
);
12+
}

examples/nextjs/biome.json

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
{
2-
"$schema": "https://biomejs.dev/schemas/2.3.2/schema.json",
3-
"extends": ["biome-config-godaddy/biome.json"],
4-
"css": {
5-
"parser": {
6-
"cssModules": true,
7-
"tailwindDirectives": true
8-
}
9-
},
10-
"files": {
11-
"includes": ["**/*", "!!**/src/globals.css"]
12-
},
13-
"linter": {
14-
"rules": {
15-
"correctness": {
16-
"useUniqueElementIds": "off"
17-
}
18-
}
19-
}
2+
"$schema": "https://biomejs.dev/schemas/2.3.3/schema.json",
3+
"extends": ["biome-config-godaddy/biome.json"],
4+
"css": {
5+
"parser": {
6+
"cssModules": true,
7+
"tailwindDirectives": true
8+
}
9+
},
10+
"files": {
11+
"includes": ["**/*", "!!**/src/globals.css"]
12+
},
13+
"linter": {
14+
"rules": {
15+
"correctness": {
16+
"useUniqueElementIds": "off"
17+
}
18+
}
19+
}
2020
}

examples/nextjs/package.json

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
{
2-
"name": "nextjs",
3-
"version": "0.1.1",
4-
"private": true,
5-
"scripts": {
6-
"dev": "next dev",
7-
"build": "next build",
8-
"start": "next start",
9-
"lint": "biome check .",
10-
"lint:fix": "biome check --write --unsafe ."
11-
},
12-
"dependencies": {
13-
"@godaddy/localizations": "workspace:*",
14-
"@godaddy/react": "workspace:*",
15-
"@tanstack/react-query": "^5.66.0",
16-
"@tanstack/react-query-devtools": "^5.76.1",
17-
"next": "16.0.1",
18-
"react": "19.2.0",
19-
"react-dom": "19.2.0",
20-
"zod": "^3.24.1"
21-
},
22-
"devDependencies": {
23-
"@biomejs/biome": "^2",
24-
"@tailwindcss/postcss": "^4",
25-
"@types/node": "^20",
26-
"@types/react": "^19",
27-
"@types/react-dom": "^19",
28-
"biome-config-godaddy": "workspace:*",
29-
"tailwindcss": "^4",
30-
"typescript": "^5"
31-
}
2+
"name": "nextjs",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "next dev",
7+
"build": "next build",
8+
"start": "next start",
9+
"lint": "biome check .",
10+
"lint:fix": "biome check --write --unsafe ."
11+
},
12+
"dependencies": {
13+
"@godaddy/localizations": "workspace:*",
14+
"@godaddy/react": "workspace:*",
15+
"@tanstack/react-query": "^5.66.0",
16+
"@tanstack/react-query-devtools": "^5.76.1",
17+
"next": "16.0.1",
18+
"react": "19.2.0",
19+
"react-dom": "19.2.0",
20+
"zod": "^3.24.1"
21+
},
22+
"devDependencies": {
23+
"@biomejs/biome": "^2",
24+
"@tailwindcss/postcss": "^4",
25+
"@types/node": "^20",
26+
"@types/react": "^19",
27+
"@types/react-dom": "^19",
28+
"biome-config-godaddy": "workspace:*",
29+
"tailwindcss": "^4",
30+
"typescript": "^5"
31+
}
3232
}

0 commit comments

Comments
 (0)