@fonderie/react-native-auth 0.1.0 → 0.3.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/README.md +23 -4
- package/dist/index.cjs +80 -60
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -11
- package/dist/index.d.ts +12 -11
- package/dist/index.js +47 -27
- package/dist/index.js.map +1 -1
- package/package.json +12 -9
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
React Native hooks for Fonderie auth — `useLogin`, `useRegister`,
|
|
4
4
|
`useSession`, `useLogout`, `useForgotPassword`, `useResetPassword`, and
|
|
5
5
|
`useVerifyEmail`. Same shape as
|
|
6
|
-
[`@fonderie/react-auth`](https://github.com/
|
|
6
|
+
[`@fonderie/react-auth`](https://github.com/fonderiejs/sdk/tree/main/packages/react-auth),
|
|
7
7
|
with the access token persisted to `AsyncStorage` instead of `localStorage`.
|
|
8
8
|
|
|
9
9
|
## Install
|
|
@@ -12,6 +12,25 @@ with the access token persisted to `AsyncStorage` instead of `localStorage`.
|
|
|
12
12
|
npm install @fonderie/react-native-auth @react-native-async-storage/async-storage
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
+
## One client at the root (recommended)
|
|
16
|
+
|
|
17
|
+
Wrap your app in `FonderieProvider` from [`@fonderie/react`](../react) once and
|
|
18
|
+
every hook resolves the client from context — no client argument at call sites:
|
|
19
|
+
|
|
20
|
+
```tsx
|
|
21
|
+
import { FonderieProvider } from "@fonderie/react";
|
|
22
|
+
|
|
23
|
+
<FonderieProvider client={client}>
|
|
24
|
+
<App />
|
|
25
|
+
</FonderieProvider>;
|
|
26
|
+
|
|
27
|
+
// anywhere below it:
|
|
28
|
+
const { ...state } = useLogin();
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Passing a client explicitly (as below) still works everywhere and takes
|
|
32
|
+
precedence over context — handy in tests and multi-client apps.
|
|
33
|
+
|
|
15
34
|
## Use
|
|
16
35
|
|
|
17
36
|
```tsx
|
|
@@ -43,14 +62,14 @@ The access token is persisted to `AsyncStorage` and restored automatically by
|
|
|
43
62
|
`useSession` on mount; don't re-implement that in app code.
|
|
44
63
|
|
|
45
64
|
Want pre-built screens instead of wiring your own form?
|
|
46
|
-
See [`@fonderie/react-native-auth-screens`](https://github.com/
|
|
65
|
+
See [`@fonderie/react-native-auth-screens`](https://github.com/fonderiejs/sdk/tree/main/packages/react-native-auth-screens).
|
|
47
66
|
|
|
48
67
|
## Why this exists
|
|
49
68
|
|
|
50
69
|
You've shipped this plumbing before — auth, teams, billing, messaging —
|
|
51
70
|
and the next project will ask for it again. Fonderie packages it once:
|
|
52
71
|
plain TypeScript modules for
|
|
53
|
-
[`@fonderie/core`](https://github.com/
|
|
72
|
+
[`@fonderie/core`](https://github.com/fonderiejs/sdk/tree/main/packages/core),
|
|
54
73
|
PostgreSQL-backed, self-hosted, MIT. No external control plane, no
|
|
55
74
|
per-seat anything. Register the modules you need; skip the ones you don't.
|
|
56
75
|
|
|
@@ -59,7 +78,7 @@ around `@fonderie/client`, nothing more. No business logic lives here; it
|
|
|
59
78
|
lives in `@fonderie/auth` on the server.
|
|
60
79
|
|
|
61
80
|
Browse the whole set at
|
|
62
|
-
[
|
|
81
|
+
[fonderiejs/sdk](https://github.com/fonderiejs/sdk) · follow
|
|
63
82
|
[@fonderiejs](https://x.com/fonderiejs)
|
|
64
83
|
|
|
65
84
|
## License
|
package/dist/index.cjs
CHANGED
|
@@ -44,17 +44,19 @@ var import_client7 = require("@fonderie/client");
|
|
|
44
44
|
|
|
45
45
|
// src/hooks/useForgotPassword.ts
|
|
46
46
|
var import_client = require("@fonderie/client");
|
|
47
|
-
var import_react = require("react");
|
|
47
|
+
var import_react = require("@fonderie/react");
|
|
48
|
+
var import_react2 = require("react");
|
|
48
49
|
function useForgotPassword(client) {
|
|
49
|
-
const
|
|
50
|
-
const [
|
|
51
|
-
const [
|
|
52
|
-
const
|
|
50
|
+
const auth = (0, import_react.useFonderieSubClient)(client, (c) => c.auth, "useForgotPassword");
|
|
51
|
+
const [isLoading, setIsLoading] = (0, import_react2.useState)(false);
|
|
52
|
+
const [error, setError] = (0, import_react2.useState)(null);
|
|
53
|
+
const [sent, setSent] = (0, import_react2.useState)(false);
|
|
54
|
+
const forgotPassword = (0, import_react2.useCallback)(
|
|
53
55
|
async (email) => {
|
|
54
56
|
setIsLoading(true);
|
|
55
57
|
setError(null);
|
|
56
58
|
try {
|
|
57
|
-
await
|
|
59
|
+
await auth.forgotPassword(email);
|
|
58
60
|
setSent(true);
|
|
59
61
|
} catch (err) {
|
|
60
62
|
const apiError = err instanceof import_client.FonderieApiError ? err : new import_client.FonderieApiError("unknown", String(err), 0);
|
|
@@ -64,14 +66,15 @@ function useForgotPassword(client) {
|
|
|
64
66
|
setIsLoading(false);
|
|
65
67
|
}
|
|
66
68
|
},
|
|
67
|
-
[
|
|
69
|
+
[auth]
|
|
68
70
|
);
|
|
69
71
|
return { forgotPassword, isLoading, error, sent };
|
|
70
72
|
}
|
|
71
73
|
|
|
72
74
|
// src/hooks/useLogin.ts
|
|
73
75
|
var import_client2 = require("@fonderie/client");
|
|
74
|
-
var
|
|
76
|
+
var import_react3 = require("@fonderie/react");
|
|
77
|
+
var import_react4 = require("react");
|
|
75
78
|
|
|
76
79
|
// src/storage.ts
|
|
77
80
|
var import_async_storage = __toESM(require("@react-native-async-storage/async-storage"), 1);
|
|
@@ -88,16 +91,23 @@ function readToken() {
|
|
|
88
91
|
|
|
89
92
|
// src/hooks/useLogin.ts
|
|
90
93
|
function useLogin(client) {
|
|
91
|
-
const
|
|
92
|
-
const [
|
|
93
|
-
const [
|
|
94
|
-
const
|
|
94
|
+
const auth = (0, import_react3.useFonderieSubClient)(client, (c) => c.auth, "useLogin");
|
|
95
|
+
const [isLoading, setIsLoading] = (0, import_react4.useState)(false);
|
|
96
|
+
const [error, setError] = (0, import_react4.useState)(null);
|
|
97
|
+
const [data, setData] = (0, import_react4.useState)(null);
|
|
98
|
+
const [mfaPending, setMfaPending] = (0, import_react4.useState)(null);
|
|
99
|
+
const login = (0, import_react4.useCallback)(
|
|
95
100
|
async (input) => {
|
|
96
101
|
setIsLoading(true);
|
|
97
102
|
setError(null);
|
|
103
|
+
setMfaPending(null);
|
|
98
104
|
try {
|
|
99
|
-
const { result } = await
|
|
100
|
-
|
|
105
|
+
const { result } = await auth.login(input);
|
|
106
|
+
if ((0, import_client2.isMfaRequired)(result)) {
|
|
107
|
+
setMfaPending(result);
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
auth.setAccessToken(result.tokens.access);
|
|
101
111
|
await persistToken(result.tokens.access);
|
|
102
112
|
setData(result);
|
|
103
113
|
return result;
|
|
@@ -109,48 +119,52 @@ function useLogin(client) {
|
|
|
109
119
|
setIsLoading(false);
|
|
110
120
|
}
|
|
111
121
|
},
|
|
112
|
-
[
|
|
122
|
+
[auth]
|
|
113
123
|
);
|
|
114
|
-
return { login, isLoading, error, data };
|
|
124
|
+
return { login, isLoading, error, data, mfaPending };
|
|
115
125
|
}
|
|
116
126
|
|
|
117
127
|
// src/hooks/useLogout.ts
|
|
118
128
|
var import_client3 = require("@fonderie/client");
|
|
119
|
-
var
|
|
129
|
+
var import_react5 = require("@fonderie/react");
|
|
130
|
+
var import_react6 = require("react");
|
|
120
131
|
function useLogout(client) {
|
|
121
|
-
const
|
|
122
|
-
const [
|
|
123
|
-
const
|
|
132
|
+
const auth = (0, import_react5.useFonderieSubClient)(client, (c) => c.auth, "useLogout");
|
|
133
|
+
const [isLoading, setIsLoading] = (0, import_react6.useState)(false);
|
|
134
|
+
const [error, setError] = (0, import_react6.useState)(null);
|
|
135
|
+
const logout = (0, import_react6.useCallback)(async (refreshToken) => {
|
|
124
136
|
setIsLoading(true);
|
|
125
137
|
setError(null);
|
|
126
138
|
try {
|
|
127
|
-
await
|
|
139
|
+
await auth.logout(refreshToken);
|
|
128
140
|
} catch (err) {
|
|
129
141
|
const apiError = err instanceof import_client3.FonderieApiError ? err : new import_client3.FonderieApiError("unknown", String(err), 0);
|
|
130
142
|
setError(apiError);
|
|
131
143
|
} finally {
|
|
132
|
-
|
|
144
|
+
auth.setAccessToken(void 0);
|
|
133
145
|
await clearToken();
|
|
134
146
|
setIsLoading(false);
|
|
135
147
|
}
|
|
136
|
-
}, [
|
|
148
|
+
}, [auth]);
|
|
137
149
|
return { logout, isLoading, error };
|
|
138
150
|
}
|
|
139
151
|
|
|
140
152
|
// src/hooks/useRegister.ts
|
|
141
153
|
var import_client4 = require("@fonderie/client");
|
|
142
|
-
var
|
|
154
|
+
var import_react7 = require("@fonderie/react");
|
|
155
|
+
var import_react8 = require("react");
|
|
143
156
|
function useRegister(client) {
|
|
144
|
-
const
|
|
145
|
-
const [
|
|
146
|
-
const [
|
|
147
|
-
const
|
|
157
|
+
const auth = (0, import_react7.useFonderieSubClient)(client, (c) => c.auth, "useRegister");
|
|
158
|
+
const [isLoading, setIsLoading] = (0, import_react8.useState)(false);
|
|
159
|
+
const [error, setError] = (0, import_react8.useState)(null);
|
|
160
|
+
const [data, setData] = (0, import_react8.useState)(null);
|
|
161
|
+
const register = (0, import_react8.useCallback)(
|
|
148
162
|
async (input) => {
|
|
149
163
|
setIsLoading(true);
|
|
150
164
|
setError(null);
|
|
151
165
|
try {
|
|
152
|
-
const { result } = await
|
|
153
|
-
|
|
166
|
+
const { result } = await auth.register(input);
|
|
167
|
+
auth.setAccessToken(result.tokens.access);
|
|
154
168
|
await persistToken(result.tokens.access);
|
|
155
169
|
setData(result);
|
|
156
170
|
return result;
|
|
@@ -162,24 +176,26 @@ function useRegister(client) {
|
|
|
162
176
|
setIsLoading(false);
|
|
163
177
|
}
|
|
164
178
|
},
|
|
165
|
-
[
|
|
179
|
+
[auth]
|
|
166
180
|
);
|
|
167
181
|
return { register, isLoading, error, data };
|
|
168
182
|
}
|
|
169
183
|
|
|
170
184
|
// src/hooks/useResetPassword.ts
|
|
171
185
|
var import_client5 = require("@fonderie/client");
|
|
172
|
-
var
|
|
186
|
+
var import_react9 = require("@fonderie/react");
|
|
187
|
+
var import_react10 = require("react");
|
|
173
188
|
function useResetPassword(client) {
|
|
174
|
-
const
|
|
175
|
-
const [
|
|
176
|
-
const [
|
|
177
|
-
const
|
|
189
|
+
const auth = (0, import_react9.useFonderieSubClient)(client, (c) => c.auth, "useResetPassword");
|
|
190
|
+
const [isLoading, setIsLoading] = (0, import_react10.useState)(false);
|
|
191
|
+
const [error, setError] = (0, import_react10.useState)(null);
|
|
192
|
+
const [done, setDone] = (0, import_react10.useState)(false);
|
|
193
|
+
const resetPassword = (0, import_react10.useCallback)(
|
|
178
194
|
async (input) => {
|
|
179
195
|
setIsLoading(true);
|
|
180
196
|
setError(null);
|
|
181
197
|
try {
|
|
182
|
-
await
|
|
198
|
+
await auth.resetPassword(input);
|
|
183
199
|
setDone(true);
|
|
184
200
|
} catch (err) {
|
|
185
201
|
const apiError = err instanceof import_client5.FonderieApiError ? err : new import_client5.FonderieApiError("unknown", String(err), 0);
|
|
@@ -189,64 +205,68 @@ function useResetPassword(client) {
|
|
|
189
205
|
setIsLoading(false);
|
|
190
206
|
}
|
|
191
207
|
},
|
|
192
|
-
[
|
|
208
|
+
[auth]
|
|
193
209
|
);
|
|
194
210
|
return { resetPassword, isLoading, error, done };
|
|
195
211
|
}
|
|
196
212
|
|
|
197
213
|
// src/hooks/useSession.ts
|
|
198
|
-
var
|
|
214
|
+
var import_react11 = require("@fonderie/react");
|
|
215
|
+
var import_react12 = require("react");
|
|
199
216
|
function useSession(client) {
|
|
200
|
-
const
|
|
201
|
-
const [
|
|
202
|
-
const [
|
|
203
|
-
const
|
|
217
|
+
const auth = (0, import_react11.useFonderieSubClient)(client, (c) => c.auth, "useSession");
|
|
218
|
+
const [user, setUser] = (0, import_react12.useState)(null);
|
|
219
|
+
const [isLoading, setIsLoading] = (0, import_react12.useState)(true);
|
|
220
|
+
const [isAuthenticated, setIsAuthenticated] = (0, import_react12.useState)(false);
|
|
221
|
+
const refresh = (0, import_react12.useCallback)(async () => {
|
|
204
222
|
setIsLoading(true);
|
|
205
223
|
try {
|
|
206
|
-
const { result } = await
|
|
224
|
+
const { result } = await auth.getUser();
|
|
207
225
|
setUser(result.user);
|
|
208
226
|
setIsAuthenticated(true);
|
|
209
227
|
} catch {
|
|
210
228
|
setUser(null);
|
|
211
229
|
setIsAuthenticated(false);
|
|
212
|
-
|
|
230
|
+
auth.setAccessToken(void 0);
|
|
213
231
|
await clearToken();
|
|
214
232
|
} finally {
|
|
215
233
|
setIsLoading(false);
|
|
216
234
|
}
|
|
217
|
-
}, [
|
|
218
|
-
const logout = (0,
|
|
235
|
+
}, [auth]);
|
|
236
|
+
const logout = (0, import_react12.useCallback)(async (refreshToken) => {
|
|
219
237
|
try {
|
|
220
|
-
await
|
|
238
|
+
await auth.logout(refreshToken);
|
|
221
239
|
} catch {
|
|
222
240
|
}
|
|
223
|
-
|
|
241
|
+
auth.setAccessToken(void 0);
|
|
224
242
|
await clearToken();
|
|
225
243
|
setUser(null);
|
|
226
244
|
setIsAuthenticated(false);
|
|
227
|
-
}, [
|
|
228
|
-
(0,
|
|
245
|
+
}, [auth]);
|
|
246
|
+
(0, import_react12.useEffect)(() => {
|
|
229
247
|
readToken().then((token) => {
|
|
230
|
-
if (token)
|
|
248
|
+
if (token) auth.setAccessToken(token);
|
|
231
249
|
void refresh();
|
|
232
250
|
});
|
|
233
|
-
}, [
|
|
251
|
+
}, [auth, refresh]);
|
|
234
252
|
return { user, isLoading, isAuthenticated, refresh, logout };
|
|
235
253
|
}
|
|
236
254
|
|
|
237
255
|
// src/hooks/useVerifyEmail.ts
|
|
238
256
|
var import_client6 = require("@fonderie/client");
|
|
239
|
-
var
|
|
257
|
+
var import_react13 = require("@fonderie/react");
|
|
258
|
+
var import_react14 = require("react");
|
|
240
259
|
function useVerifyEmail(client) {
|
|
241
|
-
const
|
|
242
|
-
const [
|
|
243
|
-
const [
|
|
244
|
-
const
|
|
260
|
+
const auth = (0, import_react13.useFonderieSubClient)(client, (c) => c.auth, "useVerifyEmail");
|
|
261
|
+
const [isLoading, setIsLoading] = (0, import_react14.useState)(false);
|
|
262
|
+
const [error, setError] = (0, import_react14.useState)(null);
|
|
263
|
+
const [data, setData] = (0, import_react14.useState)(null);
|
|
264
|
+
const verifyEmail = (0, import_react14.useCallback)(
|
|
245
265
|
async (pin) => {
|
|
246
266
|
setIsLoading(true);
|
|
247
267
|
setError(null);
|
|
248
268
|
try {
|
|
249
|
-
const { result } = await
|
|
269
|
+
const { result } = await auth.verifyEmail(pin);
|
|
250
270
|
setData(result);
|
|
251
271
|
return result;
|
|
252
272
|
} catch (err) {
|
|
@@ -257,7 +277,7 @@ function useVerifyEmail(client) {
|
|
|
257
277
|
setIsLoading(false);
|
|
258
278
|
}
|
|
259
279
|
},
|
|
260
|
-
[
|
|
280
|
+
[auth]
|
|
261
281
|
);
|
|
262
282
|
return { verifyEmail, isLoading, error, data };
|
|
263
283
|
}
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/hooks/useForgotPassword.ts","../src/hooks/useLogin.ts","../src/storage.ts","../src/hooks/useLogout.ts","../src/hooks/useRegister.ts","../src/hooks/useResetPassword.ts","../src/hooks/useSession.ts","../src/hooks/useVerifyEmail.ts"],"sourcesContent":["export type {\n\tAuthClient,\n\tILoginInput,\n\tILoginResult,\n\tIRegisterInput,\n\tIRegisterResult,\n\tIResetPasswordInput,\n\tITokens,\n\tIUserDTO,\n\tIVerifyEmailResult,\n} from '@fonderie/client';\n\nexport { FonderieApiError } from '@fonderie/client';\nexport type {\n\tIUseForgotPasswordReturn,\n\tIUseLoginReturn,\n\tIUseLogoutReturn,\n\tIUseRegisterReturn,\n\tIUseResetPasswordReturn,\n\tIUseSessionReturn,\n\tIUseVerifyEmailReturn,\n} from './hooks';\nexport {\n\tuseForgotPassword,\n\tuseLogin,\n\tuseLogout,\n\tuseRegister,\n\tuseResetPassword,\n\tuseSession,\n\tuseVerifyEmail,\n} from './hooks';\n","import type { AuthClient } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useCallback, useState } from 'react';\n\nexport interface IUseForgotPasswordReturn {\n\tforgotPassword: (email: string) => Promise<void>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tsent: boolean;\n}\n\nexport function useForgotPassword(client: AuthClient): IUseForgotPasswordReturn {\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [sent, setSent] = useState(false);\n\n\tconst forgotPassword = useCallback(\n\t\tasync (email: string) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\ttry {\n\t\t\t\tawait client.forgotPassword(email);\n\t\t\t\tsetSent(true);\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[client],\n\t);\n\n\treturn { forgotPassword, isLoading, error, sent };\n}\n","import type { AuthClient, ILoginInput, ILoginResult } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useCallback, useState } from 'react';\nimport { persistToken } from '../storage';\n\nexport interface IUseLoginReturn {\n\tlogin: (input: ILoginInput) => Promise<ILoginResult>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tdata: ILoginResult | null;\n}\n\nexport function useLogin(client: AuthClient): IUseLoginReturn {\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [data, setData] = useState<ILoginResult | null>(null);\n\n\tconst login = useCallback(\n\t\tasync (input: ILoginInput) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\ttry {\n\t\t\t\tconst { result } = await client.login(input);\n\t\t\t\tclient.setAccessToken(result.tokens.access);\n\t\t\t\tawait persistToken(result.tokens.access);\n\t\t\t\tsetData(result);\n\t\t\t\treturn result;\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[client],\n\t);\n\n\treturn { login, isLoading, error, data };\n}\n","import AsyncStorage from '@react-native-async-storage/async-storage';\n\nexport const TOKEN_KEY = 'fonderie_access_token';\n\nexport function persistToken(token: string) {\n\treturn AsyncStorage.setItem(TOKEN_KEY, token);\n}\n\nexport function clearToken() {\n\treturn AsyncStorage.removeItem(TOKEN_KEY);\n}\n\nexport function readToken() {\n\treturn AsyncStorage.getItem(TOKEN_KEY);\n}\n","import type { AuthClient } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useCallback, useState } from 'react';\nimport { clearToken } from '../storage';\n\nexport interface IUseLogoutReturn {\n\tlogout: () => Promise<void>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n}\n\nexport function useLogout(client: AuthClient): IUseLogoutReturn {\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\n\tconst logout = useCallback(async () => {\n\t\tsetIsLoading(true);\n\t\tsetError(null);\n\t\ttry {\n\t\t\tawait client.logout();\n\t\t} catch (err) {\n\t\t\tconst apiError =\n\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\tsetError(apiError);\n\t\t} finally {\n\t\t\tclient.setAccessToken(undefined);\n\t\t\tawait clearToken();\n\t\t\tsetIsLoading(false);\n\t\t}\n\t}, [client]);\n\n\treturn { logout, isLoading, error };\n}\n","import type { AuthClient, IRegisterInput, IRegisterResult } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useCallback, useState } from 'react';\nimport { persistToken } from '../storage';\n\nexport interface IUseRegisterReturn {\n\tregister: (input: IRegisterInput) => Promise<IRegisterResult>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tdata: IRegisterResult | null;\n}\n\nexport function useRegister(client: AuthClient): IUseRegisterReturn {\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [data, setData] = useState<IRegisterResult | null>(null);\n\n\tconst register = useCallback(\n\t\tasync (input: IRegisterInput) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\ttry {\n\t\t\t\tconst { result } = await client.register(input);\n\t\t\t\tclient.setAccessToken(result.tokens.access);\n\t\t\t\tawait persistToken(result.tokens.access);\n\t\t\t\tsetData(result);\n\t\t\t\treturn result;\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[client],\n\t);\n\n\treturn { register, isLoading, error, data };\n}\n","import type { AuthClient, IResetPasswordInput } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useCallback, useState } from 'react';\n\nexport interface IUseResetPasswordReturn {\n\tresetPassword: (input: IResetPasswordInput) => Promise<void>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tdone: boolean;\n}\n\nexport function useResetPassword(client: AuthClient): IUseResetPasswordReturn {\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [done, setDone] = useState(false);\n\n\tconst resetPassword = useCallback(\n\t\tasync (input: IResetPasswordInput) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\ttry {\n\t\t\t\tawait client.resetPassword(input);\n\t\t\t\tsetDone(true);\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[client],\n\t);\n\n\treturn { resetPassword, isLoading, error, done };\n}\n","import type { AuthClient, IUserDTO } from '@fonderie/client';\nimport { useCallback, useEffect, useState } from 'react';\nimport { clearToken, readToken } from '../storage';\n\nexport interface IUseSessionReturn {\n\tuser: IUserDTO | null;\n\tisLoading: boolean;\n\tisAuthenticated: boolean;\n\trefresh: () => Promise<void>;\n\tlogout: () => Promise<void>;\n}\n\nexport function useSession(client: AuthClient): IUseSessionReturn {\n\tconst [user, setUser] = useState<IUserDTO | null>(null);\n\tconst [isLoading, setIsLoading] = useState(true);\n\tconst [isAuthenticated, setIsAuthenticated] = useState(false);\n\n\tconst refresh = useCallback(async () => {\n\t\tsetIsLoading(true);\n\t\ttry {\n\t\t\tconst { result } = await client.getUser();\n\t\t\tsetUser(result.user);\n\t\t\tsetIsAuthenticated(true);\n\t\t} catch {\n\t\t\tsetUser(null);\n\t\t\tsetIsAuthenticated(false);\n\t\t\tclient.setAccessToken(undefined);\n\t\t\tawait clearToken();\n\t\t} finally {\n\t\t\tsetIsLoading(false);\n\t\t}\n\t}, [client]);\n\n\tconst logout = useCallback(async () => {\n\t\ttry {\n\t\t\tawait client.logout();\n\t\t} catch {\n\t\t\t// Session is being torn down regardless of server response.\n\t\t}\n\t\tclient.setAccessToken(undefined);\n\t\tawait clearToken();\n\t\tsetUser(null);\n\t\tsetIsAuthenticated(false);\n\t}, [client]);\n\n\tuseEffect(() => {\n\t\treadToken().then((token) => {\n\t\t\tif (token) client.setAccessToken(token);\n\t\t\tvoid refresh();\n\t\t});\n\t}, [client, refresh]);\n\n\treturn { user, isLoading, isAuthenticated, refresh, logout };\n}\n","import type { AuthClient, IVerifyEmailResult } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useCallback, useState } from 'react';\n\nexport interface IUseVerifyEmailReturn {\n\tverifyEmail: (pin: string) => Promise<IVerifyEmailResult>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tdata: IVerifyEmailResult | null;\n}\n\nexport function useVerifyEmail(client: AuthClient): IUseVerifyEmailReturn {\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [data, setData] = useState<IVerifyEmailResult | null>(null);\n\n\tconst verifyEmail = useCallback(\n\t\tasync (pin: string) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\ttry {\n\t\t\t\tconst { result } = await client.verifyEmail(pin);\n\t\t\t\tsetData(result);\n\t\t\t\treturn result;\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[client],\n\t);\n\n\treturn { verifyEmail, isLoading, error, data };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAYA,IAAAA,iBAAiC;;;ACXjC,oBAAiC;AACjC,mBAAsC;AAS/B,SAAS,kBAAkB,QAA8C;AAC/E,QAAM,CAAC,WAAW,YAAY,QAAI,uBAAS,KAAK;AAChD,QAAM,CAAC,OAAO,QAAQ,QAAI,uBAAkC,IAAI;AAChE,QAAM,CAAC,MAAM,OAAO,QAAI,uBAAS,KAAK;AAEtC,QAAM,qBAAiB;AAAA,IACtB,OAAO,UAAkB;AACxB,mBAAa,IAAI;AACjB,eAAS,IAAI;AACb,UAAI;AACH,cAAM,OAAO,e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WAAW,OAAO,KAAK;AAC9C;","names":["import_client","import_client","import_react","AsyncStorage","import_client","import_react","import_client","import_react","import_client","import_react","import_react","import_client","import_react"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/hooks/useForgotPassword.ts","../src/hooks/useLogin.ts","../src/storage.ts","../src/hooks/useLogout.ts","../src/hooks/useRegister.ts","../src/hooks/useResetPassword.ts","../src/hooks/useSession.ts","../src/hooks/useVerifyEmail.ts"],"sourcesContent":["export type {\n\tAuthClient,\n\tILoginInput,\n\tILoginResult,\n\tIRegisterInput,\n\tIRegisterResult,\n\tIResetPasswordInput,\n\tITokens,\n\tIUserDTO,\n\tIVerifyEmailResult,\n} from '@fonderie/client';\n\nexport { FonderieApiError } from '@fonderie/client';\nexport type {\n\tIUseForgotPasswordReturn,\n\tIUseLoginReturn,\n\tIUseLogoutReturn,\n\tIUseRegisterReturn,\n\tIUseResetPasswordReturn,\n\tIUseSessionReturn,\n\tIUseVerifyEmailReturn,\n} from './hooks';\nexport {\n\tuseForgotPassword,\n\tuseLogin,\n\tuseLogout,\n\tuseRegister,\n\tuseResetPassword,\n\tuseSession,\n\tuseVerifyEmail,\n} from './hooks';\n","import type { AuthClient } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useFonderieSubClient } from '@fonderie/react';\nimport { useCallback, useState } from 'react';\n\nexport interface IUseForgotPasswordReturn {\n\tforgotPassword: (email: string) => Promise<void>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tsent: boolean;\n}\n\nexport function useForgotPassword(client?: AuthClient): IUseForgotPasswordReturn {\n\tconst auth = useFonderieSubClient(client, (c) => c.auth, 'useForgotPassword');\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [sent, setSent] = useState(false);\n\n\tconst forgotPassword = useCallback(\n\t\tasync (email: string) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\ttry {\n\t\t\t\tawait auth.forgotPassword(email);\n\t\t\t\tsetSent(true);\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[auth],\n\t);\n\n\treturn { forgotPassword, isLoading, error, sent };\n}\n","import type { AuthClient, ILoginInput, ILoginResult, IMfaRequiredResult } from '@fonderie/client';\nimport { FonderieApiError, isMfaRequired } from '@fonderie/client';\nimport { useFonderieSubClient } from '@fonderie/react';\nimport { useCallback, useState } from 'react';\nimport { persistToken } from '../storage';\n\nexport interface IUseLoginReturn {\n\tlogin: (input: ILoginInput) => Promise<ILoginResult | IMfaRequiredResult>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tdata: ILoginResult | null;\n\t// Set when the account requires MFA: no tokens were issued — complete the\n\t// login with client.auth.mfa.verifyLogin(mfaPending.mfaToken, code).\n\tmfaPending: IMfaRequiredResult | null;\n}\n\nexport function useLogin(client?: AuthClient): IUseLoginReturn {\n\tconst auth = useFonderieSubClient(client, (c) => c.auth, 'useLogin');\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [data, setData] = useState<ILoginResult | null>(null);\n\tconst [mfaPending, setMfaPending] = useState<IMfaRequiredResult | null>(null);\n\n\tconst login = useCallback(\n\t\tasync (input: ILoginInput) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\tsetMfaPending(null);\n\t\t\ttry {\n\t\t\t\tconst { result } = await auth.login(input);\n\t\t\t\tif (isMfaRequired(result)) {\n\t\t\t\t\tsetMfaPending(result);\n\t\t\t\t\treturn result;\n\t\t\t\t}\n\t\t\t\tauth.setAccessToken(result.tokens.access);\n\t\t\t\tawait persistToken(result.tokens.access);\n\t\t\t\tsetData(result);\n\t\t\t\treturn result;\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[auth],\n\t);\n\n\treturn { login, isLoading, error, data, mfaPending };\n}\n","import AsyncStorage from '@react-native-async-storage/async-storage';\n\nexport const TOKEN_KEY = 'fonderie_access_token';\n\nexport function persistToken(token: string) {\n\treturn AsyncStorage.setItem(TOKEN_KEY, token);\n}\n\nexport function clearToken() {\n\treturn AsyncStorage.removeItem(TOKEN_KEY);\n}\n\nexport function readToken() {\n\treturn AsyncStorage.getItem(TOKEN_KEY);\n}\n","import type { AuthClient } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useFonderieSubClient } from '@fonderie/react';\nimport { useCallback, useState } from 'react';\nimport { clearToken } from '../storage';\n\nexport interface IUseLogoutReturn {\n\tlogout: (refreshToken?: string) => Promise<void>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n}\n\nexport function useLogout(client?: AuthClient): IUseLogoutReturn {\n\tconst auth = useFonderieSubClient(client, (c) => c.auth, 'useLogout');\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\n\tconst logout = useCallback(async (refreshToken?: string) => {\n\t\tsetIsLoading(true);\n\t\tsetError(null);\n\t\ttry {\n\t\t\tawait auth.logout(refreshToken);\n\t\t} catch (err) {\n\t\t\tconst apiError =\n\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\tsetError(apiError);\n\t\t} finally {\n\t\t\tauth.setAccessToken(undefined);\n\t\t\tawait clearToken();\n\t\t\tsetIsLoading(false);\n\t\t}\n\t}, [auth]);\n\n\treturn { logout, isLoading, error };\n}\n","import type { AuthClient, IRegisterInput, IRegisterResult } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useFonderieSubClient } from '@fonderie/react';\nimport { useCallback, useState } from 'react';\nimport { persistToken } from '../storage';\n\nexport interface IUseRegisterReturn {\n\tregister: (input: IRegisterInput) => Promise<IRegisterResult>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tdata: IRegisterResult | null;\n}\n\nexport function useRegister(client?: AuthClient): IUseRegisterReturn {\n\tconst auth = useFonderieSubClient(client, (c) => c.auth, 'useRegister');\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [data, setData] = useState<IRegisterResult | null>(null);\n\n\tconst register = useCallback(\n\t\tasync (input: IRegisterInput) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\ttry {\n\t\t\t\tconst { result } = await auth.register(input);\n\t\t\t\tauth.setAccessToken(result.tokens.access);\n\t\t\t\tawait persistToken(result.tokens.access);\n\t\t\t\tsetData(result);\n\t\t\t\treturn result;\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[auth],\n\t);\n\n\treturn { register, isLoading, error, data };\n}\n","import type { AuthClient, IResetPasswordInput } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useFonderieSubClient } from '@fonderie/react';\nimport { useCallback, useState } from 'react';\n\nexport interface IUseResetPasswordReturn {\n\tresetPassword: (input: IResetPasswordInput) => Promise<void>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tdone: boolean;\n}\n\nexport function useResetPassword(client?: AuthClient): IUseResetPasswordReturn {\n\tconst auth = useFonderieSubClient(client, (c) => c.auth, 'useResetPassword');\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [done, setDone] = useState(false);\n\n\tconst resetPassword = useCallback(\n\t\tasync (input: IResetPasswordInput) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\ttry {\n\t\t\t\tawait auth.resetPassword(input);\n\t\t\t\tsetDone(true);\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[auth],\n\t);\n\n\treturn { resetPassword, isLoading, error, done };\n}\n","import type { AuthClient, IUserDTO } from '@fonderie/client';\nimport { useFonderieSubClient } from '@fonderie/react';\nimport { useCallback, useEffect, useState } from 'react';\nimport { clearToken, readToken } from '../storage';\n\nexport interface IUseSessionReturn {\n\tuser: IUserDTO | null;\n\tisLoading: boolean;\n\tisAuthenticated: boolean;\n\trefresh: () => Promise<void>;\n\tlogout: (refreshToken?: string) => Promise<void>;\n}\n\nexport function useSession(client?: AuthClient): IUseSessionReturn {\n\tconst auth = useFonderieSubClient(client, (c) => c.auth, 'useSession');\n\tconst [user, setUser] = useState<IUserDTO | null>(null);\n\tconst [isLoading, setIsLoading] = useState(true);\n\tconst [isAuthenticated, setIsAuthenticated] = useState(false);\n\n\tconst refresh = useCallback(async () => {\n\t\tsetIsLoading(true);\n\t\ttry {\n\t\t\tconst { result } = await auth.getUser();\n\t\t\tsetUser(result.user);\n\t\t\tsetIsAuthenticated(true);\n\t\t} catch {\n\t\t\tsetUser(null);\n\t\t\tsetIsAuthenticated(false);\n\t\t\tauth.setAccessToken(undefined);\n\t\t\tawait clearToken();\n\t\t} finally {\n\t\t\tsetIsLoading(false);\n\t\t}\n\t}, [auth]);\n\n\tconst logout = useCallback(async (refreshToken?: string) => {\n\t\ttry {\n\t\t\tawait auth.logout(refreshToken);\n\t\t} catch {\n\t\t\t// Session is being torn down regardless of server response.\n\t\t}\n\t\tauth.setAccessToken(undefined);\n\t\tawait clearToken();\n\t\tsetUser(null);\n\t\tsetIsAuthenticated(false);\n\t}, [auth]);\n\n\tuseEffect(() => {\n\t\treadToken().then((token) => {\n\t\t\tif (token) auth.setAccessToken(token);\n\t\t\tvoid refresh();\n\t\t});\n\t}, [auth, refresh]);\n\n\treturn { user, isLoading, isAuthenticated, refresh, logout };\n}\n","import type { AuthClient, IVerifyEmailResult } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useFonderieSubClient } from '@fonderie/react';\nimport { useCallback, useState } from 'react';\n\nexport interface IUseVerifyEmailReturn {\n\tverifyEmail: (pin: string) => Promise<IVerifyEmailResult>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tdata: IVerifyEmailResult | null;\n}\n\nexport function useVerifyEmail(client?: AuthClient): IUseVerifyEmailReturn {\n\tconst auth = useFonderieSubClient(client, (c) => c.auth, 'useVerifyEmail');\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [data, setData] = useState<IVerifyEmailResult | null>(null);\n\n\tconst verifyEmail = useCallback(\n\t\tasync (pin: string) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\ttry {\n\t\t\t\tconst { result } = await auth.verifyEmail(pin);\n\t\t\t\tsetData(result);\n\t\t\t\treturn result;\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[auth],\n\t);\n\n\treturn { verifyEmail, isLoading, error, data };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AA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Storage","import_client","import_react","import_client","import_react","import_client","import_react","import_react","import_client","import_react"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FonderieApiError, AuthClient, ILoginInput, ILoginResult, IRegisterInput, IRegisterResult, IResetPasswordInput, IUserDTO, IVerifyEmailResult } from '@fonderie/client';
|
|
1
|
+
import { FonderieApiError, AuthClient, ILoginInput, ILoginResult, IMfaRequiredResult, IRegisterInput, IRegisterResult, IResetPasswordInput, IUserDTO, IVerifyEmailResult } from '@fonderie/client';
|
|
2
2
|
export { AuthClient, FonderieApiError, ILoginInput, ILoginResult, IRegisterInput, IRegisterResult, IResetPasswordInput, ITokens, IUserDTO, IVerifyEmailResult } from '@fonderie/client';
|
|
3
3
|
|
|
4
4
|
interface IUseForgotPasswordReturn {
|
|
@@ -7,22 +7,23 @@ interface IUseForgotPasswordReturn {
|
|
|
7
7
|
error: FonderieApiError | null;
|
|
8
8
|
sent: boolean;
|
|
9
9
|
}
|
|
10
|
-
declare function useForgotPassword(client
|
|
10
|
+
declare function useForgotPassword(client?: AuthClient): IUseForgotPasswordReturn;
|
|
11
11
|
|
|
12
12
|
interface IUseLoginReturn {
|
|
13
|
-
login: (input: ILoginInput) => Promise<ILoginResult>;
|
|
13
|
+
login: (input: ILoginInput) => Promise<ILoginResult | IMfaRequiredResult>;
|
|
14
14
|
isLoading: boolean;
|
|
15
15
|
error: FonderieApiError | null;
|
|
16
16
|
data: ILoginResult | null;
|
|
17
|
+
mfaPending: IMfaRequiredResult | null;
|
|
17
18
|
}
|
|
18
|
-
declare function useLogin(client
|
|
19
|
+
declare function useLogin(client?: AuthClient): IUseLoginReturn;
|
|
19
20
|
|
|
20
21
|
interface IUseLogoutReturn {
|
|
21
|
-
logout: () => Promise<void>;
|
|
22
|
+
logout: (refreshToken?: string) => Promise<void>;
|
|
22
23
|
isLoading: boolean;
|
|
23
24
|
error: FonderieApiError | null;
|
|
24
25
|
}
|
|
25
|
-
declare function useLogout(client
|
|
26
|
+
declare function useLogout(client?: AuthClient): IUseLogoutReturn;
|
|
26
27
|
|
|
27
28
|
interface IUseRegisterReturn {
|
|
28
29
|
register: (input: IRegisterInput) => Promise<IRegisterResult>;
|
|
@@ -30,7 +31,7 @@ interface IUseRegisterReturn {
|
|
|
30
31
|
error: FonderieApiError | null;
|
|
31
32
|
data: IRegisterResult | null;
|
|
32
33
|
}
|
|
33
|
-
declare function useRegister(client
|
|
34
|
+
declare function useRegister(client?: AuthClient): IUseRegisterReturn;
|
|
34
35
|
|
|
35
36
|
interface IUseResetPasswordReturn {
|
|
36
37
|
resetPassword: (input: IResetPasswordInput) => Promise<void>;
|
|
@@ -38,16 +39,16 @@ interface IUseResetPasswordReturn {
|
|
|
38
39
|
error: FonderieApiError | null;
|
|
39
40
|
done: boolean;
|
|
40
41
|
}
|
|
41
|
-
declare function useResetPassword(client
|
|
42
|
+
declare function useResetPassword(client?: AuthClient): IUseResetPasswordReturn;
|
|
42
43
|
|
|
43
44
|
interface IUseSessionReturn {
|
|
44
45
|
user: IUserDTO | null;
|
|
45
46
|
isLoading: boolean;
|
|
46
47
|
isAuthenticated: boolean;
|
|
47
48
|
refresh: () => Promise<void>;
|
|
48
|
-
logout: () => Promise<void>;
|
|
49
|
+
logout: (refreshToken?: string) => Promise<void>;
|
|
49
50
|
}
|
|
50
|
-
declare function useSession(client
|
|
51
|
+
declare function useSession(client?: AuthClient): IUseSessionReturn;
|
|
51
52
|
|
|
52
53
|
interface IUseVerifyEmailReturn {
|
|
53
54
|
verifyEmail: (pin: string) => Promise<IVerifyEmailResult>;
|
|
@@ -55,6 +56,6 @@ interface IUseVerifyEmailReturn {
|
|
|
55
56
|
error: FonderieApiError | null;
|
|
56
57
|
data: IVerifyEmailResult | null;
|
|
57
58
|
}
|
|
58
|
-
declare function useVerifyEmail(client
|
|
59
|
+
declare function useVerifyEmail(client?: AuthClient): IUseVerifyEmailReturn;
|
|
59
60
|
|
|
60
61
|
export { type IUseForgotPasswordReturn, type IUseLoginReturn, type IUseLogoutReturn, type IUseRegisterReturn, type IUseResetPasswordReturn, type IUseSessionReturn, type IUseVerifyEmailReturn, useForgotPassword, useLogin, useLogout, useRegister, useResetPassword, useSession, useVerifyEmail };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FonderieApiError, AuthClient, ILoginInput, ILoginResult, IRegisterInput, IRegisterResult, IResetPasswordInput, IUserDTO, IVerifyEmailResult } from '@fonderie/client';
|
|
1
|
+
import { FonderieApiError, AuthClient, ILoginInput, ILoginResult, IMfaRequiredResult, IRegisterInput, IRegisterResult, IResetPasswordInput, IUserDTO, IVerifyEmailResult } from '@fonderie/client';
|
|
2
2
|
export { AuthClient, FonderieApiError, ILoginInput, ILoginResult, IRegisterInput, IRegisterResult, IResetPasswordInput, ITokens, IUserDTO, IVerifyEmailResult } from '@fonderie/client';
|
|
3
3
|
|
|
4
4
|
interface IUseForgotPasswordReturn {
|
|
@@ -7,22 +7,23 @@ interface IUseForgotPasswordReturn {
|
|
|
7
7
|
error: FonderieApiError | null;
|
|
8
8
|
sent: boolean;
|
|
9
9
|
}
|
|
10
|
-
declare function useForgotPassword(client
|
|
10
|
+
declare function useForgotPassword(client?: AuthClient): IUseForgotPasswordReturn;
|
|
11
11
|
|
|
12
12
|
interface IUseLoginReturn {
|
|
13
|
-
login: (input: ILoginInput) => Promise<ILoginResult>;
|
|
13
|
+
login: (input: ILoginInput) => Promise<ILoginResult | IMfaRequiredResult>;
|
|
14
14
|
isLoading: boolean;
|
|
15
15
|
error: FonderieApiError | null;
|
|
16
16
|
data: ILoginResult | null;
|
|
17
|
+
mfaPending: IMfaRequiredResult | null;
|
|
17
18
|
}
|
|
18
|
-
declare function useLogin(client
|
|
19
|
+
declare function useLogin(client?: AuthClient): IUseLoginReturn;
|
|
19
20
|
|
|
20
21
|
interface IUseLogoutReturn {
|
|
21
|
-
logout: () => Promise<void>;
|
|
22
|
+
logout: (refreshToken?: string) => Promise<void>;
|
|
22
23
|
isLoading: boolean;
|
|
23
24
|
error: FonderieApiError | null;
|
|
24
25
|
}
|
|
25
|
-
declare function useLogout(client
|
|
26
|
+
declare function useLogout(client?: AuthClient): IUseLogoutReturn;
|
|
26
27
|
|
|
27
28
|
interface IUseRegisterReturn {
|
|
28
29
|
register: (input: IRegisterInput) => Promise<IRegisterResult>;
|
|
@@ -30,7 +31,7 @@ interface IUseRegisterReturn {
|
|
|
30
31
|
error: FonderieApiError | null;
|
|
31
32
|
data: IRegisterResult | null;
|
|
32
33
|
}
|
|
33
|
-
declare function useRegister(client
|
|
34
|
+
declare function useRegister(client?: AuthClient): IUseRegisterReturn;
|
|
34
35
|
|
|
35
36
|
interface IUseResetPasswordReturn {
|
|
36
37
|
resetPassword: (input: IResetPasswordInput) => Promise<void>;
|
|
@@ -38,16 +39,16 @@ interface IUseResetPasswordReturn {
|
|
|
38
39
|
error: FonderieApiError | null;
|
|
39
40
|
done: boolean;
|
|
40
41
|
}
|
|
41
|
-
declare function useResetPassword(client
|
|
42
|
+
declare function useResetPassword(client?: AuthClient): IUseResetPasswordReturn;
|
|
42
43
|
|
|
43
44
|
interface IUseSessionReturn {
|
|
44
45
|
user: IUserDTO | null;
|
|
45
46
|
isLoading: boolean;
|
|
46
47
|
isAuthenticated: boolean;
|
|
47
48
|
refresh: () => Promise<void>;
|
|
48
|
-
logout: () => Promise<void>;
|
|
49
|
+
logout: (refreshToken?: string) => Promise<void>;
|
|
49
50
|
}
|
|
50
|
-
declare function useSession(client
|
|
51
|
+
declare function useSession(client?: AuthClient): IUseSessionReturn;
|
|
51
52
|
|
|
52
53
|
interface IUseVerifyEmailReturn {
|
|
53
54
|
verifyEmail: (pin: string) => Promise<IVerifyEmailResult>;
|
|
@@ -55,6 +56,6 @@ interface IUseVerifyEmailReturn {
|
|
|
55
56
|
error: FonderieApiError | null;
|
|
56
57
|
data: IVerifyEmailResult | null;
|
|
57
58
|
}
|
|
58
|
-
declare function useVerifyEmail(client
|
|
59
|
+
declare function useVerifyEmail(client?: AuthClient): IUseVerifyEmailReturn;
|
|
59
60
|
|
|
60
61
|
export { type IUseForgotPasswordReturn, type IUseLoginReturn, type IUseLogoutReturn, type IUseRegisterReturn, type IUseResetPasswordReturn, type IUseSessionReturn, type IUseVerifyEmailReturn, useForgotPassword, useLogin, useLogout, useRegister, useResetPassword, useSession, useVerifyEmail };
|
package/dist/index.js
CHANGED
|
@@ -3,8 +3,10 @@ import { FonderieApiError as FonderieApiError7 } from "@fonderie/client";
|
|
|
3
3
|
|
|
4
4
|
// src/hooks/useForgotPassword.ts
|
|
5
5
|
import { FonderieApiError } from "@fonderie/client";
|
|
6
|
+
import { useFonderieSubClient } from "@fonderie/react";
|
|
6
7
|
import { useCallback, useState } from "react";
|
|
7
8
|
function useForgotPassword(client) {
|
|
9
|
+
const auth = useFonderieSubClient(client, (c) => c.auth, "useForgotPassword");
|
|
8
10
|
const [isLoading, setIsLoading] = useState(false);
|
|
9
11
|
const [error, setError] = useState(null);
|
|
10
12
|
const [sent, setSent] = useState(false);
|
|
@@ -13,7 +15,7 @@ function useForgotPassword(client) {
|
|
|
13
15
|
setIsLoading(true);
|
|
14
16
|
setError(null);
|
|
15
17
|
try {
|
|
16
|
-
await
|
|
18
|
+
await auth.forgotPassword(email);
|
|
17
19
|
setSent(true);
|
|
18
20
|
} catch (err) {
|
|
19
21
|
const apiError = err instanceof FonderieApiError ? err : new FonderieApiError("unknown", String(err), 0);
|
|
@@ -23,13 +25,14 @@ function useForgotPassword(client) {
|
|
|
23
25
|
setIsLoading(false);
|
|
24
26
|
}
|
|
25
27
|
},
|
|
26
|
-
[
|
|
28
|
+
[auth]
|
|
27
29
|
);
|
|
28
30
|
return { forgotPassword, isLoading, error, sent };
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
// src/hooks/useLogin.ts
|
|
32
|
-
import { FonderieApiError as FonderieApiError2 } from "@fonderie/client";
|
|
34
|
+
import { FonderieApiError as FonderieApiError2, isMfaRequired } from "@fonderie/client";
|
|
35
|
+
import { useFonderieSubClient as useFonderieSubClient2 } from "@fonderie/react";
|
|
33
36
|
import { useCallback as useCallback2, useState as useState2 } from "react";
|
|
34
37
|
|
|
35
38
|
// src/storage.ts
|
|
@@ -47,16 +50,23 @@ function readToken() {
|
|
|
47
50
|
|
|
48
51
|
// src/hooks/useLogin.ts
|
|
49
52
|
function useLogin(client) {
|
|
53
|
+
const auth = useFonderieSubClient2(client, (c) => c.auth, "useLogin");
|
|
50
54
|
const [isLoading, setIsLoading] = useState2(false);
|
|
51
55
|
const [error, setError] = useState2(null);
|
|
52
56
|
const [data, setData] = useState2(null);
|
|
57
|
+
const [mfaPending, setMfaPending] = useState2(null);
|
|
53
58
|
const login = useCallback2(
|
|
54
59
|
async (input) => {
|
|
55
60
|
setIsLoading(true);
|
|
56
61
|
setError(null);
|
|
62
|
+
setMfaPending(null);
|
|
57
63
|
try {
|
|
58
|
-
const { result } = await
|
|
59
|
-
|
|
64
|
+
const { result } = await auth.login(input);
|
|
65
|
+
if (isMfaRequired(result)) {
|
|
66
|
+
setMfaPending(result);
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
auth.setAccessToken(result.tokens.access);
|
|
60
70
|
await persistToken(result.tokens.access);
|
|
61
71
|
setData(result);
|
|
62
72
|
return result;
|
|
@@ -68,38 +78,42 @@ function useLogin(client) {
|
|
|
68
78
|
setIsLoading(false);
|
|
69
79
|
}
|
|
70
80
|
},
|
|
71
|
-
[
|
|
81
|
+
[auth]
|
|
72
82
|
);
|
|
73
|
-
return { login, isLoading, error, data };
|
|
83
|
+
return { login, isLoading, error, data, mfaPending };
|
|
74
84
|
}
|
|
75
85
|
|
|
76
86
|
// src/hooks/useLogout.ts
|
|
77
87
|
import { FonderieApiError as FonderieApiError3 } from "@fonderie/client";
|
|
88
|
+
import { useFonderieSubClient as useFonderieSubClient3 } from "@fonderie/react";
|
|
78
89
|
import { useCallback as useCallback3, useState as useState3 } from "react";
|
|
79
90
|
function useLogout(client) {
|
|
91
|
+
const auth = useFonderieSubClient3(client, (c) => c.auth, "useLogout");
|
|
80
92
|
const [isLoading, setIsLoading] = useState3(false);
|
|
81
93
|
const [error, setError] = useState3(null);
|
|
82
|
-
const logout = useCallback3(async () => {
|
|
94
|
+
const logout = useCallback3(async (refreshToken) => {
|
|
83
95
|
setIsLoading(true);
|
|
84
96
|
setError(null);
|
|
85
97
|
try {
|
|
86
|
-
await
|
|
98
|
+
await auth.logout(refreshToken);
|
|
87
99
|
} catch (err) {
|
|
88
100
|
const apiError = err instanceof FonderieApiError3 ? err : new FonderieApiError3("unknown", String(err), 0);
|
|
89
101
|
setError(apiError);
|
|
90
102
|
} finally {
|
|
91
|
-
|
|
103
|
+
auth.setAccessToken(void 0);
|
|
92
104
|
await clearToken();
|
|
93
105
|
setIsLoading(false);
|
|
94
106
|
}
|
|
95
|
-
}, [
|
|
107
|
+
}, [auth]);
|
|
96
108
|
return { logout, isLoading, error };
|
|
97
109
|
}
|
|
98
110
|
|
|
99
111
|
// src/hooks/useRegister.ts
|
|
100
112
|
import { FonderieApiError as FonderieApiError4 } from "@fonderie/client";
|
|
113
|
+
import { useFonderieSubClient as useFonderieSubClient4 } from "@fonderie/react";
|
|
101
114
|
import { useCallback as useCallback4, useState as useState4 } from "react";
|
|
102
115
|
function useRegister(client) {
|
|
116
|
+
const auth = useFonderieSubClient4(client, (c) => c.auth, "useRegister");
|
|
103
117
|
const [isLoading, setIsLoading] = useState4(false);
|
|
104
118
|
const [error, setError] = useState4(null);
|
|
105
119
|
const [data, setData] = useState4(null);
|
|
@@ -108,8 +122,8 @@ function useRegister(client) {
|
|
|
108
122
|
setIsLoading(true);
|
|
109
123
|
setError(null);
|
|
110
124
|
try {
|
|
111
|
-
const { result } = await
|
|
112
|
-
|
|
125
|
+
const { result } = await auth.register(input);
|
|
126
|
+
auth.setAccessToken(result.tokens.access);
|
|
113
127
|
await persistToken(result.tokens.access);
|
|
114
128
|
setData(result);
|
|
115
129
|
return result;
|
|
@@ -121,15 +135,17 @@ function useRegister(client) {
|
|
|
121
135
|
setIsLoading(false);
|
|
122
136
|
}
|
|
123
137
|
},
|
|
124
|
-
[
|
|
138
|
+
[auth]
|
|
125
139
|
);
|
|
126
140
|
return { register, isLoading, error, data };
|
|
127
141
|
}
|
|
128
142
|
|
|
129
143
|
// src/hooks/useResetPassword.ts
|
|
130
144
|
import { FonderieApiError as FonderieApiError5 } from "@fonderie/client";
|
|
145
|
+
import { useFonderieSubClient as useFonderieSubClient5 } from "@fonderie/react";
|
|
131
146
|
import { useCallback as useCallback5, useState as useState5 } from "react";
|
|
132
147
|
function useResetPassword(client) {
|
|
148
|
+
const auth = useFonderieSubClient5(client, (c) => c.auth, "useResetPassword");
|
|
133
149
|
const [isLoading, setIsLoading] = useState5(false);
|
|
134
150
|
const [error, setError] = useState5(null);
|
|
135
151
|
const [done, setDone] = useState5(false);
|
|
@@ -138,7 +154,7 @@ function useResetPassword(client) {
|
|
|
138
154
|
setIsLoading(true);
|
|
139
155
|
setError(null);
|
|
140
156
|
try {
|
|
141
|
-
await
|
|
157
|
+
await auth.resetPassword(input);
|
|
142
158
|
setDone(true);
|
|
143
159
|
} catch (err) {
|
|
144
160
|
const apiError = err instanceof FonderieApiError5 ? err : new FonderieApiError5("unknown", String(err), 0);
|
|
@@ -148,55 +164,59 @@ function useResetPassword(client) {
|
|
|
148
164
|
setIsLoading(false);
|
|
149
165
|
}
|
|
150
166
|
},
|
|
151
|
-
[
|
|
167
|
+
[auth]
|
|
152
168
|
);
|
|
153
169
|
return { resetPassword, isLoading, error, done };
|
|
154
170
|
}
|
|
155
171
|
|
|
156
172
|
// src/hooks/useSession.ts
|
|
173
|
+
import { useFonderieSubClient as useFonderieSubClient6 } from "@fonderie/react";
|
|
157
174
|
import { useCallback as useCallback6, useEffect, useState as useState6 } from "react";
|
|
158
175
|
function useSession(client) {
|
|
176
|
+
const auth = useFonderieSubClient6(client, (c) => c.auth, "useSession");
|
|
159
177
|
const [user, setUser] = useState6(null);
|
|
160
178
|
const [isLoading, setIsLoading] = useState6(true);
|
|
161
179
|
const [isAuthenticated, setIsAuthenticated] = useState6(false);
|
|
162
180
|
const refresh = useCallback6(async () => {
|
|
163
181
|
setIsLoading(true);
|
|
164
182
|
try {
|
|
165
|
-
const { result } = await
|
|
183
|
+
const { result } = await auth.getUser();
|
|
166
184
|
setUser(result.user);
|
|
167
185
|
setIsAuthenticated(true);
|
|
168
186
|
} catch {
|
|
169
187
|
setUser(null);
|
|
170
188
|
setIsAuthenticated(false);
|
|
171
|
-
|
|
189
|
+
auth.setAccessToken(void 0);
|
|
172
190
|
await clearToken();
|
|
173
191
|
} finally {
|
|
174
192
|
setIsLoading(false);
|
|
175
193
|
}
|
|
176
|
-
}, [
|
|
177
|
-
const logout = useCallback6(async () => {
|
|
194
|
+
}, [auth]);
|
|
195
|
+
const logout = useCallback6(async (refreshToken) => {
|
|
178
196
|
try {
|
|
179
|
-
await
|
|
197
|
+
await auth.logout(refreshToken);
|
|
180
198
|
} catch {
|
|
181
199
|
}
|
|
182
|
-
|
|
200
|
+
auth.setAccessToken(void 0);
|
|
183
201
|
await clearToken();
|
|
184
202
|
setUser(null);
|
|
185
203
|
setIsAuthenticated(false);
|
|
186
|
-
}, [
|
|
204
|
+
}, [auth]);
|
|
187
205
|
useEffect(() => {
|
|
188
206
|
readToken().then((token) => {
|
|
189
|
-
if (token)
|
|
207
|
+
if (token) auth.setAccessToken(token);
|
|
190
208
|
void refresh();
|
|
191
209
|
});
|
|
192
|
-
}, [
|
|
210
|
+
}, [auth, refresh]);
|
|
193
211
|
return { user, isLoading, isAuthenticated, refresh, logout };
|
|
194
212
|
}
|
|
195
213
|
|
|
196
214
|
// src/hooks/useVerifyEmail.ts
|
|
197
215
|
import { FonderieApiError as FonderieApiError6 } from "@fonderie/client";
|
|
216
|
+
import { useFonderieSubClient as useFonderieSubClient7 } from "@fonderie/react";
|
|
198
217
|
import { useCallback as useCallback7, useState as useState7 } from "react";
|
|
199
218
|
function useVerifyEmail(client) {
|
|
219
|
+
const auth = useFonderieSubClient7(client, (c) => c.auth, "useVerifyEmail");
|
|
200
220
|
const [isLoading, setIsLoading] = useState7(false);
|
|
201
221
|
const [error, setError] = useState7(null);
|
|
202
222
|
const [data, setData] = useState7(null);
|
|
@@ -205,7 +225,7 @@ function useVerifyEmail(client) {
|
|
|
205
225
|
setIsLoading(true);
|
|
206
226
|
setError(null);
|
|
207
227
|
try {
|
|
208
|
-
const { result } = await
|
|
228
|
+
const { result } = await auth.verifyEmail(pin);
|
|
209
229
|
setData(result);
|
|
210
230
|
return result;
|
|
211
231
|
} catch (err) {
|
|
@@ -216,7 +236,7 @@ function useVerifyEmail(client) {
|
|
|
216
236
|
setIsLoading(false);
|
|
217
237
|
}
|
|
218
238
|
},
|
|
219
|
-
[
|
|
239
|
+
[auth]
|
|
220
240
|
);
|
|
221
241
|
return { verifyEmail, isLoading, error, data };
|
|
222
242
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/hooks/useForgotPassword.ts","../src/hooks/useLogin.ts","../src/storage.ts","../src/hooks/useLogout.ts","../src/hooks/useRegister.ts","../src/hooks/useResetPassword.ts","../src/hooks/useSession.ts","../src/hooks/useVerifyEmail.ts"],"sourcesContent":["export type {\n\tAuthClient,\n\tILoginInput,\n\tILoginResult,\n\tIRegisterInput,\n\tIRegisterResult,\n\tIResetPasswordInput,\n\tITokens,\n\tIUserDTO,\n\tIVerifyEmailResult,\n} from '@fonderie/client';\n\nexport { FonderieApiError } from '@fonderie/client';\nexport type {\n\tIUseForgotPasswordReturn,\n\tIUseLoginReturn,\n\tIUseLogoutReturn,\n\tIUseRegisterReturn,\n\tIUseResetPasswordReturn,\n\tIUseSessionReturn,\n\tIUseVerifyEmailReturn,\n} from './hooks';\nexport {\n\tuseForgotPassword,\n\tuseLogin,\n\tuseLogout,\n\tuseRegister,\n\tuseResetPassword,\n\tuseSession,\n\tuseVerifyEmail,\n} from './hooks';\n","import type { AuthClient } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useCallback, useState } from 'react';\n\nexport interface IUseForgotPasswordReturn {\n\tforgotPassword: (email: string) => Promise<void>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tsent: boolean;\n}\n\nexport function useForgotPassword(client: AuthClient): IUseForgotPasswordReturn {\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [sent, setSent] = useState(false);\n\n\tconst forgotPassword = useCallback(\n\t\tasync (email: string) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\ttry {\n\t\t\t\tawait client.forgotPassword(email);\n\t\t\t\tsetSent(true);\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[client],\n\t);\n\n\treturn { forgotPassword, isLoading, error, sent };\n}\n","import type { AuthClient, ILoginInput, ILoginResult } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useCallback, useState } from 'react';\nimport { persistToken } from '../storage';\n\nexport interface IUseLoginReturn {\n\tlogin: (input: ILoginInput) => Promise<ILoginResult>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tdata: ILoginResult | null;\n}\n\nexport function useLogin(client: AuthClient): IUseLoginReturn {\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [data, setData] = useState<ILoginResult | null>(null);\n\n\tconst login = useCallback(\n\t\tasync (input: ILoginInput) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\ttry {\n\t\t\t\tconst { result } = await client.login(input);\n\t\t\t\tclient.setAccessToken(result.tokens.access);\n\t\t\t\tawait persistToken(result.tokens.access);\n\t\t\t\tsetData(result);\n\t\t\t\treturn result;\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[client],\n\t);\n\n\treturn { login, isLoading, error, data };\n}\n","import AsyncStorage from '@react-native-async-storage/async-storage';\n\nexport const TOKEN_KEY = 'fonderie_access_token';\n\nexport function persistToken(token: string) {\n\treturn AsyncStorage.setItem(TOKEN_KEY, token);\n}\n\nexport function clearToken() {\n\treturn AsyncStorage.removeItem(TOKEN_KEY);\n}\n\nexport function readToken() {\n\treturn AsyncStorage.getItem(TOKEN_KEY);\n}\n","import type { AuthClient } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useCallback, useState } from 'react';\nimport { clearToken } from '../storage';\n\nexport interface IUseLogoutReturn {\n\tlogout: () => Promise<void>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n}\n\nexport function useLogout(client: AuthClient): IUseLogoutReturn {\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\n\tconst logout = useCallback(async () => {\n\t\tsetIsLoading(true);\n\t\tsetError(null);\n\t\ttry {\n\t\t\tawait client.logout();\n\t\t} catch (err) {\n\t\t\tconst apiError =\n\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\tsetError(apiError);\n\t\t} finally {\n\t\t\tclient.setAccessToken(undefined);\n\t\t\tawait clearToken();\n\t\t\tsetIsLoading(false);\n\t\t}\n\t}, [client]);\n\n\treturn { logout, isLoading, error };\n}\n","import type { AuthClient, IRegisterInput, IRegisterResult } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useCallback, useState } from 'react';\nimport { persistToken } from '../storage';\n\nexport interface IUseRegisterReturn {\n\tregister: (input: IRegisterInput) => Promise<IRegisterResult>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tdata: IRegisterResult | null;\n}\n\nexport function useRegister(client: AuthClient): IUseRegisterReturn {\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [data, setData] = useState<IRegisterResult | null>(null);\n\n\tconst register = useCallback(\n\t\tasync (input: IRegisterInput) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\ttry {\n\t\t\t\tconst { result } = await client.register(input);\n\t\t\t\tclient.setAccessToken(result.tokens.access);\n\t\t\t\tawait persistToken(result.tokens.access);\n\t\t\t\tsetData(result);\n\t\t\t\treturn result;\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[client],\n\t);\n\n\treturn { register, isLoading, error, data };\n}\n","import type { AuthClient, IResetPasswordInput } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useCallback, useState } from 'react';\n\nexport interface IUseResetPasswordReturn {\n\tresetPassword: (input: IResetPasswordInput) => Promise<void>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tdone: boolean;\n}\n\nexport function useResetPassword(client: AuthClient): IUseResetPasswordReturn {\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [done, setDone] = useState(false);\n\n\tconst resetPassword = useCallback(\n\t\tasync (input: IResetPasswordInput) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\ttry {\n\t\t\t\tawait client.resetPassword(input);\n\t\t\t\tsetDone(true);\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[client],\n\t);\n\n\treturn { resetPassword, isLoading, error, done };\n}\n","import type { AuthClient, IUserDTO } from '@fonderie/client';\nimport { useCallback, useEffect, useState } from 'react';\nimport { clearToken, readToken } from '../storage';\n\nexport interface IUseSessionReturn {\n\tuser: IUserDTO | null;\n\tisLoading: boolean;\n\tisAuthenticated: boolean;\n\trefresh: () => Promise<void>;\n\tlogout: () => Promise<void>;\n}\n\nexport function useSession(client: AuthClient): IUseSessionReturn {\n\tconst [user, setUser] = useState<IUserDTO | null>(null);\n\tconst [isLoading, setIsLoading] = useState(true);\n\tconst [isAuthenticated, setIsAuthenticated] = useState(false);\n\n\tconst refresh = useCallback(async () => {\n\t\tsetIsLoading(true);\n\t\ttry {\n\t\t\tconst { result } = await client.getUser();\n\t\t\tsetUser(result.user);\n\t\t\tsetIsAuthenticated(true);\n\t\t} catch {\n\t\t\tsetUser(null);\n\t\t\tsetIsAuthenticated(false);\n\t\t\tclient.setAccessToken(undefined);\n\t\t\tawait clearToken();\n\t\t} finally {\n\t\t\tsetIsLoading(false);\n\t\t}\n\t}, [client]);\n\n\tconst logout = useCallback(async () => {\n\t\ttry {\n\t\t\tawait client.logout();\n\t\t} catch {\n\t\t\t// Session is being torn down regardless of server response.\n\t\t}\n\t\tclient.setAccessToken(undefined);\n\t\tawait clearToken();\n\t\tsetUser(null);\n\t\tsetIsAuthenticated(false);\n\t}, [client]);\n\n\tuseEffect(() => {\n\t\treadToken().then((token) => {\n\t\t\tif (token) client.setAccessToken(token);\n\t\t\tvoid refresh();\n\t\t});\n\t}, [client, refresh]);\n\n\treturn { user, isLoading, isAuthenticated, refresh, logout };\n}\n","import type { AuthClient, IVerifyEmailResult } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useCallback, useState } from 'react';\n\nexport interface IUseVerifyEmailReturn {\n\tverifyEmail: (pin: string) => Promise<IVerifyEmailResult>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tdata: IVerifyEmailResult | null;\n}\n\nexport function useVerifyEmail(client: AuthClient): IUseVerifyEmailReturn {\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [data, setData] = useState<IVerifyEmailResult | null>(null);\n\n\tconst verifyEmail = useCallback(\n\t\tasync (pin: string) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\ttry {\n\t\t\t\tconst { result } = await client.verifyEmail(pin);\n\t\t\t\tsetData(result);\n\t\t\t\treturn result;\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[client],\n\t);\n\n\treturn { verifyEmail, isLoading, error, data };\n}\n"],"mappings":";AAYA,SAAS,oBAAAA,yBAAwB;;;ACXjC,SAAS,wBAAwB;AACjC,SAAS,aAAa,gBAAgB;AAS/B,SAAS,kBAAkB,QAA8C;AAC/E,QAAM,CAAC,WAAW,YAAY,IAAI,SAAS,KAAK;AAChD,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAkC,IAAI;AAChE,QAAM,CAAC,MAAM,OAAO,IAAI,SAAS,KAAK;AAEtC,QAAM,iBAAiB;AAAA,IACtB,OAAO,UAAkB;AACxB,mBAAa,IAAI;AACjB,eAAS,IAAI;AACb,UAAI;AACH,cAAM,OAAO,eAAe,KAAK;AACjC,gBAAQ,IAAI;AAAA,MACb,SAAS,KAAK;AACb,cAAM,WACL,eAAe,mBAAmB,MAAM,IAAI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A,IACA,CAAC,MAAM;AAAA,EACR;AAEA,SAAO,EAAE,aAAa,WAAW,OAAO,KAAK;AAC9C;","names":["FonderieApiError","FonderieApiError","useCallback","useState","useState","useCallback","FonderieApiError","FonderieApiError","useCallback","useState","useState","useCallback","FonderieApiError","FonderieApiError","useCallback","useState","useState","useCallback","FonderieApiError","FonderieApiError","useCallback","useState","useCallback","useState","useState","useCallback","FonderieApiError","useCallback","useState"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/hooks/useForgotPassword.ts","../src/hooks/useLogin.ts","../src/storage.ts","../src/hooks/useLogout.ts","../src/hooks/useRegister.ts","../src/hooks/useResetPassword.ts","../src/hooks/useSession.ts","../src/hooks/useVerifyEmail.ts"],"sourcesContent":["export type {\n\tAuthClient,\n\tILoginInput,\n\tILoginResult,\n\tIRegisterInput,\n\tIRegisterResult,\n\tIResetPasswordInput,\n\tITokens,\n\tIUserDTO,\n\tIVerifyEmailResult,\n} from '@fonderie/client';\n\nexport { FonderieApiError } from '@fonderie/client';\nexport type {\n\tIUseForgotPasswordReturn,\n\tIUseLoginReturn,\n\tIUseLogoutReturn,\n\tIUseRegisterReturn,\n\tIUseResetPasswordReturn,\n\tIUseSessionReturn,\n\tIUseVerifyEmailReturn,\n} from './hooks';\nexport {\n\tuseForgotPassword,\n\tuseLogin,\n\tuseLogout,\n\tuseRegister,\n\tuseResetPassword,\n\tuseSession,\n\tuseVerifyEmail,\n} from './hooks';\n","import type { AuthClient } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useFonderieSubClient } from '@fonderie/react';\nimport { useCallback, useState } from 'react';\n\nexport interface IUseForgotPasswordReturn {\n\tforgotPassword: (email: string) => Promise<void>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tsent: boolean;\n}\n\nexport function useForgotPassword(client?: AuthClient): IUseForgotPasswordReturn {\n\tconst auth = useFonderieSubClient(client, (c) => c.auth, 'useForgotPassword');\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [sent, setSent] = useState(false);\n\n\tconst forgotPassword = useCallback(\n\t\tasync (email: string) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\ttry {\n\t\t\t\tawait auth.forgotPassword(email);\n\t\t\t\tsetSent(true);\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[auth],\n\t);\n\n\treturn { forgotPassword, isLoading, error, sent };\n}\n","import type { AuthClient, ILoginInput, ILoginResult, IMfaRequiredResult } from '@fonderie/client';\nimport { FonderieApiError, isMfaRequired } from '@fonderie/client';\nimport { useFonderieSubClient } from '@fonderie/react';\nimport { useCallback, useState } from 'react';\nimport { persistToken } from '../storage';\n\nexport interface IUseLoginReturn {\n\tlogin: (input: ILoginInput) => Promise<ILoginResult | IMfaRequiredResult>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tdata: ILoginResult | null;\n\t// Set when the account requires MFA: no tokens were issued — complete the\n\t// login with client.auth.mfa.verifyLogin(mfaPending.mfaToken, code).\n\tmfaPending: IMfaRequiredResult | null;\n}\n\nexport function useLogin(client?: AuthClient): IUseLoginReturn {\n\tconst auth = useFonderieSubClient(client, (c) => c.auth, 'useLogin');\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [data, setData] = useState<ILoginResult | null>(null);\n\tconst [mfaPending, setMfaPending] = useState<IMfaRequiredResult | null>(null);\n\n\tconst login = useCallback(\n\t\tasync (input: ILoginInput) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\tsetMfaPending(null);\n\t\t\ttry {\n\t\t\t\tconst { result } = await auth.login(input);\n\t\t\t\tif (isMfaRequired(result)) {\n\t\t\t\t\tsetMfaPending(result);\n\t\t\t\t\treturn result;\n\t\t\t\t}\n\t\t\t\tauth.setAccessToken(result.tokens.access);\n\t\t\t\tawait persistToken(result.tokens.access);\n\t\t\t\tsetData(result);\n\t\t\t\treturn result;\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[auth],\n\t);\n\n\treturn { login, isLoading, error, data, mfaPending };\n}\n","import AsyncStorage from '@react-native-async-storage/async-storage';\n\nexport const TOKEN_KEY = 'fonderie_access_token';\n\nexport function persistToken(token: string) {\n\treturn AsyncStorage.setItem(TOKEN_KEY, token);\n}\n\nexport function clearToken() {\n\treturn AsyncStorage.removeItem(TOKEN_KEY);\n}\n\nexport function readToken() {\n\treturn AsyncStorage.getItem(TOKEN_KEY);\n}\n","import type { AuthClient } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useFonderieSubClient } from '@fonderie/react';\nimport { useCallback, useState } from 'react';\nimport { clearToken } from '../storage';\n\nexport interface IUseLogoutReturn {\n\tlogout: (refreshToken?: string) => Promise<void>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n}\n\nexport function useLogout(client?: AuthClient): IUseLogoutReturn {\n\tconst auth = useFonderieSubClient(client, (c) => c.auth, 'useLogout');\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\n\tconst logout = useCallback(async (refreshToken?: string) => {\n\t\tsetIsLoading(true);\n\t\tsetError(null);\n\t\ttry {\n\t\t\tawait auth.logout(refreshToken);\n\t\t} catch (err) {\n\t\t\tconst apiError =\n\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\tsetError(apiError);\n\t\t} finally {\n\t\t\tauth.setAccessToken(undefined);\n\t\t\tawait clearToken();\n\t\t\tsetIsLoading(false);\n\t\t}\n\t}, [auth]);\n\n\treturn { logout, isLoading, error };\n}\n","import type { AuthClient, IRegisterInput, IRegisterResult } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useFonderieSubClient } from '@fonderie/react';\nimport { useCallback, useState } from 'react';\nimport { persistToken } from '../storage';\n\nexport interface IUseRegisterReturn {\n\tregister: (input: IRegisterInput) => Promise<IRegisterResult>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tdata: IRegisterResult | null;\n}\n\nexport function useRegister(client?: AuthClient): IUseRegisterReturn {\n\tconst auth = useFonderieSubClient(client, (c) => c.auth, 'useRegister');\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [data, setData] = useState<IRegisterResult | null>(null);\n\n\tconst register = useCallback(\n\t\tasync (input: IRegisterInput) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\ttry {\n\t\t\t\tconst { result } = await auth.register(input);\n\t\t\t\tauth.setAccessToken(result.tokens.access);\n\t\t\t\tawait persistToken(result.tokens.access);\n\t\t\t\tsetData(result);\n\t\t\t\treturn result;\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[auth],\n\t);\n\n\treturn { register, isLoading, error, data };\n}\n","import type { AuthClient, IResetPasswordInput } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useFonderieSubClient } from '@fonderie/react';\nimport { useCallback, useState } from 'react';\n\nexport interface IUseResetPasswordReturn {\n\tresetPassword: (input: IResetPasswordInput) => Promise<void>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tdone: boolean;\n}\n\nexport function useResetPassword(client?: AuthClient): IUseResetPasswordReturn {\n\tconst auth = useFonderieSubClient(client, (c) => c.auth, 'useResetPassword');\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [done, setDone] = useState(false);\n\n\tconst resetPassword = useCallback(\n\t\tasync (input: IResetPasswordInput) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\ttry {\n\t\t\t\tawait auth.resetPassword(input);\n\t\t\t\tsetDone(true);\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally {\n\t\t\t\tsetIsLoading(false);\n\t\t\t}\n\t\t},\n\t\t[auth],\n\t);\n\n\treturn { resetPassword, isLoading, error, done };\n}\n","import type { AuthClient, IUserDTO } from '@fonderie/client';\nimport { useFonderieSubClient } from '@fonderie/react';\nimport { useCallback, useEffect, useState } from 'react';\nimport { clearToken, readToken } from '../storage';\n\nexport interface IUseSessionReturn {\n\tuser: IUserDTO | null;\n\tisLoading: boolean;\n\tisAuthenticated: boolean;\n\trefresh: () => Promise<void>;\n\tlogout: (refreshToken?: string) => Promise<void>;\n}\n\nexport function useSession(client?: AuthClient): IUseSessionReturn {\n\tconst auth = useFonderieSubClient(client, (c) => c.auth, 'useSession');\n\tconst [user, setUser] = useState<IUserDTO | null>(null);\n\tconst [isLoading, setIsLoading] = useState(true);\n\tconst [isAuthenticated, setIsAuthenticated] = useState(false);\n\n\tconst refresh = useCallback(async () => {\n\t\tsetIsLoading(true);\n\t\ttry {\n\t\t\tconst { result } = await auth.getUser();\n\t\t\tsetUser(result.user);\n\t\t\tsetIsAuthenticated(true);\n\t\t} catch {\n\t\t\tsetUser(null);\n\t\t\tsetIsAuthenticated(false);\n\t\t\tauth.setAccessToken(undefined);\n\t\t\tawait clearToken();\n\t\t} finally {\n\t\t\tsetIsLoading(false);\n\t\t}\n\t}, [auth]);\n\n\tconst logout = useCallback(async (refreshToken?: string) => {\n\t\ttry {\n\t\t\tawait auth.logout(refreshToken);\n\t\t} catch {\n\t\t\t// Session is being torn down regardless of server response.\n\t\t}\n\t\tauth.setAccessToken(undefined);\n\t\tawait clearToken();\n\t\tsetUser(null);\n\t\tsetIsAuthenticated(false);\n\t}, [auth]);\n\n\tuseEffect(() => {\n\t\treadToken().then((token) => {\n\t\t\tif (token) auth.setAccessToken(token);\n\t\t\tvoid refresh();\n\t\t});\n\t}, [auth, refresh]);\n\n\treturn { user, isLoading, isAuthenticated, refresh, logout };\n}\n","import type { AuthClient, IVerifyEmailResult } from '@fonderie/client';\nimport { FonderieApiError } from '@fonderie/client';\nimport { useFonderieSubClient } from '@fonderie/react';\nimport { useCallback, useState } from 'react';\n\nexport interface IUseVerifyEmailReturn {\n\tverifyEmail: (pin: string) => Promise<IVerifyEmailResult>;\n\tisLoading: boolean;\n\terror: FonderieApiError | null;\n\tdata: IVerifyEmailResult | null;\n}\n\nexport function useVerifyEmail(client?: AuthClient): IUseVerifyEmailReturn {\n\tconst auth = useFonderieSubClient(client, (c) => c.auth, 'useVerifyEmail');\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<FonderieApiError | null>(null);\n\tconst [data, setData] = useState<IVerifyEmailResult | null>(null);\n\n\tconst verifyEmail = useCallback(\n\t\tasync (pin: string) => {\n\t\t\tsetIsLoading(true);\n\t\t\tsetError(null);\n\t\t\ttry {\n\t\t\t\tconst { result } = await auth.verifyEmail(pin);\n\t\t\t\tsetData(result);\n\t\t\t\treturn result;\n\t\t\t} catch (err) {\n\t\t\t\tconst apiError =\n\t\t\t\t\terr instanceof FonderieApiError ? err : new FonderieApiError('unknown', String(err), 0);\n\t\t\t\tsetError(apiError);\n\t\t\t\tthrow apiError;\n\t\t\t} finally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AK;AACb,cAAM,WACL,eAAeF,oBAAmB,MAAM,IAAIA,kBAAiB,WAAW,OAAO,GAAG,GAAG,CAAC;AACvF,iBAAS,QAAQ;AACjB,cAAM;AAAA,MACP,UAAE;AACD,qBAAa,KAAK;AAAA,MACnB;AAAA,IACD;AAAA,IACA,CAAC,IAAI;AAAA,EACN;AAEA,SAAO,EAAE,aAAa,WAAW,OAAO,KAAK;AAC9C;","names":["FonderieApiError","FonderieApiError","useFonderieSubClient","useCallback","useState","useFonderieSubClient","useState","useCallback","FonderieApiError","FonderieApiError","useFonderieSubClient","useCallback","useState","useFonderieSubClient","useState","useCallback","FonderieApiError","FonderieApiError","useFonderieSubClient","useCallback","useState","useFonderieSubClient","useState","useCallback","FonderieApiError","FonderieApiError","useFonderieSubClient","useCallback","useState","useFonderieSubClient","useCallback","useState","useFonderieSubClient","useState","useCallback","FonderieApiError","useFonderieSubClient","useCallback","useState"]}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fonderie/react-native-auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "React Native hooks for Fonderie auth — thin bindings over @fonderie/client.",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"
|
|
6
|
+
"fonderiejs",
|
|
7
7
|
"react-native",
|
|
8
8
|
"auth",
|
|
9
9
|
"hooks",
|
|
@@ -28,25 +28,28 @@
|
|
|
28
28
|
"build": "tsup",
|
|
29
29
|
"dev": "tsup --watch",
|
|
30
30
|
"typecheck": "tsc --noEmit",
|
|
31
|
+
"test": "tsx --tsconfig tsconfig.test.json --test src/__tests__/*.test.tsx",
|
|
31
32
|
"lint": "biome lint src",
|
|
32
33
|
"format": "biome format --write src",
|
|
33
34
|
"check": "biome check --write src"
|
|
34
35
|
},
|
|
35
36
|
"dependencies": {
|
|
36
|
-
"@fonderie/client": "*"
|
|
37
|
+
"@fonderie/client": "*",
|
|
38
|
+
"@fonderie/react": "*"
|
|
37
39
|
},
|
|
38
40
|
"peerDependencies": {
|
|
39
41
|
"react": ">=18.0.0",
|
|
40
42
|
"@react-native-async-storage/async-storage": ">=1.18.0"
|
|
41
43
|
},
|
|
42
44
|
"devDependencies": {
|
|
43
|
-
"@react-native-async-storage/async-storage": "1.
|
|
44
|
-
"@types/
|
|
45
|
-
"react
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"react": "
|
|
45
|
+
"@react-native-async-storage/async-storage": "3.1.1",
|
|
46
|
+
"@types/node": "^26.2.0",
|
|
47
|
+
"@types/react": "^19.2.18",
|
|
48
|
+
"react": "^19.2.8",
|
|
49
|
+
"react-dom": "^19.2.8",
|
|
50
|
+
"react-native": "0.87.0",
|
|
49
51
|
"tsup": "^8.5.1",
|
|
52
|
+
"tsx": "^4.21.0",
|
|
50
53
|
"typescript": "^6.0.3"
|
|
51
54
|
},
|
|
52
55
|
"publishConfig": {
|