@cohostvip/cohost-react 0.3.12 → 0.3.13

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.
@@ -1,5 +1,5 @@
1
1
  import * as React from 'react';
2
- import { AuthClient, type AuthConfig, type AuthState, type AuthUser, type OTPType, type SetAuthenticatedInput } from '@cohostvip/cohost-auth';
2
+ import { AuthClient, type AuthConfig, type AuthState, type AuthUser, type OTPType, type SetAuthenticatedInput, type TokenAuthResult } from '@cohostvip/cohost-auth';
3
3
  /**
4
4
  * Value provided by AuthContext
5
5
  */
@@ -18,6 +18,8 @@ export interface AuthContextValue {
18
18
  getToken: () => Promise<string | null>;
19
19
  /** Manually set authenticated state (for custom auth flows like passkey) */
20
20
  setAuthenticated: (input: SetAuthenticatedInput) => void;
21
+ /** Authenticate using a token (validates via API and sets auth state) */
22
+ authenticateWithToken: (token: string) => Promise<TokenAuthResult>;
21
23
  }
22
24
  /**
23
25
  * Props for AuthProvider
@@ -1 +1 @@
1
- {"version":3,"file":"AuthContext.d.ts","sourceRoot":"","sources":["../../src/auth/AuthContext.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,EACL,UAAU,EAEV,KAAK,UAAU,EACf,KAAK,SAAS,EACd,KAAK,QAAQ,EACb,KAAK,OAAO,EACZ,KAAK,qBAAqB,EAC3B,MAAM,wBAAwB,CAAC;AAEhC;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,yBAAyB;IACzB,KAAK,EAAE,SAAS,CAAC;IACjB,yCAAyC;IACzC,MAAM,EAAE,UAAU,CAAC;IACnB,yDAAyD;IACzD,UAAU,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IAClE,6BAA6B;IAC7B,SAAS,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK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AI,CAAC,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IAClE,6BAA6B;IAC7B,SAAS,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;IAChE,gCAAgC;IAChC,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,sDAAsD;IACtD,QAAQ,EAAE,MAAM,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IACvC,4EAA4E;IAC5E,gBAAgB,EAAE,CAAC,KAAK,EAAE,qBAAqB,KAAK,IAAI,CAAC;IACzD,yEAAyE;IACzE,qBAAqB,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,eAAe,CAAC,CAAC;CACpE;AAED;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,yBAAyB;IACzB,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B,GAAG,CACA;IACE,gDAAgD;IAChD,MAAM,EAAE,UAAU,CAAC;IACnB,2DAA2D;IAC3D,MAAM,CAAC,EAAE,KAAK,CAAC;CAChB,GACD;IACE,yBAAyB;IACzB,MAAM,CAAC,EAAE,KAAK,CAAC;IACf,uCAAuC;IACvC,MAAM,EAAE,UAAU,CAAC;CACpB,CACJ,CAAC;AAEF,eAAO,MAAM,WAAW,wCAA+C,CAAC;AAExE;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,YAAY,EAAE,KAAK,CAAC,EAAE,CAAC,iBAAiB,CA8CpD,CAAC"}
@@ -48,6 +48,7 @@ export const AuthProvider = ({ children, config, client: providedClient, }) => {
48
48
  signOut: () => client.signOut(),
49
49
  getToken: () => client.getToken(),
50
50
  setAuthenticated: (input) => client.setAuthenticated(input),
51
+ authenticateWithToken: (token) => client.authenticateWithToken(token),
51
52
  }), [state, client]);
52
53
  return _jsx(AuthContext.Provider, { value: value, children: children });
53
54
  };
@@ -1,4 +1,5 @@
1
1
  export { AuthProvider, type AuthProviderProps, type AuthContextValue } from './AuthContext';
2
2
  export { useAuth, useUser, useAuthClient } from './hooks';
3
+ export { useTokenAuth, type UseTokenAuthOptions } from './useTokenAuth';
3
4
  export { AuthGuard, type AuthGuardProps } from './AuthGuard';
4
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,KAAK,iBAAiB,EAAE,KAAK,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAG5F,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAG1D,OAAO,EAAE,SAAS,EAAE,KAAK,cAAc,EAAE,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,KAAK,iBAAiB,EAAE,KAAK,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAG5F,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,KAAK,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAGxE,OAAO,EAAE,SAAS,EAAE,KAAK,cAAc,EAAE,MAAM,aAAa,CAAC"}
@@ -2,5 +2,6 @@
2
2
  export { AuthProvider } from './AuthContext';
3
3
  // Hooks
4
4
  export { useAuth, useUser, useAuthClient } from './hooks';
5
+ export { useTokenAuth } from './useTokenAuth';
5
6
  // Components
6
7
  export { AuthGuard } from './AuthGuard';
@@ -0,0 +1,39 @@
1
+ import type { TokenAuthResult } from '@cohostvip/cohost-auth';
2
+ /**
3
+ * Options for useTokenAuth hook
4
+ */
5
+ export interface UseTokenAuthOptions {
6
+ /**
7
+ * Callback when token authentication completes (success or failure).
8
+ * Called before the page is refreshed.
9
+ */
10
+ onComplete?: (result: TokenAuthResult) => void;
11
+ }
12
+ /**
13
+ * Hook that handles token-based authentication from URL query params.
14
+ *
15
+ * When the configured `tokenParam` is found in the URL:
16
+ * 1. Validates the token via API
17
+ * 2. If valid, sets authenticated state
18
+ * 3. Removes the token from URL and refreshes (or replaces history)
19
+ *
20
+ * @example
21
+ * ```tsx
22
+ * // In your app's root component or layout
23
+ * function App() {
24
+ * useTokenAuth({
25
+ * onComplete: (result) => {
26
+ * if (result.success) {
27
+ * console.log('Logged in as:', result.user?.email);
28
+ * } else {
29
+ * console.log('Token auth failed:', result.error);
30
+ * }
31
+ * }
32
+ * });
33
+ *
34
+ * return <div>...</div>;
35
+ * }
36
+ * ```
37
+ */
38
+ export declare function useTokenAuth(options?: UseTokenAuthOptions): void;
39
+ //# sourceMappingURL=useTokenAuth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTokenAuth.d.ts","sourceRoot":"","sources":["../../src/auth/useTokenAuth.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAG9D;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC;;;OAGG;IACH,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,eAAe,KAAK,IAAI,CAAC;CAChD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAE,mBAAwB,GAAG,IAAI,CA8CpE"}
@@ -0,0 +1,67 @@
1
+ import { useEffect, useRef, useCallback } from 'react';
2
+ import { useAuthClient } from './hooks';
3
+ /**
4
+ * Hook that handles token-based authentication from URL query params.
5
+ *
6
+ * When the configured `tokenParam` is found in the URL:
7
+ * 1. Validates the token via API
8
+ * 2. If valid, sets authenticated state
9
+ * 3. Removes the token from URL and refreshes (or replaces history)
10
+ *
11
+ * @example
12
+ * ```tsx
13
+ * // In your app's root component or layout
14
+ * function App() {
15
+ * useTokenAuth({
16
+ * onComplete: (result) => {
17
+ * if (result.success) {
18
+ * console.log('Logged in as:', result.user?.email);
19
+ * } else {
20
+ * console.log('Token auth failed:', result.error);
21
+ * }
22
+ * }
23
+ * });
24
+ *
25
+ * return <div>...</div>;
26
+ * }
27
+ * ```
28
+ */
29
+ export function useTokenAuth(options = {}) {
30
+ const client = useAuthClient();
31
+ const { onComplete } = options;
32
+ const processedRef = useRef(false);
33
+ const processToken = useCallback(async () => {
34
+ // Only run in browser
35
+ if (typeof window === 'undefined')
36
+ return;
37
+ // Only process once per mount
38
+ if (processedRef.current)
39
+ return;
40
+ // Check if tokenParam is configured
41
+ const tokenParam = client.tokenParamName;
42
+ if (!tokenParam)
43
+ return;
44
+ // Get token from URL
45
+ const urlParams = new URLSearchParams(window.location.search);
46
+ const token = urlParams.get(tokenParam);
47
+ if (!token)
48
+ return;
49
+ // Mark as processed to prevent duplicate processing
50
+ processedRef.current = true;
51
+ // Authenticate with the token
52
+ const result = await client.authenticateWithToken(token);
53
+ // Call onComplete callback
54
+ onComplete?.(result);
55
+ // Remove token from URL
56
+ urlParams.delete(tokenParam);
57
+ const newSearch = urlParams.toString();
58
+ const newUrl = window.location.pathname + (newSearch ? `?${newSearch}` : '') + window.location.hash;
59
+ // Replace history state to remove token, then refresh to clean up
60
+ window.history.replaceState({}, '', newUrl);
61
+ // Refresh the page to ensure clean state
62
+ window.location.reload();
63
+ }, [client, onComplete]);
64
+ useEffect(() => {
65
+ processToken();
66
+ }, [processToken]);
67
+ }
@@ -13,6 +13,7 @@ export type CohostProviderProps = {
13
13
  export declare const CohostProvider: React.FC<CohostProviderProps>;
14
14
  export declare const useCohostClient: () => {
15
15
  client: CohostClient;
16
+ apiUrl: string;
16
17
  debug: boolean;
17
18
  };
18
19
  //# sourceMappingURL=CohostContext.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"CohostContext.d.ts","sourceRoot":"","sources":["../../src/context/CohostContext.tsx"],"names":[],"mappings":"AAAA,OAAO,KA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IAAC,KAAK,EAAE,OAAO,CAAA;CAIxF,CAAC"}
@@ -4,8 +4,9 @@ import { createCohostClient } from '@cohostvip/cohost-node';
4
4
  const CohostContext = createContext(null);
5
5
  export const CohostProvider = ({ client: providedClient, settings, token: providedToken, children, }) => {
6
6
  const client = providedClient ?? createCohostClient({ token: providedToken, settings }); // assumes a factory fn in cohost-node
7
+ const apiUrl = client.apiUrl;
7
8
  const debug = settings?.debug ?? false;
8
- return (_jsx(CohostContext.Provider, { value: { client, debug }, children: children }));
9
+ return (_jsx(CohostContext.Provider, { value: { client, apiUrl, debug }, children: children }));
9
10
  };
10
11
  export const useCohostClient = () => {
11
12
  const ctx = useContext(CohostContext);
package/dist/index.d.ts CHANGED
@@ -6,5 +6,5 @@ export { CohostStartCheckoutProvider } from './provider/CohostStartCheckoutProvi
6
6
  export { CreditCardInformation } from './lib/tokenizers/types';
7
7
  export { useCohost } from './hooks/useCohost';
8
8
  export { formatCurrency } from './lib/utils';
9
- export { AuthProvider, type AuthProviderProps, type AuthContextValue, useAuth, useUser, useAuthClient, AuthGuard, type AuthGuardProps, } from './auth';
9
+ export { AuthProvider, type AuthProviderProps, type AuthContextValue, useAuth, useUser, useAuthClient, useTokenAuth, type UseTokenAuthOptions, AuthGuard, type AuthGuardProps, } from './auth';
10
10
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AACnH,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AACnF,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,KAAK,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AACpG,OAAO,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AAC5F,OAAO,EAAE,2BAA2B,EAAE,MAAM,wCAAwC,CAAC;AACrF,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAG7C,OAAO,EACL,YAAY,EACZ,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EACrB,OAAO,EACP,OAAO,EACP,aAAa,EACb,SAAS,EACT,KAAK,cAAc,GACpB,MAAM,QAAQ,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AACnH,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AACnF,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,KAAK,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AACpG,OAAO,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AAC5F,OAAO,EAAE,2BAA2B,EAAE,MAAM,wCAAwC,CAAC;AACrF,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAG7C,OAAO,EACL,YAAY,EACZ,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EACrB,OAAO,EACP,OAAO,EACP,aAAa,EACb,YAAY,EACZ,KAAK,mBAAmB,EACxB,SAAS,EACT,KAAK,cAAc,GACpB,MAAM,QAAQ,CAAC"}
package/dist/index.js CHANGED
@@ -6,4 +6,4 @@ export { CohostStartCheckoutProvider } from './provider/CohostStartCheckoutProvi
6
6
  export { useCohost } from './hooks/useCohost';
7
7
  export { formatCurrency } from './lib/utils';
8
8
  // Auth bindings
9
- export { AuthProvider, useAuth, useUser, useAuthClient, AuthGuard, } from './auth';
9
+ export { AuthProvider, useAuth, useUser, useAuthClient, useTokenAuth, AuthGuard, } from './auth';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cohostvip/cohost-react",
3
- "version": "0.3.12",
3
+ "version": "0.3.13",
4
4
  "description": "React bindings for the Cohost API",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -20,8 +20,8 @@
20
20
  "react-dom": "^18.0.0 || ^19.0.0"
21
21
  },
22
22
  "dependencies": {
23
- "@cohostvip/cohost-node": "^0.3.12",
24
- "@cohostvip/cohost-auth": "^0.3.12"
23
+ "@cohostvip/cohost-node": "^0.3.13",
24
+ "@cohostvip/cohost-auth": "^0.3.13"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@testing-library/jest-dom": "6.6.3",