@usequeek/theme-kit 0.5.16 → 0.5.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/hooks/use-auth-flow.tsx +17 -32
- package/package.json +1 -1
- package/utils/navigate.ts +8 -0
package/hooks/use-auth-flow.tsx
CHANGED
|
@@ -4,9 +4,10 @@ import type { JSX } from 'react';
|
|
|
4
4
|
import { createContext, useContext, useState, type ReactNode } from 'react';
|
|
5
5
|
import { QueekSdkError, type EmailVerifyOtpResponse } from '@usequeek/client-sdk';
|
|
6
6
|
import { useIsThemePreview, useStorefront, useThemeStrings } from '../provider';
|
|
7
|
-
import { getQueekClient, setAuthTokens
|
|
7
|
+
import { getQueekClient, setAuthTokens } from '../sdk/queek-client';
|
|
8
8
|
import { useAuthModalStore } from '../stores/auth-modal-store';
|
|
9
9
|
import { useUserStore } from '../stores/user-store';
|
|
10
|
+
import { navigateTo } from '../utils/navigate';
|
|
10
11
|
|
|
11
12
|
export type AuthStep =
|
|
12
13
|
| 'email-otp'
|
|
@@ -85,10 +86,20 @@ export function mapEmailError(err: unknown, t: (key: string) => string): string
|
|
|
85
86
|
return t('auth.error.generic');
|
|
86
87
|
}
|
|
87
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Where a store's Google sign-in starts: `<basePath>/auth/google?return_to=<this page>`. `return_to` is the page's own
|
|
91
|
+
* path and query, relative (the handler accepts nothing else), so the shopper comes back where they were.
|
|
92
|
+
*/
|
|
93
|
+
export function googleSignInStartHref(basePath: string, location: Pick<Location, 'pathname' | 'search'>): string {
|
|
94
|
+
const returnTo = `${location.pathname || '/'}${location.search}`;
|
|
95
|
+
|
|
96
|
+
return `${basePath}/auth/google?return_to=${encodeURIComponent(returnTo)}`;
|
|
97
|
+
}
|
|
98
|
+
|
|
88
99
|
const AuthFlowContext = createContext<AuthFlowValue | null>(null);
|
|
89
100
|
|
|
90
101
|
export function AuthFlowProvider({ children }: { children: ReactNode }): JSX.Element {
|
|
91
|
-
const { vendor } = useStorefront();
|
|
102
|
+
const { vendor, basePath } = useStorefront();
|
|
92
103
|
const t = useThemeStrings();
|
|
93
104
|
const isThemePreview = useIsThemePreview();
|
|
94
105
|
const closeModal = useAuthModalStore((s) => s.close);
|
|
@@ -125,8 +136,7 @@ export function AuthFlowProvider({ children }: { children: ReactNode }): JSX.Ele
|
|
|
125
136
|
});
|
|
126
137
|
}
|
|
127
138
|
|
|
128
|
-
//
|
|
129
|
-
// — it must stay mounted whether or not the auth modal is open.
|
|
139
|
+
// The Google return lands on the storefront's own /auth/callback page (G7 S1), which redeems the code server-side.
|
|
130
140
|
|
|
131
141
|
// ── Email OTP flow (primary) ──────────────────────────────────────────────
|
|
132
142
|
|
|
@@ -268,38 +278,13 @@ export function AuthFlowProvider({ children }: { children: ReactNode }): JSX.Ele
|
|
|
268
278
|
|
|
269
279
|
// ── Google OAuth ──────────────────────────────────────────────────────────
|
|
270
280
|
|
|
281
|
+
// The store's own /auth/google handler (G7 S1) starts Google on the server and keeps the flow's state in an HttpOnly cookie;
|
|
282
|
+
// Google sends the shopper back to the store's /auth/callback landing, never to this page, so no code ever reaches it.
|
|
271
283
|
const initiateGoogleAuth = async (): Promise<void> => {
|
|
272
284
|
if (blockPreview()) return;
|
|
273
285
|
setError(null);
|
|
274
286
|
setLoading(true);
|
|
275
|
-
|
|
276
|
-
const returnTo = typeof window !== 'undefined' ? window.location.href : '';
|
|
277
|
-
const slug = vendor.slug ?? '';
|
|
278
|
-
const url = `${QUEEK_AUTH_URL}/oauth/google/start?channel=storefront&vendor_slug=${encodeURIComponent(slug)}&return_to=${encodeURIComponent(returnTo)}`;
|
|
279
|
-
const res = await fetch(url);
|
|
280
|
-
if (!res.ok) {
|
|
281
|
-
throw new Error(`The Queek sign-in service answered HTTP ${res.status} when starting Google sign-in. Retry, or use email sign-in.`);
|
|
282
|
-
}
|
|
283
|
-
// Response envelope: { status, message, data: { url } }
|
|
284
|
-
const body = (await res.json()) as {
|
|
285
|
-
data?: { url?: string; redirect_url?: string };
|
|
286
|
-
url?: string;
|
|
287
|
-
redirect_url?: string;
|
|
288
|
-
};
|
|
289
|
-
const redirectUrl =
|
|
290
|
-
body.data?.url ?? body.data?.redirect_url ?? body.url ?? body.redirect_url;
|
|
291
|
-
if (redirectUrl) {
|
|
292
|
-
window.location.href = redirectUrl;
|
|
293
|
-
} else {
|
|
294
|
-
throw new Error('The Queek sign-in service did not return a Google redirect URL.');
|
|
295
|
-
}
|
|
296
|
-
} catch (err) {
|
|
297
|
-
// The shopper sees the translated message; the cause stays in the console for the theme developer.
|
|
298
|
-
console.error('[theme-kit] could not start Google sign-in:', err);
|
|
299
|
-
setError(t('auth.error.google'));
|
|
300
|
-
} finally {
|
|
301
|
-
setLoading(false);
|
|
302
|
-
}
|
|
287
|
+
navigateTo(googleSignInStartHref(basePath, window.location));
|
|
303
288
|
};
|
|
304
289
|
|
|
305
290
|
// ── Navigation ────────────────────────────────────────────────────────────
|
package/package.json
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A full document navigation (never a client-side route change): the page that asked for it is replaced, so nothing of it
|
|
3
|
+
* survives into the next document. One call site per flow, so a test can observe where a flow sends the browser (a real
|
|
4
|
+
* `window.location` cannot be stubbed in jsdom).
|
|
5
|
+
*/
|
|
6
|
+
export function navigateTo(url: string): void {
|
|
7
|
+
window.location.assign(url);
|
|
8
|
+
}
|