@youversion/platform-react-hooks 0.6.0 → 0.8.0

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.
Files changed (84) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +56 -0
  3. package/README.md +3 -3
  4. package/dist/__tests__/mocks/auth.d.ts +12 -0
  5. package/dist/__tests__/mocks/auth.d.ts.map +1 -0
  6. package/dist/__tests__/mocks/auth.js +44 -0
  7. package/dist/__tests__/mocks/auth.js.map +1 -0
  8. package/dist/__tests__/utils/test-utils.d.ts +15 -0
  9. package/dist/__tests__/utils/test-utils.d.ts.map +1 -0
  10. package/dist/__tests__/utils/test-utils.js +24 -0
  11. package/dist/__tests__/utils/test-utils.js.map +1 -0
  12. package/dist/context/YouVersionAuthContext.d.ts +4 -0
  13. package/dist/context/YouVersionAuthContext.d.ts.map +1 -0
  14. package/dist/context/YouVersionAuthContext.js +13 -0
  15. package/dist/context/YouVersionAuthContext.js.map +1 -0
  16. package/dist/context/YouVersionAuthProvider.d.ts +8 -0
  17. package/dist/context/YouVersionAuthProvider.d.ts.map +1 -0
  18. package/dist/context/YouVersionAuthProvider.js +80 -0
  19. package/dist/context/YouVersionAuthProvider.js.map +1 -0
  20. package/dist/context/YouVersionContext.d.ts +8 -0
  21. package/dist/context/YouVersionContext.d.ts.map +1 -0
  22. package/dist/context/YouVersionContext.js +4 -0
  23. package/dist/context/YouVersionContext.js.map +1 -0
  24. package/dist/context/YouVersionProvider.d.ts +17 -0
  25. package/dist/context/YouVersionProvider.d.ts.map +1 -0
  26. package/dist/context/YouVersionProvider.js +25 -0
  27. package/dist/context/YouVersionProvider.js.map +1 -0
  28. package/dist/context/index.d.ts +2 -2
  29. package/dist/context/index.d.ts.map +1 -1
  30. package/dist/context/index.js +2 -2
  31. package/dist/context/index.js.map +1 -1
  32. package/dist/index.d.ts +2 -0
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +3 -0
  35. package/dist/index.js.map +1 -1
  36. package/dist/types/auth.d.ts +18 -0
  37. package/dist/types/auth.d.ts.map +1 -0
  38. package/dist/types/auth.js +2 -0
  39. package/dist/types/auth.js.map +1 -0
  40. package/dist/useBibleClient.js +3 -3
  41. package/dist/useBibleClient.js.map +1 -1
  42. package/dist/useHighlights.js +3 -3
  43. package/dist/useHighlights.js.map +1 -1
  44. package/dist/useLanguages.js +3 -3
  45. package/dist/useLanguages.js.map +1 -1
  46. package/dist/usePassage.d.ts +3 -3
  47. package/dist/usePassage.d.ts.map +1 -1
  48. package/dist/usePassage.js +5 -5
  49. package/dist/usePassage.js.map +1 -1
  50. package/dist/useYVAuth.d.ts +97 -0
  51. package/dist/useYVAuth.d.ts.map +1 -0
  52. package/dist/useYVAuth.js +135 -0
  53. package/dist/useYVAuth.js.map +1 -0
  54. package/package.json +3 -3
  55. package/src/__tests__/mocks/auth.ts +48 -0
  56. package/src/__tests__/utils/test-utils.tsx +43 -0
  57. package/src/context/YouVersionAuthContext.test.tsx +88 -0
  58. package/src/context/YouVersionAuthContext.tsx +20 -0
  59. package/src/context/YouVersionAuthProvider.test.tsx +373 -0
  60. package/src/context/YouVersionAuthProvider.tsx +90 -0
  61. package/src/context/{BibleSDKContext.tsx → YouVersionContext.tsx} +2 -2
  62. package/src/context/YouVersionProvider.tsx +65 -0
  63. package/src/context/index.ts +2 -2
  64. package/src/index.ts +4 -0
  65. package/src/types/auth.ts +20 -0
  66. package/src/useBibleClient.test.tsx +14 -14
  67. package/src/useBibleClient.ts +3 -3
  68. package/src/useHighlights.test.tsx +6 -6
  69. package/src/useHighlights.ts +3 -3
  70. package/src/useLanguages.test.tsx +6 -6
  71. package/src/useLanguages.ts +3 -3
  72. package/src/usePassage.ts +8 -15
  73. package/src/useVOTD.test.tsx +6 -6
  74. package/src/useYVAuth.test.tsx +378 -0
  75. package/src/useYVAuth.ts +179 -0
  76. package/dist/context/BibleSDKContext.d.ts +0 -8
  77. package/dist/context/BibleSDKContext.d.ts.map +0 -1
  78. package/dist/context/BibleSDKContext.js +0 -4
  79. package/dist/context/BibleSDKContext.js.map +0 -1
  80. package/dist/context/BibleSDKProvider.d.ts +0 -9
  81. package/dist/context/BibleSDKProvider.d.ts.map +0 -1
  82. package/dist/context/BibleSDKProvider.js +0 -18
  83. package/dist/context/BibleSDKProvider.js.map +0 -1
  84. package/src/context/BibleSDKProvider.tsx +0 -35
@@ -0,0 +1,179 @@
1
+ import { useMemo, useCallback } from 'react';
2
+ import {
3
+ YouVersionAPIUsers,
4
+ YouVersionPlatformConfiguration,
5
+ type AuthenticationState,
6
+ type SignInWithYouVersionPermissionValues,
7
+ type SignInWithYouVersionResult,
8
+ type YouVersionUserInfo,
9
+ } from '@youversion/platform-core';
10
+ import { useYouVersionAuthContext } from './context/YouVersionAuthContext';
11
+
12
+ export interface UseYVAuthReturn {
13
+ // Main auth state
14
+ auth: AuthenticationState;
15
+
16
+ // Actions
17
+ signIn: (params: {
18
+ redirectUrl: string;
19
+ permissions?: SignInWithYouVersionPermissionValues[];
20
+ }) => Promise<void>;
21
+ signOut: () => void;
22
+
23
+ // Callback processing (for callback page) - caches user info
24
+ processCallback: () => Promise<SignInWithYouVersionResult | null>;
25
+
26
+ // Cached user info (populated after successful callback)
27
+ userInfo: YouVersionUserInfo | null;
28
+ }
29
+
30
+ /**
31
+ * Comprehensive YouVersion authentication hook using vanilla React.
32
+ *
33
+ * Provides complete auth functionality without external dependencies.
34
+ * Sign-in redirects to YouVersion, callback processing caches user info.
35
+ *
36
+ * @returns Complete auth state and functionality
37
+ *
38
+ * @example
39
+ * ```tsx
40
+ * // Sign-in page
41
+ * import { useYVAuth, SignInWithYouVersionPermission } from '@youversion/platform-react-hooks';
42
+ *
43
+ * function SignInPage() {
44
+ * const { auth, signIn } = useYVAuth();
45
+ *
46
+ * const handleSignIn = async () => {
47
+ * try {
48
+ * await signIn({
49
+ * redirectUrl: 'https://myapp.com/callback',
50
+ * permissions: [SignInWithYouVersionPermission.bibles]
51
+ * });
52
+ * } catch (error) {
53
+ * console.error('Sign in failed:', error);
54
+ * }
55
+ * };
56
+ *
57
+ * if (auth.isAuthenticated) {
58
+ * return <div>Already signed in!</div>;
59
+ * }
60
+ *
61
+ * return (
62
+ * <button onClick={handleSignIn} disabled={auth.isLoading}>
63
+ * {auth.isLoading ? 'Signing in...' : 'Sign In'}
64
+ * </button>
65
+ * );
66
+ * }
67
+ *
68
+ * // Callback page
69
+ * function CallbackPage() {
70
+ * const { processCallback } = useYVAuth();
71
+ * const [isProcessing, setIsProcessing] = useState(true);
72
+ *
73
+ * useEffect(() => {
74
+ * processCallback()
75
+ * .then(result => {
76
+ * if (result) {
77
+ * console.log('Auth success:', result.name);
78
+ * // User info is now cached
79
+ * // Redirect to app
80
+ * }
81
+ * })
82
+ * .catch(error => {
83
+ * console.error('Auth failed:', error);
84
+ * })
85
+ * .finally(() => {
86
+ * setIsProcessing(false);
87
+ * });
88
+ * }, [processCallback]);
89
+ *
90
+ * return (
91
+ * <div>
92
+ * {isProcessing ? 'Processing authentication...' : 'Authentication complete'}
93
+ * </div>
94
+ * );
95
+ * }
96
+ *
97
+ * // App with cached user info
98
+ * function App() {
99
+ * const { auth, userInfo, signOut } = useYVAuth();
100
+ *
101
+ * if (!auth.isAuthenticated) {
102
+ * return <SignInPage />;
103
+ * }
104
+ *
105
+ * return (
106
+ * <div>
107
+ * <p>Welcome, {userInfo?.name || 'User'}!</p>
108
+ * <button onClick={signOut}>Sign Out</button>
109
+ * </div>
110
+ * );
111
+ * }
112
+ * ```
113
+ */
114
+ export function useYVAuth(): UseYVAuthReturn {
115
+ // Get auth state from provider context
116
+ const { userInfo, setUserInfo, isLoading, error } = useYouVersionAuthContext();
117
+
118
+ // Derive authentication state
119
+ const isAuthenticated = !!userInfo;
120
+
121
+ // Get current tokens for actions
122
+ const authTokens = useMemo(() => {
123
+ if (typeof window !== 'undefined') {
124
+ return {
125
+ accessToken: YouVersionPlatformConfiguration.accessToken,
126
+ idToken: YouVersionPlatformConfiguration.idToken,
127
+ };
128
+ }
129
+ return { accessToken: null, idToken: null };
130
+ }, []);
131
+
132
+ // Sign in function
133
+ const signIn = useCallback(
134
+ async ({
135
+ redirectUrl,
136
+ permissions = [],
137
+ }: {
138
+ redirectUrl: string;
139
+ permissions?: SignInWithYouVersionPermissionValues[];
140
+ }) => {
141
+ await YouVersionAPIUsers.signIn(new Set(permissions), redirectUrl);
142
+ // Note: This will redirect, so code after this won't execute
143
+ },
144
+ [],
145
+ );
146
+
147
+ // Process callback function
148
+ const processCallback = useCallback(async (): Promise<SignInWithYouVersionResult | null> => {
149
+ const result = await YouVersionAPIUsers.handleAuthCallback();
150
+ return result;
151
+ }, []);
152
+
153
+ // Derive auth state
154
+ const auth: AuthenticationState = useMemo(
155
+ () => ({
156
+ isAuthenticated,
157
+ isLoading,
158
+ accessToken: authTokens.accessToken,
159
+ idToken: authTokens.idToken,
160
+ result: null,
161
+ error,
162
+ }),
163
+ [isAuthenticated, isLoading, authTokens.accessToken, authTokens.idToken, error],
164
+ );
165
+
166
+ // Sign out function
167
+ const signOut = useCallback(() => {
168
+ YouVersionPlatformConfiguration.clearAuthTokens();
169
+ setUserInfo(null);
170
+ }, []);
171
+
172
+ return {
173
+ auth,
174
+ signIn,
175
+ signOut,
176
+ processCallback,
177
+ userInfo,
178
+ };
179
+ }
@@ -1,8 +0,0 @@
1
- type BibleSDKContextData = {
2
- appKey: string;
3
- apiHost?: string;
4
- installationId?: string;
5
- };
6
- export declare const BibleSDKContext: import("react").Context<BibleSDKContextData | null>;
7
- export {};
8
- //# sourceMappingURL=BibleSDKContext.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BibleSDKContext.d.ts","sourceRoot":"","sources":["../../src/context/BibleSDKContext.tsx"],"names":[],"mappings":"AAIA,KAAK,mBAAmB,GAAG;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF,eAAO,MAAM,eAAe,qDAAkD,CAAC"}
@@ -1,4 +0,0 @@
1
- 'use client';
2
- import { createContext } from 'react';
3
- export const BibleSDKContext = createContext(null);
4
- //# sourceMappingURL=BibleSDKContext.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BibleSDKContext.js","sourceRoot":"","sources":["../../src/context/BibleSDKContext.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAQtC,MAAM,CAAC,MAAM,eAAe,GAAG,aAAa,CAA6B,IAAI,CAAC,CAAC"}
@@ -1,9 +0,0 @@
1
- import type { PropsWithChildren, ReactNode } from 'react';
2
- type BibleSDKProviderProps = {
3
- children: ReactNode;
4
- appKey: string;
5
- apiHost?: string;
6
- };
7
- export declare function BibleSDKProvider({ appKey, apiHost, children, }: PropsWithChildren<BibleSDKProviderProps>): React.ReactElement;
8
- export {};
9
- //# sourceMappingURL=BibleSDKProvider.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BibleSDKProvider.d.ts","sourceRoot":"","sources":["../../src/context/BibleSDKProvider.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAK1D,KAAK,qBAAqB,GAAG;IAC3B,QAAQ,EAAE,SAAS,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,wBAAgB,gBAAgB,CAAC,EAC/B,MAAM,EACN,OAA8B,EAC9B,QAAQ,GACT,EAAE,iBAAiB,CAAC,qBAAqB,CAAC,GAAG,KAAK,CAAC,YAAY,CAiB/D"}
@@ -1,18 +0,0 @@
1
- 'use client';
2
- import { jsx as _jsx } from "react/jsx-runtime";
3
- import { useEffect } from 'react';
4
- import { BibleSDKContext } from './BibleSDKContext';
5
- import { YouVersionPlatformConfiguration } from '@youversion/platform-core';
6
- export function BibleSDKProvider({ appKey, apiHost = 'api.youversion.com', children, }) {
7
- // Syncing appKey and apiHost to YouVersionPlatformConfiguration
8
- // so that this can be in sync with any other code that uses
9
- // the YouVersionPlatformConfiguration, of which a lot of our
10
- // core package uses this configuration.
11
- useEffect(() => {
12
- YouVersionPlatformConfiguration.appKey = appKey;
13
- YouVersionPlatformConfiguration.apiHost = apiHost;
14
- }, [appKey, apiHost]);
15
- // Installation ID gets set automatically by YouVersionPlatformConfiguration
16
- return (_jsx(BibleSDKContext.Provider, { value: { appKey, apiHost, installationId: YouVersionPlatformConfiguration.installationId }, children: children }));
17
- }
18
- //# sourceMappingURL=BibleSDKProvider.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BibleSDKProvider.js","sourceRoot":"","sources":["../../src/context/BibleSDKProvider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAGb,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAClC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,+BAA+B,EAAE,MAAM,2BAA2B,CAAC;AAQ5E,MAAM,UAAU,gBAAgB,CAAC,EAC/B,MAAM,EACN,OAAO,GAAG,oBAAoB,EAC9B,QAAQ,GACiC;IACzC,gEAAgE;IAChE,4DAA4D;IAC5D,6DAA6D;IAC7D,wCAAwC;IACxC,SAAS,CAAC,GAAG,EAAE;QACb,+BAA+B,CAAC,MAAM,GAAG,MAAM,CAAC;QAChD,+BAA+B,CAAC,OAAO,GAAG,OAAO,CAAC;IACpD,CAAC,EAAE,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACtB,4EAA4E;IAC5E,OAAO,CACL,KAAC,eAAe,CAAC,QAAQ,IACvB,KAAK,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,EAAE,+BAA+B,CAAC,cAAc,EAAE,YAEzF,QAAQ,GACgB,CAC5B,CAAC;AACJ,CAAC"}
@@ -1,35 +0,0 @@
1
- 'use client';
2
-
3
- import type { PropsWithChildren, ReactNode } from 'react';
4
- import { useEffect } from 'react';
5
- import { BibleSDKContext } from './BibleSDKContext';
6
- import { YouVersionPlatformConfiguration } from '@youversion/platform-core';
7
-
8
- type BibleSDKProviderProps = {
9
- children: ReactNode;
10
- appKey: string;
11
- apiHost?: string;
12
- };
13
-
14
- export function BibleSDKProvider({
15
- appKey,
16
- apiHost = 'api.youversion.com',
17
- children,
18
- }: PropsWithChildren<BibleSDKProviderProps>): React.ReactElement {
19
- // Syncing appKey and apiHost to YouVersionPlatformConfiguration
20
- // so that this can be in sync with any other code that uses
21
- // the YouVersionPlatformConfiguration, of which a lot of our
22
- // core package uses this configuration.
23
- useEffect(() => {
24
- YouVersionPlatformConfiguration.appKey = appKey;
25
- YouVersionPlatformConfiguration.apiHost = apiHost;
26
- }, [appKey, apiHost]);
27
- // Installation ID gets set automatically by YouVersionPlatformConfiguration
28
- return (
29
- <BibleSDKContext.Provider
30
- value={{ appKey, apiHost, installationId: YouVersionPlatformConfiguration.installationId }}
31
- >
32
- {children}
33
- </BibleSDKContext.Provider>
34
- );
35
- }