@gately/react 1.0.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.
@@ -0,0 +1,42 @@
1
+ /**
2
+ * useUser Hook
3
+ * Dedicated hook for accessing user information
4
+ */
5
+ import type { User } from '@gately/sdk';
6
+ export interface UseUserReturn {
7
+ user: User | null;
8
+ profile: any | null;
9
+ isLoading: boolean;
10
+ error: Error | null;
11
+ refetch: () => Promise<void>;
12
+ update: (updates: any) => Promise<any>;
13
+ delete: () => Promise<void>;
14
+ }
15
+ /**
16
+ * Hook to access and manage user information
17
+ *
18
+ * @returns {UseUserReturn} User data with loading and error states
19
+ *
20
+ * @example
21
+ * ```tsx
22
+ * function UserProfile() {
23
+ * const { user, profile, isLoading, error, update } = useUser()
24
+ *
25
+ * if (isLoading) return <p>Loading...</p>
26
+ * if (error) return <p>Error: {error.message}</p>
27
+ * if (!user) return <p>Not logged in</p>
28
+ *
29
+ * return (
30
+ * <div>
31
+ * <p>Email: {user.email}</p>
32
+ * <button onClick={() => update({ name: 'New Name' })}>
33
+ * Update Profile
34
+ * </button>
35
+ * </div>
36
+ * )
37
+ * }
38
+ * ```
39
+ */
40
+ export declare function useUser(): UseUserReturn;
41
+ export default useUser;
42
+ //# sourceMappingURL=useUser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useUser.d.ts","sourceRoot":"","sources":["../../src/hooks/useUser.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AAEvC,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,IAAI,GAAG,IAAI,CAAA;IACjB,OAAO,EAAE,GAAG,GAAG,IAAI,CAAA;IACnB,SAAS,EAAE,OAAO,CAAA;IAClB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAA;IACnB,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;IAC5B,MAAM,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,CAAC,CAAA;IACtC,MAAM,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAC5B;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,OAAO,IAAI,aAAa,CAoGvC;AAED,eAAe,OAAO,CAAA"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @gately/react
3
+ * React SDK for Gately authentication and user management
4
+ */
5
+ export { GatelyProvider, GatelyContext } from './context';
6
+ export type { GatelyContextValue, GatelyProviderProps } from './context';
7
+ export { useAuth, useUser, useProtected } from './hooks';
8
+ export type { UseUserReturn, UseProtectedReturn } from './hooks';
9
+ export type { User, Session, AuthResponse } from '@gately/sdk';
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AACzD,YAAY,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAA;AAGxE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AACxD,YAAY,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AAGhE,YAAY,EAAE,IAAI,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,397 @@
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+ var sdk = require('@gately/sdk');
5
+
6
+ /**
7
+ * Gately React Context
8
+ * Provides authentication state and methods to React components
9
+ */
10
+ const GatelyContext = React.createContext(undefined);
11
+ function GatelyProvider({ projectId, apiUrl, children, autoRefresh = true }) {
12
+ const [client] = React.useState(() => {
13
+ if (typeof window === 'undefined')
14
+ return null;
15
+ try {
16
+ return new sdk.GatelyClient(projectId, { apiUrl, autoRefresh });
17
+ }
18
+ catch (error) {
19
+ console.error('Failed to initialize Gately client:', error);
20
+ return null;
21
+ }
22
+ });
23
+ const [user, setUser] = React.useState(null);
24
+ const [session, setSession] = React.useState(null);
25
+ const [isLoading, setIsLoading] = React.useState(true);
26
+ const [error, setError] = React.useState(null);
27
+ React.useEffect(() => {
28
+ if (!client)
29
+ return;
30
+ setIsLoading(true);
31
+ setUser(client.getUser());
32
+ setSession(client.getSession());
33
+ setIsLoading(false);
34
+ const handleAuthStateChange = (newUser, newSession) => {
35
+ setUser(newUser);
36
+ setSession(newSession);
37
+ setError(null);
38
+ };
39
+ client.onAuthStateChange(handleAuthStateChange);
40
+ return () => {
41
+ client.offAuthStateChange(handleAuthStateChange);
42
+ };
43
+ }, [client]);
44
+ const login = React.useCallback(async (email, password) => {
45
+ if (!client)
46
+ throw new Error('Gately client not initialized');
47
+ setError(null);
48
+ try {
49
+ return await client.login(email, password);
50
+ }
51
+ catch (err) {
52
+ const error = err instanceof Error ? err : new Error(String(err));
53
+ setError(error);
54
+ throw error;
55
+ }
56
+ }, [client]);
57
+ const signup = React.useCallback(async (email, password, metadata) => {
58
+ if (!client)
59
+ throw new Error('Gately client not initialized');
60
+ setError(null);
61
+ try {
62
+ return await client.signup(email, password, metadata);
63
+ }
64
+ catch (err) {
65
+ const error = err instanceof Error ? err : new Error(String(err));
66
+ setError(error);
67
+ throw error;
68
+ }
69
+ }, [client]);
70
+ const logout = React.useCallback(async () => {
71
+ if (!client)
72
+ throw new Error('Gately client not initialized');
73
+ setError(null);
74
+ try {
75
+ await client.logout();
76
+ }
77
+ catch (err) {
78
+ const error = err instanceof Error ? err : new Error(String(err));
79
+ setError(error);
80
+ throw error;
81
+ }
82
+ }, [client]);
83
+ const sendMagicLink = React.useCallback(async (email, redirectTo) => {
84
+ if (!client)
85
+ throw new Error('Gately client not initialized');
86
+ setError(null);
87
+ try {
88
+ await client.sendMagicLink(email, { redirectTo });
89
+ }
90
+ catch (err) {
91
+ const error = err instanceof Error ? err : new Error(String(err));
92
+ setError(error);
93
+ throw error;
94
+ }
95
+ }, [client]);
96
+ const resetPassword = React.useCallback(async (email) => {
97
+ if (!client)
98
+ throw new Error('Gately client not initialized');
99
+ setError(null);
100
+ try {
101
+ await client.resetPassword(email);
102
+ }
103
+ catch (err) {
104
+ const error = err instanceof Error ? err : new Error(String(err));
105
+ setError(error);
106
+ throw error;
107
+ }
108
+ }, [client]);
109
+ const fetchSession = React.useCallback(async () => {
110
+ if (!client)
111
+ throw new Error('Gately client not initialized');
112
+ setError(null);
113
+ try {
114
+ return await client.fetchSession();
115
+ }
116
+ catch (err) {
117
+ const error = err instanceof Error ? err : new Error(String(err));
118
+ setError(error);
119
+ throw error;
120
+ }
121
+ }, [client]);
122
+ const getUserProfile = React.useCallback(async () => {
123
+ if (!client)
124
+ throw new Error('Gately client not initialized');
125
+ setError(null);
126
+ try {
127
+ return await client.getUserProfile();
128
+ }
129
+ catch (err) {
130
+ const error = err instanceof Error ? err : new Error(String(err));
131
+ setError(error);
132
+ throw error;
133
+ }
134
+ }, [client]);
135
+ const updateUserProfile = React.useCallback(async (updates) => {
136
+ if (!client)
137
+ throw new Error('Gately client not initialized');
138
+ setError(null);
139
+ try {
140
+ return await client.updateUserProfile(updates);
141
+ }
142
+ catch (err) {
143
+ const error = err instanceof Error ? err : new Error(String(err));
144
+ setError(error);
145
+ throw error;
146
+ }
147
+ }, [client]);
148
+ const deleteUserAccount = React.useCallback(async () => {
149
+ if (!client)
150
+ throw new Error('Gately client not initialized');
151
+ setError(null);
152
+ try {
153
+ await client.deleteUserAccount();
154
+ }
155
+ catch (err) {
156
+ const error = err instanceof Error ? err : new Error(String(err));
157
+ setError(error);
158
+ throw error;
159
+ }
160
+ }, [client]);
161
+ const changePassword = React.useCallback(async (current, newPassword) => {
162
+ if (!client)
163
+ throw new Error('Gately client not initialized');
164
+ setError(null);
165
+ try {
166
+ await client.changePassword(current, newPassword);
167
+ }
168
+ catch (err) {
169
+ const error = err instanceof Error ? err : new Error(String(err));
170
+ setError(error);
171
+ throw error;
172
+ }
173
+ }, [client]);
174
+ const value = {
175
+ user,
176
+ session,
177
+ isLoading,
178
+ isAuthenticated: !!session && new Date(session.expires_at) > new Date(),
179
+ error,
180
+ login,
181
+ signup,
182
+ logout,
183
+ sendMagicLink,
184
+ resetPassword,
185
+ fetchSession,
186
+ getUserProfile,
187
+ updateUserProfile,
188
+ deleteUserAccount,
189
+ changePassword,
190
+ client
191
+ };
192
+ return React.createElement(GatelyContext.Provider, { value }, children);
193
+ }
194
+
195
+ /**
196
+ * useAuth Hook
197
+ * Main hook for accessing Gately auth context
198
+ */
199
+ /**
200
+ * Hook to access Gately authentication context
201
+ * Must be used inside a GatelyProvider
202
+ *
203
+ * @returns {GatelyContextValue} Auth context with user, session, and auth methods
204
+ *
205
+ * @example
206
+ * ```tsx
207
+ * function LoginForm() {
208
+ * const { login, isLoading, error } = useAuth()
209
+ *
210
+ * const handleSubmit = async (email, password) => {
211
+ * try {
212
+ * await login(email, password)
213
+ * } catch (err) {
214
+ * console.error(err)
215
+ * }
216
+ * }
217
+ *
218
+ * return (
219
+ * <>
220
+ * {error && <p>{error.message}</p>}
221
+ * <button disabled={isLoading} onClick={() => handleSubmit('test@example.com', 'pass')}>
222
+ * {isLoading ? 'Logging in...' : 'Log In'}
223
+ * </button>
224
+ * </>
225
+ * )
226
+ * }
227
+ * ```
228
+ */
229
+ function useAuth() {
230
+ const context = React.useContext(GatelyContext);
231
+ if (context === undefined) {
232
+ throw new Error('useAuth must be used within a GatelyProvider');
233
+ }
234
+ return context;
235
+ }
236
+
237
+ /**
238
+ * useUser Hook
239
+ * Dedicated hook for accessing user information
240
+ */
241
+ /**
242
+ * Hook to access and manage user information
243
+ *
244
+ * @returns {UseUserReturn} User data with loading and error states
245
+ *
246
+ * @example
247
+ * ```tsx
248
+ * function UserProfile() {
249
+ * const { user, profile, isLoading, error, update } = useUser()
250
+ *
251
+ * if (isLoading) return <p>Loading...</p>
252
+ * if (error) return <p>Error: {error.message}</p>
253
+ * if (!user) return <p>Not logged in</p>
254
+ *
255
+ * return (
256
+ * <div>
257
+ * <p>Email: {user.email}</p>
258
+ * <button onClick={() => update({ name: 'New Name' })}>
259
+ * Update Profile
260
+ * </button>
261
+ * </div>
262
+ * )
263
+ * }
264
+ * ```
265
+ */
266
+ function useUser() {
267
+ const { user, isAuthenticated, getUserProfile, updateUserProfile, deleteUserAccount } = useAuth();
268
+ const [profile, setProfile] = React.useState(null);
269
+ const [isLoading, setIsLoading] = React.useState(false);
270
+ const [error, setError] = React.useState(null);
271
+ // Fetch user profile on mount or when auth state changes
272
+ React.useEffect(() => {
273
+ if (!isAuthenticated || !user) {
274
+ setProfile(null);
275
+ return;
276
+ }
277
+ const fetchProfile = async () => {
278
+ setIsLoading(true);
279
+ setError(null);
280
+ try {
281
+ const profileData = await getUserProfile();
282
+ setProfile(profileData);
283
+ }
284
+ catch (err) {
285
+ setError(err instanceof Error ? err : new Error(String(err)));
286
+ setProfile(null);
287
+ }
288
+ finally {
289
+ setIsLoading(false);
290
+ }
291
+ };
292
+ fetchProfile();
293
+ }, [isAuthenticated, user, getUserProfile]);
294
+ const refetch = React.useCallback(async () => {
295
+ if (!isAuthenticated) {
296
+ throw new Error('User must be authenticated to fetch profile');
297
+ }
298
+ setIsLoading(true);
299
+ setError(null);
300
+ try {
301
+ const profileData = await getUserProfile();
302
+ setProfile(profileData);
303
+ }
304
+ catch (err) {
305
+ const error = err instanceof Error ? err : new Error(String(err));
306
+ setError(error);
307
+ throw error;
308
+ }
309
+ finally {
310
+ setIsLoading(false);
311
+ }
312
+ }, [isAuthenticated, getUserProfile]);
313
+ const update = React.useCallback(async (updates) => {
314
+ if (!isAuthenticated) {
315
+ throw new Error('User must be authenticated to update profile');
316
+ }
317
+ setIsLoading(true);
318
+ setError(null);
319
+ try {
320
+ const updatedProfile = await updateUserProfile(updates);
321
+ setProfile(updatedProfile);
322
+ return updatedProfile;
323
+ }
324
+ catch (err) {
325
+ const error = err instanceof Error ? err : new Error(String(err));
326
+ setError(error);
327
+ throw error;
328
+ }
329
+ finally {
330
+ setIsLoading(false);
331
+ }
332
+ }, [isAuthenticated, updateUserProfile]);
333
+ const deleteUser = React.useCallback(async () => {
334
+ if (!isAuthenticated) {
335
+ throw new Error('User must be authenticated to delete account');
336
+ }
337
+ setIsLoading(true);
338
+ setError(null);
339
+ try {
340
+ await deleteUserAccount();
341
+ setProfile(null);
342
+ }
343
+ catch (err) {
344
+ const error = err instanceof Error ? err : new Error(String(err));
345
+ setError(error);
346
+ throw error;
347
+ }
348
+ finally {
349
+ setIsLoading(false);
350
+ }
351
+ }, [isAuthenticated, deleteUserAccount]);
352
+ return {
353
+ user,
354
+ profile,
355
+ isLoading,
356
+ error,
357
+ refetch,
358
+ update,
359
+ delete: deleteUser
360
+ };
361
+ }
362
+
363
+ /**
364
+ * useProtected Hook
365
+ * Hook for protecting components that require authentication
366
+ */
367
+ /**
368
+ * Hook to check if user is authenticated and protect a component
369
+ *
370
+ * @returns {UseProtectedReturn} Authentication status
371
+ *
372
+ * @example
373
+ * ```tsx
374
+ * function ProtectedPage() {
375
+ * const { isAuthenticated, isLoading } = useProtected()
376
+ *
377
+ * if (isLoading) return <p>Loading...</p>
378
+ * if (!isAuthenticated) return <p>Please log in to access this page</p>
379
+ *
380
+ * return <div>Protected content</div>
381
+ * }
382
+ * ```
383
+ */
384
+ function useProtected() {
385
+ const { isAuthenticated, isLoading, user } = useAuth();
386
+ return {
387
+ isAuthenticated,
388
+ isLoading,
389
+ user
390
+ };
391
+ }
392
+
393
+ exports.GatelyContext = GatelyContext;
394
+ exports.GatelyProvider = GatelyProvider;
395
+ exports.useAuth = useAuth;
396
+ exports.useProtected = useProtected;
397
+ exports.useUser = useUser;