@fonderie/react-native-auth 0.5.0 → 0.6.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.
- package/dist/index.cjs +308 -89
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +44 -3
- package/dist/index.d.ts +44 -3
- package/dist/index.js +302 -87
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,16 +1,100 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import { FonderieApiError as
|
|
2
|
+
import { FonderieApiError as FonderieApiError12, isMfaRequired as isMfaRequired2 } from "@fonderie/client";
|
|
3
3
|
|
|
4
|
-
// src/hooks/
|
|
4
|
+
// src/hooks/useAccountData.ts
|
|
5
5
|
import { FonderieApiError } from "@fonderie/client";
|
|
6
6
|
import { useFonderieSubClient } from "@fonderie/react";
|
|
7
7
|
import { useCallback, useState } from "react";
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
|
|
9
|
+
// src/storage.ts
|
|
10
|
+
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
11
|
+
var TOKEN_KEY = "fonderie_access_token";
|
|
12
|
+
function persistToken(token) {
|
|
13
|
+
return AsyncStorage.setItem(TOKEN_KEY, token);
|
|
14
|
+
}
|
|
15
|
+
function clearToken() {
|
|
16
|
+
return AsyncStorage.removeItem(TOKEN_KEY);
|
|
17
|
+
}
|
|
18
|
+
function readToken() {
|
|
19
|
+
return AsyncStorage.getItem(TOKEN_KEY);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// src/hooks/useAccountData.ts
|
|
23
|
+
function useAccountData(client) {
|
|
24
|
+
const auth = useFonderieSubClient(client, (c) => c.auth, "useAccountData");
|
|
10
25
|
const [isLoading, setIsLoading] = useState(false);
|
|
11
26
|
const [error, setError] = useState(null);
|
|
12
|
-
const
|
|
13
|
-
|
|
27
|
+
const run = useCallback(async (op) => {
|
|
28
|
+
setIsLoading(true);
|
|
29
|
+
setError(null);
|
|
30
|
+
try {
|
|
31
|
+
return await op();
|
|
32
|
+
} catch (err) {
|
|
33
|
+
const apiError = err instanceof FonderieApiError ? err : new FonderieApiError("unknown", String(err), 0);
|
|
34
|
+
setError(apiError);
|
|
35
|
+
throw apiError;
|
|
36
|
+
} finally {
|
|
37
|
+
setIsLoading(false);
|
|
38
|
+
}
|
|
39
|
+
}, []);
|
|
40
|
+
const exportData = useCallback(
|
|
41
|
+
() => run(async () => {
|
|
42
|
+
const { result } = await auth.exportData();
|
|
43
|
+
return result;
|
|
44
|
+
}),
|
|
45
|
+
[auth, run]
|
|
46
|
+
);
|
|
47
|
+
const deleteUser = useCallback(
|
|
48
|
+
() => run(async () => {
|
|
49
|
+
await auth.deleteUser();
|
|
50
|
+
auth.setAccessToken(void 0);
|
|
51
|
+
await clearToken();
|
|
52
|
+
}),
|
|
53
|
+
[auth, run]
|
|
54
|
+
);
|
|
55
|
+
return { exportData, deleteUser, isLoading, error };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// src/hooks/useChangePassword.ts
|
|
59
|
+
import { FonderieApiError as FonderieApiError2 } from "@fonderie/client";
|
|
60
|
+
import { useFonderieSubClient as useFonderieSubClient2 } from "@fonderie/react";
|
|
61
|
+
import { useCallback as useCallback2, useState as useState2 } from "react";
|
|
62
|
+
function useChangePassword(client) {
|
|
63
|
+
const auth = useFonderieSubClient2(client, (c) => c.auth, "useChangePassword");
|
|
64
|
+
const [isLoading, setIsLoading] = useState2(false);
|
|
65
|
+
const [error, setError] = useState2(null);
|
|
66
|
+
const [done, setDone] = useState2(false);
|
|
67
|
+
const changePassword = useCallback2(
|
|
68
|
+
async (input) => {
|
|
69
|
+
setIsLoading(true);
|
|
70
|
+
setError(null);
|
|
71
|
+
setDone(false);
|
|
72
|
+
try {
|
|
73
|
+
await auth.changePassword(input);
|
|
74
|
+
setDone(true);
|
|
75
|
+
} catch (err) {
|
|
76
|
+
const apiError = err instanceof FonderieApiError2 ? err : new FonderieApiError2("unknown", String(err), 0);
|
|
77
|
+
setError(apiError);
|
|
78
|
+
throw apiError;
|
|
79
|
+
} finally {
|
|
80
|
+
setIsLoading(false);
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
[auth]
|
|
84
|
+
);
|
|
85
|
+
return { changePassword, isLoading, error, done };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// src/hooks/useForgotPassword.ts
|
|
89
|
+
import { FonderieApiError as FonderieApiError3 } from "@fonderie/client";
|
|
90
|
+
import { useFonderieSubClient as useFonderieSubClient3 } from "@fonderie/react";
|
|
91
|
+
import { useCallback as useCallback3, useState as useState3 } from "react";
|
|
92
|
+
function useForgotPassword(client) {
|
|
93
|
+
const auth = useFonderieSubClient3(client, (c) => c.auth, "useForgotPassword");
|
|
94
|
+
const [isLoading, setIsLoading] = useState3(false);
|
|
95
|
+
const [error, setError] = useState3(null);
|
|
96
|
+
const [sent, setSent] = useState3(false);
|
|
97
|
+
const forgotPassword = useCallback3(
|
|
14
98
|
async (email) => {
|
|
15
99
|
setIsLoading(true);
|
|
16
100
|
setError(null);
|
|
@@ -18,7 +102,7 @@ function useForgotPassword(client) {
|
|
|
18
102
|
await auth.forgotPassword(email);
|
|
19
103
|
setSent(true);
|
|
20
104
|
} catch (err) {
|
|
21
|
-
const apiError = err instanceof
|
|
105
|
+
const apiError = err instanceof FonderieApiError3 ? err : new FonderieApiError3("unknown", String(err), 0);
|
|
22
106
|
setError(apiError);
|
|
23
107
|
throw apiError;
|
|
24
108
|
} finally {
|
|
@@ -31,31 +115,16 @@ function useForgotPassword(client) {
|
|
|
31
115
|
}
|
|
32
116
|
|
|
33
117
|
// src/hooks/useLogin.ts
|
|
34
|
-
import { FonderieApiError as
|
|
35
|
-
import { useFonderieSubClient as
|
|
36
|
-
import { useCallback as
|
|
37
|
-
|
|
38
|
-
// src/storage.ts
|
|
39
|
-
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
40
|
-
var TOKEN_KEY = "fonderie_access_token";
|
|
41
|
-
function persistToken(token) {
|
|
42
|
-
return AsyncStorage.setItem(TOKEN_KEY, token);
|
|
43
|
-
}
|
|
44
|
-
function clearToken() {
|
|
45
|
-
return AsyncStorage.removeItem(TOKEN_KEY);
|
|
46
|
-
}
|
|
47
|
-
function readToken() {
|
|
48
|
-
return AsyncStorage.getItem(TOKEN_KEY);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// src/hooks/useLogin.ts
|
|
118
|
+
import { FonderieApiError as FonderieApiError4, isMfaRequired } from "@fonderie/client";
|
|
119
|
+
import { useFonderieSubClient as useFonderieSubClient4 } from "@fonderie/react";
|
|
120
|
+
import { useCallback as useCallback4, useState as useState4 } from "react";
|
|
52
121
|
function useLogin(client) {
|
|
53
|
-
const auth =
|
|
54
|
-
const [isLoading, setIsLoading] =
|
|
55
|
-
const [error, setError] =
|
|
56
|
-
const [data, setData] =
|
|
57
|
-
const [mfaPending, setMfaPending] =
|
|
58
|
-
const login =
|
|
122
|
+
const auth = useFonderieSubClient4(client, (c) => c.auth, "useLogin");
|
|
123
|
+
const [isLoading, setIsLoading] = useState4(false);
|
|
124
|
+
const [error, setError] = useState4(null);
|
|
125
|
+
const [data, setData] = useState4(null);
|
|
126
|
+
const [mfaPending, setMfaPending] = useState4(null);
|
|
127
|
+
const login = useCallback4(
|
|
59
128
|
async (input) => {
|
|
60
129
|
setIsLoading(true);
|
|
61
130
|
setError(null);
|
|
@@ -71,7 +140,7 @@ function useLogin(client) {
|
|
|
71
140
|
setData(result);
|
|
72
141
|
return result;
|
|
73
142
|
} catch (err) {
|
|
74
|
-
const apiError = err instanceof
|
|
143
|
+
const apiError = err instanceof FonderieApiError4 ? err : new FonderieApiError4("unknown", String(err), 0);
|
|
75
144
|
setError(apiError);
|
|
76
145
|
throw apiError;
|
|
77
146
|
} finally {
|
|
@@ -84,15 +153,15 @@ function useLogin(client) {
|
|
|
84
153
|
}
|
|
85
154
|
|
|
86
155
|
// src/hooks/useMfaLogin.ts
|
|
87
|
-
import { FonderieApiError as
|
|
88
|
-
import { useFonderieSubClient as
|
|
89
|
-
import { useCallback as
|
|
156
|
+
import { FonderieApiError as FonderieApiError5 } from "@fonderie/client";
|
|
157
|
+
import { useFonderieSubClient as useFonderieSubClient5 } from "@fonderie/react";
|
|
158
|
+
import { useCallback as useCallback5, useState as useState5 } from "react";
|
|
90
159
|
function useMfaLogin(client) {
|
|
91
|
-
const auth =
|
|
92
|
-
const [isLoading, setIsLoading] =
|
|
93
|
-
const [error, setError] =
|
|
94
|
-
const [data, setData] =
|
|
95
|
-
const verifyLogin =
|
|
160
|
+
const auth = useFonderieSubClient5(client, (c) => c.auth, "useMfaLogin");
|
|
161
|
+
const [isLoading, setIsLoading] = useState5(false);
|
|
162
|
+
const [error, setError] = useState5(null);
|
|
163
|
+
const [data, setData] = useState5(null);
|
|
164
|
+
const verifyLogin = useCallback5(
|
|
96
165
|
async (mfaToken, code) => {
|
|
97
166
|
setIsLoading(true);
|
|
98
167
|
setError(null);
|
|
@@ -103,7 +172,7 @@ function useMfaLogin(client) {
|
|
|
103
172
|
setData(result);
|
|
104
173
|
return result;
|
|
105
174
|
} catch (err) {
|
|
106
|
-
const apiError = err instanceof
|
|
175
|
+
const apiError = err instanceof FonderieApiError5 ? err : new FonderieApiError5("unknown", String(err), 0);
|
|
107
176
|
setError(apiError);
|
|
108
177
|
throw apiError;
|
|
109
178
|
} finally {
|
|
@@ -115,21 +184,147 @@ function useMfaLogin(client) {
|
|
|
115
184
|
return { verifyLogin, isLoading, error, data };
|
|
116
185
|
}
|
|
117
186
|
|
|
187
|
+
// src/hooks/useMfaSetup.ts
|
|
188
|
+
import { FonderieApiError as FonderieApiError6 } from "@fonderie/client";
|
|
189
|
+
import { useFonderieSubClient as useFonderieSubClient6 } from "@fonderie/react";
|
|
190
|
+
import { useCallback as useCallback6, useState as useState6 } from "react";
|
|
191
|
+
function useMfaSetup(client) {
|
|
192
|
+
const auth = useFonderieSubClient6(client, (c) => c.auth, "useMfaSetup");
|
|
193
|
+
const [setupData, setSetupData] = useState6(null);
|
|
194
|
+
const [isLoading, setIsLoading] = useState6(false);
|
|
195
|
+
const [error, setError] = useState6(null);
|
|
196
|
+
const run = useCallback6(async (op) => {
|
|
197
|
+
setIsLoading(true);
|
|
198
|
+
setError(null);
|
|
199
|
+
try {
|
|
200
|
+
return await op();
|
|
201
|
+
} catch (err) {
|
|
202
|
+
const apiError = err instanceof FonderieApiError6 ? err : new FonderieApiError6("unknown", String(err), 0);
|
|
203
|
+
setError(apiError);
|
|
204
|
+
throw apiError;
|
|
205
|
+
} finally {
|
|
206
|
+
setIsLoading(false);
|
|
207
|
+
}
|
|
208
|
+
}, []);
|
|
209
|
+
const setup = useCallback6(
|
|
210
|
+
() => run(async () => {
|
|
211
|
+
const { result } = await auth.mfa.setup();
|
|
212
|
+
setSetupData(result);
|
|
213
|
+
return result;
|
|
214
|
+
}),
|
|
215
|
+
[auth, run]
|
|
216
|
+
);
|
|
217
|
+
const verify = useCallback6(
|
|
218
|
+
(code) => run(async () => {
|
|
219
|
+
const { result } = await auth.mfa.verify(code);
|
|
220
|
+
auth.setAccessToken(result.tokens.access);
|
|
221
|
+
await persistToken(result.tokens.access);
|
|
222
|
+
return result;
|
|
223
|
+
}),
|
|
224
|
+
[auth, run]
|
|
225
|
+
);
|
|
226
|
+
const disable = useCallback6(
|
|
227
|
+
(code) => run(async () => {
|
|
228
|
+
await auth.mfa.disable(code);
|
|
229
|
+
}),
|
|
230
|
+
[auth, run]
|
|
231
|
+
);
|
|
232
|
+
const regenerateBackupCodes = useCallback6(
|
|
233
|
+
(code) => run(async () => {
|
|
234
|
+
const { result } = await auth.mfa.regenerateBackupCodes(code);
|
|
235
|
+
return result.backupCodes;
|
|
236
|
+
}),
|
|
237
|
+
[auth, run]
|
|
238
|
+
);
|
|
239
|
+
return { setup, setupData, verify, disable, regenerateBackupCodes, isLoading, error };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// src/hooks/useProfile.ts
|
|
243
|
+
import { FonderieApiError as FonderieApiError7 } from "@fonderie/client";
|
|
244
|
+
import { useFonderieSubClient as useFonderieSubClient7 } from "@fonderie/react";
|
|
245
|
+
import { useCallback as useCallback7, useEffect, useState as useState7 } from "react";
|
|
246
|
+
function useProfile(client) {
|
|
247
|
+
const auth = useFonderieSubClient7(client, (c) => c.auth, "useProfile");
|
|
248
|
+
const [user, setUser] = useState7(null);
|
|
249
|
+
const [isLoading, setIsLoading] = useState7(true);
|
|
250
|
+
const [error, setError] = useState7(null);
|
|
251
|
+
const refresh = useCallback7(async () => {
|
|
252
|
+
setIsLoading(true);
|
|
253
|
+
setError(null);
|
|
254
|
+
try {
|
|
255
|
+
const { result } = await auth.getUser();
|
|
256
|
+
setUser(result.user);
|
|
257
|
+
} catch (err) {
|
|
258
|
+
const apiError = err instanceof FonderieApiError7 ? err : new FonderieApiError7("unknown", String(err), 0);
|
|
259
|
+
setError(apiError);
|
|
260
|
+
} finally {
|
|
261
|
+
setIsLoading(false);
|
|
262
|
+
}
|
|
263
|
+
}, [auth]);
|
|
264
|
+
const mutate = useCallback7(
|
|
265
|
+
async (op) => {
|
|
266
|
+
setError(null);
|
|
267
|
+
try {
|
|
268
|
+
return await op();
|
|
269
|
+
} catch (err) {
|
|
270
|
+
const apiError = err instanceof FonderieApiError7 ? err : new FonderieApiError7("unknown", String(err), 0);
|
|
271
|
+
setError(apiError);
|
|
272
|
+
throw apiError;
|
|
273
|
+
}
|
|
274
|
+
},
|
|
275
|
+
[]
|
|
276
|
+
);
|
|
277
|
+
const updateProfile = useCallback7(
|
|
278
|
+
(input) => mutate(async () => {
|
|
279
|
+
const { result } = await auth.updateProfile(input);
|
|
280
|
+
setUser(result.user);
|
|
281
|
+
return result.user;
|
|
282
|
+
}),
|
|
283
|
+
[auth, mutate]
|
|
284
|
+
);
|
|
285
|
+
const updatePreferences = useCallback7(
|
|
286
|
+
(input) => mutate(async () => {
|
|
287
|
+
const { result } = await auth.updatePreferences(input);
|
|
288
|
+
setUser(result.user);
|
|
289
|
+
return result.user;
|
|
290
|
+
}),
|
|
291
|
+
[auth, mutate]
|
|
292
|
+
);
|
|
293
|
+
const updateEmail = useCallback7(
|
|
294
|
+
(email) => mutate(async () => {
|
|
295
|
+
await auth.updateEmail(email);
|
|
296
|
+
await refresh();
|
|
297
|
+
}),
|
|
298
|
+
[auth, mutate, refresh]
|
|
299
|
+
);
|
|
300
|
+
const updatePhone = useCallback7(
|
|
301
|
+
(phone) => mutate(async () => {
|
|
302
|
+
await auth.updatePhone(phone);
|
|
303
|
+
await refresh();
|
|
304
|
+
}),
|
|
305
|
+
[auth, mutate, refresh]
|
|
306
|
+
);
|
|
307
|
+
useEffect(() => {
|
|
308
|
+
void refresh();
|
|
309
|
+
}, [refresh]);
|
|
310
|
+
return { user, refresh, updateProfile, updatePreferences, updateEmail, updatePhone, isLoading, error };
|
|
311
|
+
}
|
|
312
|
+
|
|
118
313
|
// src/hooks/useLogout.ts
|
|
119
|
-
import { FonderieApiError as
|
|
120
|
-
import { useFonderieSubClient as
|
|
121
|
-
import { useCallback as
|
|
314
|
+
import { FonderieApiError as FonderieApiError8 } from "@fonderie/client";
|
|
315
|
+
import { useFonderieSubClient as useFonderieSubClient8 } from "@fonderie/react";
|
|
316
|
+
import { useCallback as useCallback8, useState as useState8 } from "react";
|
|
122
317
|
function useLogout(client) {
|
|
123
|
-
const auth =
|
|
124
|
-
const [isLoading, setIsLoading] =
|
|
125
|
-
const [error, setError] =
|
|
126
|
-
const logout =
|
|
318
|
+
const auth = useFonderieSubClient8(client, (c) => c.auth, "useLogout");
|
|
319
|
+
const [isLoading, setIsLoading] = useState8(false);
|
|
320
|
+
const [error, setError] = useState8(null);
|
|
321
|
+
const logout = useCallback8(async (refreshToken) => {
|
|
127
322
|
setIsLoading(true);
|
|
128
323
|
setError(null);
|
|
129
324
|
try {
|
|
130
325
|
await auth.logout(refreshToken);
|
|
131
326
|
} catch (err) {
|
|
132
|
-
const apiError = err instanceof
|
|
327
|
+
const apiError = err instanceof FonderieApiError8 ? err : new FonderieApiError8("unknown", String(err), 0);
|
|
133
328
|
setError(apiError);
|
|
134
329
|
} finally {
|
|
135
330
|
auth.setAccessToken(void 0);
|
|
@@ -141,15 +336,15 @@ function useLogout(client) {
|
|
|
141
336
|
}
|
|
142
337
|
|
|
143
338
|
// src/hooks/useRegister.ts
|
|
144
|
-
import { FonderieApiError as
|
|
145
|
-
import { useFonderieSubClient as
|
|
146
|
-
import { useCallback as
|
|
339
|
+
import { FonderieApiError as FonderieApiError9 } from "@fonderie/client";
|
|
340
|
+
import { useFonderieSubClient as useFonderieSubClient9 } from "@fonderie/react";
|
|
341
|
+
import { useCallback as useCallback9, useState as useState9 } from "react";
|
|
147
342
|
function useRegister(client) {
|
|
148
|
-
const auth =
|
|
149
|
-
const [isLoading, setIsLoading] =
|
|
150
|
-
const [error, setError] =
|
|
151
|
-
const [data, setData] =
|
|
152
|
-
const register =
|
|
343
|
+
const auth = useFonderieSubClient9(client, (c) => c.auth, "useRegister");
|
|
344
|
+
const [isLoading, setIsLoading] = useState9(false);
|
|
345
|
+
const [error, setError] = useState9(null);
|
|
346
|
+
const [data, setData] = useState9(null);
|
|
347
|
+
const register = useCallback9(
|
|
153
348
|
async (input) => {
|
|
154
349
|
setIsLoading(true);
|
|
155
350
|
setError(null);
|
|
@@ -160,7 +355,7 @@ function useRegister(client) {
|
|
|
160
355
|
setData(result);
|
|
161
356
|
return result;
|
|
162
357
|
} catch (err) {
|
|
163
|
-
const apiError = err instanceof
|
|
358
|
+
const apiError = err instanceof FonderieApiError9 ? err : new FonderieApiError9("unknown", String(err), 0);
|
|
164
359
|
setError(apiError);
|
|
165
360
|
throw apiError;
|
|
166
361
|
} finally {
|
|
@@ -173,15 +368,15 @@ function useRegister(client) {
|
|
|
173
368
|
}
|
|
174
369
|
|
|
175
370
|
// src/hooks/useResetPassword.ts
|
|
176
|
-
import { FonderieApiError as
|
|
177
|
-
import { useFonderieSubClient as
|
|
178
|
-
import { useCallback as
|
|
371
|
+
import { FonderieApiError as FonderieApiError10 } from "@fonderie/client";
|
|
372
|
+
import { useFonderieSubClient as useFonderieSubClient10 } from "@fonderie/react";
|
|
373
|
+
import { useCallback as useCallback10, useState as useState10 } from "react";
|
|
179
374
|
function useResetPassword(client) {
|
|
180
|
-
const auth =
|
|
181
|
-
const [isLoading, setIsLoading] =
|
|
182
|
-
const [error, setError] =
|
|
183
|
-
const [done, setDone] =
|
|
184
|
-
const resetPassword =
|
|
375
|
+
const auth = useFonderieSubClient10(client, (c) => c.auth, "useResetPassword");
|
|
376
|
+
const [isLoading, setIsLoading] = useState10(false);
|
|
377
|
+
const [error, setError] = useState10(null);
|
|
378
|
+
const [done, setDone] = useState10(false);
|
|
379
|
+
const resetPassword = useCallback10(
|
|
185
380
|
async (input) => {
|
|
186
381
|
setIsLoading(true);
|
|
187
382
|
setError(null);
|
|
@@ -189,7 +384,7 @@ function useResetPassword(client) {
|
|
|
189
384
|
await auth.resetPassword(input);
|
|
190
385
|
setDone(true);
|
|
191
386
|
} catch (err) {
|
|
192
|
-
const apiError = err instanceof
|
|
387
|
+
const apiError = err instanceof FonderieApiError10 ? err : new FonderieApiError10("unknown", String(err), 0);
|
|
193
388
|
setError(apiError);
|
|
194
389
|
throw apiError;
|
|
195
390
|
} finally {
|
|
@@ -202,14 +397,14 @@ function useResetPassword(client) {
|
|
|
202
397
|
}
|
|
203
398
|
|
|
204
399
|
// src/hooks/useSession.ts
|
|
205
|
-
import { useFonderieSubClient as
|
|
206
|
-
import { useCallback as
|
|
400
|
+
import { useFonderieSubClient as useFonderieSubClient11 } from "@fonderie/react";
|
|
401
|
+
import { useCallback as useCallback11, useEffect as useEffect2, useState as useState11 } from "react";
|
|
207
402
|
function useSession(client) {
|
|
208
|
-
const auth =
|
|
209
|
-
const [user, setUser] =
|
|
210
|
-
const [isLoading, setIsLoading] =
|
|
211
|
-
const [isAuthenticated, setIsAuthenticated] =
|
|
212
|
-
const refresh =
|
|
403
|
+
const auth = useFonderieSubClient11(client, (c) => c.auth, "useSession");
|
|
404
|
+
const [user, setUser] = useState11(null);
|
|
405
|
+
const [isLoading, setIsLoading] = useState11(true);
|
|
406
|
+
const [isAuthenticated, setIsAuthenticated] = useState11(false);
|
|
407
|
+
const refresh = useCallback11(async () => {
|
|
213
408
|
setIsLoading(true);
|
|
214
409
|
try {
|
|
215
410
|
const { result } = await auth.getUser();
|
|
@@ -224,7 +419,7 @@ function useSession(client) {
|
|
|
224
419
|
setIsLoading(false);
|
|
225
420
|
}
|
|
226
421
|
}, [auth]);
|
|
227
|
-
const logout =
|
|
422
|
+
const logout = useCallback11(async (refreshToken) => {
|
|
228
423
|
try {
|
|
229
424
|
await auth.logout(refreshToken);
|
|
230
425
|
} catch {
|
|
@@ -234,7 +429,7 @@ function useSession(client) {
|
|
|
234
429
|
setUser(null);
|
|
235
430
|
setIsAuthenticated(false);
|
|
236
431
|
}, [auth]);
|
|
237
|
-
|
|
432
|
+
useEffect2(() => {
|
|
238
433
|
readToken().then((token) => {
|
|
239
434
|
if (token) auth.setAccessToken(token);
|
|
240
435
|
void refresh();
|
|
@@ -244,15 +439,31 @@ function useSession(client) {
|
|
|
244
439
|
}
|
|
245
440
|
|
|
246
441
|
// src/hooks/useVerifyEmail.ts
|
|
247
|
-
import { FonderieApiError as
|
|
248
|
-
import { useFonderieSubClient as
|
|
249
|
-
import { useCallback as
|
|
442
|
+
import { FonderieApiError as FonderieApiError11 } from "@fonderie/client";
|
|
443
|
+
import { useFonderieSubClient as useFonderieSubClient12 } from "@fonderie/react";
|
|
444
|
+
import { useCallback as useCallback12, useState as useState12 } from "react";
|
|
250
445
|
function useVerifyEmail(client) {
|
|
251
|
-
const auth =
|
|
252
|
-
const [isLoading, setIsLoading] =
|
|
253
|
-
const [error, setError] =
|
|
254
|
-
const [data, setData] =
|
|
255
|
-
const
|
|
446
|
+
const auth = useFonderieSubClient12(client, (c) => c.auth, "useVerifyEmail");
|
|
447
|
+
const [isLoading, setIsLoading] = useState12(false);
|
|
448
|
+
const [error, setError] = useState12(null);
|
|
449
|
+
const [data, setData] = useState12(null);
|
|
450
|
+
const [resent, setResent] = useState12(false);
|
|
451
|
+
const resend = useCallback12(async () => {
|
|
452
|
+
setIsLoading(true);
|
|
453
|
+
setError(null);
|
|
454
|
+
setResent(false);
|
|
455
|
+
try {
|
|
456
|
+
await auth.sendVerificationEmail();
|
|
457
|
+
setResent(true);
|
|
458
|
+
} catch (err) {
|
|
459
|
+
const apiError = err instanceof FonderieApiError11 ? err : new FonderieApiError11("unknown", String(err), 0);
|
|
460
|
+
setError(apiError);
|
|
461
|
+
throw apiError;
|
|
462
|
+
} finally {
|
|
463
|
+
setIsLoading(false);
|
|
464
|
+
}
|
|
465
|
+
}, [auth]);
|
|
466
|
+
const verifyEmail = useCallback12(
|
|
256
467
|
async (pin) => {
|
|
257
468
|
setIsLoading(true);
|
|
258
469
|
setError(null);
|
|
@@ -261,7 +472,7 @@ function useVerifyEmail(client) {
|
|
|
261
472
|
setData(result);
|
|
262
473
|
return result;
|
|
263
474
|
} catch (err) {
|
|
264
|
-
const apiError = err instanceof
|
|
475
|
+
const apiError = err instanceof FonderieApiError11 ? err : new FonderieApiError11("unknown", String(err), 0);
|
|
265
476
|
setError(apiError);
|
|
266
477
|
throw apiError;
|
|
267
478
|
} finally {
|
|
@@ -270,19 +481,23 @@ function useVerifyEmail(client) {
|
|
|
270
481
|
},
|
|
271
482
|
[auth]
|
|
272
483
|
);
|
|
273
|
-
return { verifyEmail, isLoading, error, data };
|
|
484
|
+
return { verifyEmail, resend, resent, isLoading, error, data };
|
|
274
485
|
}
|
|
275
486
|
export {
|
|
276
|
-
|
|
487
|
+
FonderieApiError12 as FonderieApiError,
|
|
277
488
|
TOKEN_KEY,
|
|
278
489
|
clearToken,
|
|
279
490
|
isMfaRequired2 as isMfaRequired,
|
|
280
491
|
persistToken,
|
|
281
492
|
readToken,
|
|
493
|
+
useAccountData,
|
|
494
|
+
useChangePassword,
|
|
282
495
|
useForgotPassword,
|
|
283
496
|
useLogin,
|
|
284
497
|
useLogout,
|
|
285
498
|
useMfaLogin,
|
|
499
|
+
useMfaSetup,
|
|
500
|
+
useProfile,
|
|
286
501
|
useRegister,
|
|
287
502
|
useResetPassword,
|
|
288
503
|
useSession,
|