dauth-context-react 2.1.0 → 2.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/dist/index.js CHANGED
@@ -1,8 +1,733 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
19
 
2
- 'use strict'
20
+ // src/index.tsx
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ DauthProvider: () => DauthProvider,
24
+ useDauth: () => useDauth
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var import_react = require("react");
3
28
 
4
- if (process.env.NODE_ENV === 'production') {
5
- module.exports = require('./dauth-context-react.cjs.production.min.js')
6
- } else {
7
- module.exports = require('./dauth-context-react.cjs.development.js')
29
+ // src/initialDauthState.ts
30
+ var initialDauthState = {
31
+ user: {
32
+ language: (typeof window !== "undefined" ? window.document.documentElement.getAttribute("lang") : null) || "es"
33
+ },
34
+ domain: {},
35
+ isLoading: true,
36
+ isAuthenticated: false,
37
+ loginWithRedirect: () => {
38
+ },
39
+ logout: () => {
40
+ },
41
+ getAccessToken: () => Promise.resolve(""),
42
+ updateUser: () => Promise.resolve(false),
43
+ updateUserWithRedirect: () => {
44
+ },
45
+ // Send email verification
46
+ sendEmailVerificationStatus: {
47
+ status: {
48
+ type: "info",
49
+ message: "Sending email verification..."
50
+ },
51
+ isLoading: false
52
+ },
53
+ sendEmailVerification: () => Promise.resolve(false),
54
+ deleteAccount: () => Promise.resolve(false)
55
+ };
56
+ var initialDauthState_default = initialDauthState;
57
+
58
+ // src/reducer/dauth.types.ts
59
+ var LOGIN = "LOGIN";
60
+ var SET_IS_LOADING = "SET_IS_LOADING";
61
+ var UPDATE_USER = "UPDATE_USER";
62
+ var SET_SEND_EMAIL_VERIFICATION_IS_LOADING = "SET_SEND_EMAIL_VERIFICATION_IS_LOADING";
63
+ var SET_SEND_EMAIL_VERIFICATION_STATUS = "SET_SEND_EMAIL_VERIFICATION_STATUS";
64
+
65
+ // src/reducer/dauth.reducer.ts
66
+ function userReducer(state, action) {
67
+ const { type, payload } = action;
68
+ switch (type) {
69
+ case LOGIN: {
70
+ const login = {
71
+ ...state,
72
+ user: payload.user,
73
+ domain: payload.domain,
74
+ isAuthenticated: payload.isAuthenticated
75
+ };
76
+ return login;
77
+ }
78
+ case SET_IS_LOADING: {
79
+ const isLoading = {
80
+ ...state,
81
+ isLoading: payload.isLoading
82
+ };
83
+ return isLoading;
84
+ }
85
+ case UPDATE_USER: {
86
+ const updateUser = {
87
+ ...state,
88
+ user: {
89
+ ...state.user,
90
+ ...payload
91
+ }
92
+ };
93
+ return updateUser;
94
+ }
95
+ case SET_SEND_EMAIL_VERIFICATION_STATUS: {
96
+ const setSendEmailVerificationStatus = {
97
+ ...state,
98
+ sendEmailVerificationStatus: {
99
+ ...state.sendEmailVerificationStatus,
100
+ status: {
101
+ type: payload.type,
102
+ message: payload.message
103
+ }
104
+ }
105
+ };
106
+ return setSendEmailVerificationStatus;
107
+ }
108
+ case SET_SEND_EMAIL_VERIFICATION_IS_LOADING: {
109
+ const setSendEmailVerificationIsLoading = {
110
+ ...state,
111
+ sendEmailVerificationStatus: {
112
+ ...state.sendEmailVerificationStatus,
113
+ isLoading: payload
114
+ }
115
+ };
116
+ return setSendEmailVerificationIsLoading;
117
+ }
118
+ default:
119
+ return state;
120
+ }
121
+ }
122
+
123
+ // src/api/utils/config.ts
124
+ var apiVersion = "v1";
125
+ var serverDomain = "dauth.ovh";
126
+ function checkIsLocalhost() {
127
+ if (typeof window === "undefined") return false;
128
+ const hostname = window.location.hostname;
129
+ return Boolean(
130
+ hostname === "localhost" || hostname === "[::1]" || hostname.match(
131
+ /(192)\.(168)\.(1)\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gm
132
+ ) || hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/)
133
+ );
134
+ }
135
+ function getServerBasePath() {
136
+ const isLocalhost = checkIsLocalhost();
137
+ const serverPort = 4012;
138
+ const serverLocalUrl = `${window.location.protocol}//${window.location.hostname}:${serverPort}/api/${apiVersion}`;
139
+ const serverProdUrl = `https://${serverDomain}/api/${apiVersion}`;
140
+ return isLocalhost ? serverLocalUrl : serverProdUrl;
141
+ }
142
+ function getClientBasePath() {
143
+ const isLocalhost = checkIsLocalhost();
144
+ const clientPort = 5185;
145
+ const clientLocalUrl = `${window.location.protocol}//${window.location.hostname}:${clientPort}`;
146
+ const clientProdUrl = `https://${serverDomain}`;
147
+ return isLocalhost ? clientLocalUrl : clientProdUrl;
148
+ }
149
+
150
+ // src/api/dauth.api.ts
151
+ var getUserAPI = async (domainName, token) => {
152
+ const params = {
153
+ method: "GET",
154
+ headers: {
155
+ Authorization: token,
156
+ "Content-Type": "application/json"
157
+ }
158
+ };
159
+ const response = await fetch(
160
+ `${getServerBasePath()}/app/${domainName}/user`,
161
+ params
162
+ );
163
+ const data = await response.json();
164
+ return { response, data };
165
+ };
166
+ var updateUserAPI = async (domainName, user, token) => {
167
+ const params = {
168
+ method: "PATCH",
169
+ headers: {
170
+ Authorization: token,
171
+ "Content-Type": "application/json"
172
+ },
173
+ body: JSON.stringify(user)
174
+ };
175
+ const response = await fetch(
176
+ `${getServerBasePath()}/app/${domainName}/user`,
177
+ params
178
+ );
179
+ const data = await response.json();
180
+ return { response, data };
181
+ };
182
+ var sendEmailVerificationAPI = async (domainName, token) => {
183
+ const params = {
184
+ method: "GET",
185
+ headers: {
186
+ Authorization: token,
187
+ "Content-Type": "application/json"
188
+ }
189
+ };
190
+ const response = await fetch(
191
+ `${getServerBasePath()}/app/${domainName}/resend-email-verification`,
192
+ params
193
+ );
194
+ const data = await response.json();
195
+ return { response, data };
196
+ };
197
+ var refreshTokenAPI = async (domainName, refreshToken) => {
198
+ const params = {
199
+ method: "POST",
200
+ headers: { "Content-Type": "application/json" },
201
+ body: JSON.stringify({ refreshToken })
202
+ };
203
+ const response = await fetch(
204
+ `${getServerBasePath()}/app/${domainName}/refresh-token`,
205
+ params
206
+ );
207
+ const data = await response.json();
208
+ return { response, data };
209
+ };
210
+ var deleteAccountAPI = async (domainName, token) => {
211
+ const params = {
212
+ method: "DELETE",
213
+ headers: {
214
+ Authorization: token,
215
+ "Content-Type": "application/json"
216
+ }
217
+ };
218
+ const response = await fetch(
219
+ `${getServerBasePath()}/app/${domainName}/user`,
220
+ params
221
+ );
222
+ const data = await response.json();
223
+ return { response, data };
224
+ };
225
+ var logoutAPI = async (domainName, refreshToken) => {
226
+ const params = {
227
+ method: "POST",
228
+ headers: { "Content-Type": "application/json" },
229
+ body: JSON.stringify({ refreshToken })
230
+ };
231
+ const response = await fetch(
232
+ `${getServerBasePath()}/app/${domainName}/logout`,
233
+ params
234
+ );
235
+ return { response };
236
+ };
237
+
238
+ // src/reducer/dauth.actions.ts
239
+ async function setDauthStateAction({
240
+ dispatch,
241
+ token,
242
+ refreshToken,
243
+ domainName,
244
+ storageKeys,
245
+ onError
246
+ }) {
247
+ dispatch({ type: SET_IS_LOADING, payload: { isLoading: true } });
248
+ try {
249
+ const getUserFetch = await getUserAPI(domainName, token);
250
+ if (getUserFetch.response.status === 200) {
251
+ dispatch({
252
+ type: LOGIN,
253
+ payload: {
254
+ user: getUserFetch.data.user,
255
+ domain: getUserFetch.data.domain,
256
+ isAuthenticated: true
257
+ }
258
+ });
259
+ window.history.replaceState({}, document.title, window.location.pathname);
260
+ localStorage.setItem(storageKeys.accessToken, token);
261
+ localStorage.setItem(storageKeys.refreshToken, refreshToken);
262
+ return;
263
+ } else {
264
+ return resetUser(dispatch, storageKeys);
265
+ }
266
+ } catch (error) {
267
+ onError(error instanceof Error ? error : new Error(String(error)));
268
+ return resetUser(dispatch, storageKeys);
269
+ } finally {
270
+ dispatch({
271
+ type: SET_IS_LOADING,
272
+ payload: { isLoading: false }
273
+ });
274
+ }
275
+ }
276
+ async function setAutoLoginAction({
277
+ dispatch,
278
+ domainName,
279
+ storageKeys,
280
+ onError
281
+ }) {
282
+ dispatch({ type: SET_IS_LOADING, payload: { isLoading: true } });
283
+ const storedRefreshToken = localStorage.getItem(storageKeys.refreshToken);
284
+ if (!storedRefreshToken) {
285
+ dispatch({
286
+ type: SET_IS_LOADING,
287
+ payload: { isLoading: false }
288
+ });
289
+ return resetUser(dispatch, storageKeys);
290
+ }
291
+ try {
292
+ const refreshResult = await refreshTokenAPI(domainName, storedRefreshToken);
293
+ if (refreshResult.response.status === 200) {
294
+ const newAccessToken = refreshResult.data.accessToken;
295
+ const newRefreshToken = refreshResult.data.refreshToken;
296
+ localStorage.setItem(storageKeys.accessToken, newAccessToken);
297
+ localStorage.setItem(storageKeys.refreshToken, newRefreshToken);
298
+ const getUserFetch = await getUserAPI(domainName, newAccessToken);
299
+ if (getUserFetch.response.status === 200) {
300
+ dispatch({
301
+ type: LOGIN,
302
+ payload: {
303
+ user: getUserFetch.data.user,
304
+ domain: getUserFetch.data.domain,
305
+ isAuthenticated: true
306
+ }
307
+ });
308
+ return;
309
+ }
310
+ }
311
+ resetUser(dispatch, storageKeys);
312
+ } catch (error) {
313
+ onError(error instanceof Error ? error : new Error(String(error)));
314
+ resetUser(dispatch, storageKeys);
315
+ } finally {
316
+ dispatch({
317
+ type: SET_IS_LOADING,
318
+ payload: { isLoading: false }
319
+ });
320
+ }
8
321
  }
322
+ async function setLogoutAction({
323
+ dispatch,
324
+ domainName,
325
+ storageKeys
326
+ }) {
327
+ const storedRefreshToken = localStorage.getItem(storageKeys.refreshToken);
328
+ if (storedRefreshToken && domainName) {
329
+ try {
330
+ await logoutAPI(domainName, storedRefreshToken);
331
+ } catch (_) {
332
+ }
333
+ }
334
+ dispatch({ type: SET_IS_LOADING, payload: { isLoading: true } });
335
+ dispatch({
336
+ type: LOGIN,
337
+ payload: {
338
+ user: {
339
+ language: window.document.documentElement.getAttribute("lang") || "es"
340
+ },
341
+ domain: {},
342
+ isAuthenticated: false
343
+ }
344
+ });
345
+ localStorage.removeItem(storageKeys.accessToken);
346
+ localStorage.removeItem(storageKeys.refreshToken);
347
+ return dispatch({
348
+ type: SET_IS_LOADING,
349
+ payload: { isLoading: false }
350
+ });
351
+ }
352
+ async function refreshSessionAction({
353
+ dispatch,
354
+ domainName,
355
+ storageKeys,
356
+ onError
357
+ }) {
358
+ const storedRefreshToken = localStorage.getItem(storageKeys.refreshToken);
359
+ if (!storedRefreshToken) {
360
+ return resetUser(dispatch, storageKeys);
361
+ }
362
+ try {
363
+ const refreshResult = await refreshTokenAPI(domainName, storedRefreshToken);
364
+ if (refreshResult.response.status === 200) {
365
+ localStorage.setItem(
366
+ storageKeys.accessToken,
367
+ refreshResult.data.accessToken
368
+ );
369
+ localStorage.setItem(
370
+ storageKeys.refreshToken,
371
+ refreshResult.data.refreshToken
372
+ );
373
+ return;
374
+ }
375
+ resetUser(dispatch, storageKeys);
376
+ } catch (error) {
377
+ onError(error instanceof Error ? error : new Error(String(error)));
378
+ resetUser(dispatch, storageKeys);
379
+ }
380
+ }
381
+ async function setUpdateUserAction({
382
+ dispatch,
383
+ domainName,
384
+ user,
385
+ token,
386
+ onError
387
+ }) {
388
+ if (user.language) {
389
+ window.document.documentElement.setAttribute("lang", user.language);
390
+ }
391
+ if (!token) {
392
+ dispatch({
393
+ type: UPDATE_USER,
394
+ payload: user
395
+ });
396
+ return false;
397
+ }
398
+ try {
399
+ const getUserFetch = await updateUserAPI(domainName, user, token);
400
+ if (getUserFetch.response.status === 200) {
401
+ dispatch({
402
+ type: UPDATE_USER,
403
+ payload: getUserFetch.data.user
404
+ });
405
+ return true;
406
+ } else {
407
+ onError(new Error("Update user error: " + getUserFetch.data.message));
408
+ return false;
409
+ }
410
+ } catch (error) {
411
+ onError(
412
+ error instanceof Error ? error : new Error("Update user error")
413
+ );
414
+ return false;
415
+ }
416
+ }
417
+ async function sendEmailVerificationAction({
418
+ dispatch,
419
+ domainName,
420
+ token
421
+ }) {
422
+ dispatch({
423
+ type: SET_SEND_EMAIL_VERIFICATION_IS_LOADING,
424
+ payload: true
425
+ });
426
+ dispatch({
427
+ type: SET_SEND_EMAIL_VERIFICATION_STATUS,
428
+ payload: { type: "info", message: "Sending email verification..." }
429
+ });
430
+ try {
431
+ const sendEmailFetch = await sendEmailVerificationAPI(domainName, token);
432
+ if (sendEmailFetch.response.status === 200) {
433
+ dispatch({
434
+ type: SET_SEND_EMAIL_VERIFICATION_STATUS,
435
+ payload: { type: "success", message: sendEmailFetch.data.message }
436
+ });
437
+ dispatch({
438
+ type: SET_SEND_EMAIL_VERIFICATION_IS_LOADING,
439
+ payload: false
440
+ });
441
+ return true;
442
+ } else {
443
+ dispatch({
444
+ type: SET_SEND_EMAIL_VERIFICATION_STATUS,
445
+ payload: { type: "error", message: sendEmailFetch.data.message }
446
+ });
447
+ dispatch({
448
+ type: SET_SEND_EMAIL_VERIFICATION_IS_LOADING,
449
+ payload: false
450
+ });
451
+ return false;
452
+ }
453
+ } catch (_) {
454
+ dispatch({
455
+ type: SET_SEND_EMAIL_VERIFICATION_STATUS,
456
+ payload: {
457
+ type: "error",
458
+ message: "Send email verification fetch error"
459
+ }
460
+ });
461
+ dispatch({
462
+ type: SET_SEND_EMAIL_VERIFICATION_IS_LOADING,
463
+ payload: false
464
+ });
465
+ return false;
466
+ }
467
+ }
468
+ async function getAccessTokenAction({
469
+ dispatch,
470
+ domainName,
471
+ storageKeys,
472
+ onError
473
+ }) {
474
+ const token_ls = localStorage.getItem(storageKeys.accessToken);
475
+ if (!token_ls) return "token-not-found";
476
+ try {
477
+ const payloadB64 = token_ls.split(".")[1];
478
+ if (payloadB64) {
479
+ const payload = JSON.parse(atob(payloadB64));
480
+ const expiresIn = (payload.exp || 0) * 1e3 - Date.now();
481
+ if (expiresIn < 5 * 60 * 1e3) {
482
+ await refreshSessionAction({
483
+ dispatch,
484
+ domainName,
485
+ storageKeys,
486
+ onError
487
+ });
488
+ const refreshedToken = localStorage.getItem(storageKeys.accessToken);
489
+ return refreshedToken || "token-not-found";
490
+ }
491
+ }
492
+ } catch (_) {
493
+ }
494
+ return token_ls;
495
+ }
496
+ async function deleteAccountAction({
497
+ dispatch,
498
+ domainName,
499
+ storageKeys,
500
+ onError,
501
+ token
502
+ }) {
503
+ try {
504
+ const result = await deleteAccountAPI(domainName, token);
505
+ if (result.response.status === 200) {
506
+ resetUser(dispatch, storageKeys);
507
+ return true;
508
+ }
509
+ return false;
510
+ } catch (error) {
511
+ onError(
512
+ error instanceof Error ? error : new Error("Delete account error")
513
+ );
514
+ return false;
515
+ }
516
+ }
517
+ var resetUser = (dispatch, storageKeys) => {
518
+ localStorage.removeItem(storageKeys.accessToken);
519
+ localStorage.removeItem(storageKeys.refreshToken);
520
+ return dispatch({
521
+ type: LOGIN,
522
+ payload: {
523
+ user: {},
524
+ domain: {},
525
+ isAuthenticated: false
526
+ }
527
+ });
528
+ };
529
+
530
+ // src/constants.ts
531
+ var TOKEN_LS = "dauth_state";
532
+ var REFRESH_TOKEN_LS = "dauth_refresh_token";
533
+
534
+ // src/api/utils/routes.ts
535
+ var routes = {
536
+ signin: "signin",
537
+ updateUser: "update-user"
538
+ };
539
+
540
+ // src/index.tsx
541
+ var import_jsx_runtime = require("react/jsx-runtime");
542
+ var defaultOnError = (error) => console.error(error);
543
+ var DauthProvider = (props) => {
544
+ const { domainName, children, storageKey, onError } = props;
545
+ const [dauthState, dispatch] = (0, import_react.useReducer)(userReducer, initialDauthState_default);
546
+ const refreshTimerRef = (0, import_react.useRef)(null);
547
+ const storageKeys = (0, import_react.useMemo)(
548
+ () => ({
549
+ accessToken: storageKey?.accessToken ?? TOKEN_LS,
550
+ refreshToken: storageKey?.refreshToken ?? REFRESH_TOKEN_LS
551
+ }),
552
+ [storageKey?.accessToken, storageKey?.refreshToken]
553
+ );
554
+ const handleError = (0, import_react.useCallback)(
555
+ (error) => (onError ?? defaultOnError)(error),
556
+ [onError]
557
+ );
558
+ const ctx = (0, import_react.useMemo)(
559
+ () => ({ dispatch, domainName, storageKeys, onError: handleError }),
560
+ [domainName, storageKeys, handleError]
561
+ );
562
+ const scheduleRefresh = (0, import_react.useCallback)(() => {
563
+ if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
564
+ const token = localStorage.getItem(storageKeys.accessToken);
565
+ if (!token) return;
566
+ try {
567
+ const payloadB64 = token.split(".")[1];
568
+ if (!payloadB64) return;
569
+ const payload = JSON.parse(atob(payloadB64));
570
+ const expiresIn = (payload.exp || 0) * 1e3 - Date.now();
571
+ const refreshIn = Math.max(expiresIn - 5 * 60 * 1e3, 1e4);
572
+ refreshTimerRef.current = setTimeout(async () => {
573
+ await refreshSessionAction(ctx);
574
+ scheduleRefresh();
575
+ }, refreshIn);
576
+ } catch (_) {
577
+ refreshTimerRef.current = setTimeout(async () => {
578
+ await refreshSessionAction(ctx);
579
+ scheduleRefresh();
580
+ }, 5 * 60 * 1e3);
581
+ }
582
+ }, [ctx, storageKeys.accessToken]);
583
+ (0, import_react.useEffect)(() => {
584
+ (async () => {
585
+ const queryString = window.location.search;
586
+ if (!queryString) return;
587
+ const urlParams = new URLSearchParams(queryString);
588
+ const token_url = urlParams.get(storageKeys.accessToken);
589
+ const refresh_url = urlParams.get(storageKeys.refreshToken);
590
+ if (token_url && refresh_url && !dauthState.isAuthenticated) {
591
+ return setDauthStateAction({
592
+ ...ctx,
593
+ token: token_url,
594
+ refreshToken: refresh_url
595
+ });
596
+ }
597
+ })();
598
+ }, []);
599
+ (0, import_react.useEffect)(() => {
600
+ (async () => {
601
+ const refreshToken = localStorage.getItem(storageKeys.refreshToken);
602
+ if (refreshToken && !dauthState.isAuthenticated) {
603
+ return setAutoLoginAction(ctx);
604
+ } else {
605
+ return dispatch({
606
+ type: SET_IS_LOADING,
607
+ payload: { isLoading: false }
608
+ });
609
+ }
610
+ })();
611
+ }, []);
612
+ (0, import_react.useEffect)(() => {
613
+ if (dauthState.isAuthenticated) {
614
+ scheduleRefresh();
615
+ }
616
+ return () => {
617
+ if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
618
+ };
619
+ }, [dauthState.isAuthenticated, scheduleRefresh]);
620
+ const loginWithRedirect = (0, import_react.useCallback)(() => {
621
+ return window.location.replace(
622
+ `${getClientBasePath()}/${domainName}/${routes.signin}`
623
+ );
624
+ }, [domainName]);
625
+ const logout = (0, import_react.useCallback)(() => {
626
+ if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
627
+ return setLogoutAction({
628
+ dispatch,
629
+ domainName,
630
+ storageKeys
631
+ });
632
+ }, [domainName, storageKeys]);
633
+ const getAccessToken = (0, import_react.useCallback)(async () => {
634
+ const token = await getAccessTokenAction(ctx);
635
+ return token;
636
+ }, [ctx]);
637
+ const updateUser = (0, import_react.useCallback)(
638
+ async (fields) => {
639
+ const token_ls = localStorage.getItem(storageKeys.accessToken);
640
+ const {
641
+ name,
642
+ lastname,
643
+ nickname,
644
+ telPrefix,
645
+ telSuffix,
646
+ language,
647
+ avatar,
648
+ birthDate,
649
+ country,
650
+ metadata
651
+ } = fields;
652
+ const user = {
653
+ name,
654
+ lastname,
655
+ nickname,
656
+ telPrefix,
657
+ telSuffix,
658
+ language,
659
+ avatar,
660
+ birthDate,
661
+ country,
662
+ metadata
663
+ };
664
+ return await setUpdateUserAction({
665
+ ...ctx,
666
+ user,
667
+ token: token_ls
668
+ });
669
+ },
670
+ [ctx, storageKeys.accessToken]
671
+ );
672
+ const updateUserWithRedirect = (0, import_react.useCallback)(() => {
673
+ const token_ls = localStorage.getItem(storageKeys.accessToken);
674
+ if (!token_ls) return;
675
+ return window.location.replace(
676
+ `${getClientBasePath()}/${domainName}/${routes.updateUser}/${token_ls}`
677
+ );
678
+ }, [domainName, storageKeys.accessToken]);
679
+ const sendEmailVerification = (0, import_react.useCallback)(async () => {
680
+ const token_ls = localStorage.getItem(storageKeys.accessToken);
681
+ if (!token_ls) return false;
682
+ return await sendEmailVerificationAction({
683
+ ...ctx,
684
+ token: token_ls
685
+ });
686
+ }, [ctx, storageKeys.accessToken]);
687
+ const deleteAccount = (0, import_react.useCallback)(async () => {
688
+ const token_ls = localStorage.getItem(storageKeys.accessToken);
689
+ if (!token_ls) return false;
690
+ if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
691
+ return await deleteAccountAction({
692
+ ...ctx,
693
+ token: token_ls
694
+ });
695
+ }, [ctx, storageKeys.accessToken]);
696
+ const memoProvider = (0, import_react.useMemo)(
697
+ () => ({
698
+ ...dauthState,
699
+ loginWithRedirect,
700
+ logout,
701
+ getAccessToken,
702
+ updateUser,
703
+ updateUserWithRedirect,
704
+ sendEmailVerification,
705
+ deleteAccount
706
+ }),
707
+ [
708
+ dauthState,
709
+ loginWithRedirect,
710
+ logout,
711
+ getAccessToken,
712
+ updateUser,
713
+ updateUserWithRedirect,
714
+ sendEmailVerification,
715
+ deleteAccount
716
+ ]
717
+ );
718
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(DauthContext.Provider, { value: memoProvider, children });
719
+ };
720
+ var DauthContext = (0, import_react.createContext)(initialDauthState_default);
721
+ var useDauth = () => {
722
+ const context = (0, import_react.useContext)(DauthContext);
723
+ if (!context) {
724
+ throw new Error("useDauth must be used inside DauthProvider");
725
+ }
726
+ return context;
727
+ };
728
+ // Annotate the CommonJS export names for ESM import in node:
729
+ 0 && (module.exports = {
730
+ DauthProvider,
731
+ useDauth
732
+ });
733
+ //# sourceMappingURL=index.js.map