@ttoss/react-auth-core 0.4.29 → 0.4.30

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.mjs ADDED
@@ -0,0 +1,1059 @@
1
+ /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
+ import { NotificationsBox, useNotifications } from "@ttoss/react-notifications";
3
+ import { Box, Button, Flex, Heading, Link, Text } from "@ttoss/ui";
4
+ import * as React from "react";
5
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
6
+ import { NotificationCard } from "@ttoss/components/NotificationCard";
7
+ import { useI18n } from "@ttoss/react-i18n";
8
+ import { Form, FormFieldCheckbox, FormFieldInput, FormFieldPassword, useForm, yup, yupResolver } from "@ttoss/forms";
9
+ import { notify } from "@ttoss/logger";
10
+ import { ErrorBoundary as ErrorBoundary$1 } from "react-error-boundary";
11
+
12
+ //#region src/AuthCard.tsx
13
+ const LogoContext = React.createContext({});
14
+ const LogoProvider = ({
15
+ children,
16
+ ...values
17
+ }) => {
18
+ return /* @__PURE__ */jsx(LogoContext.Provider, {
19
+ value: values,
20
+ children
21
+ });
22
+ };
23
+ const AuthCard = ({
24
+ children,
25
+ title,
26
+ buttonLabel,
27
+ extraButton,
28
+ isValidForm
29
+ }) => {
30
+ const {
31
+ logo
32
+ } = React.useContext(LogoContext);
33
+ const {
34
+ isLoading
35
+ } = useNotifications();
36
+ return /* @__PURE__ */jsxs(Box, {
37
+ sx: {
38
+ maxWidth: "400px",
39
+ width: "full",
40
+ border: "md",
41
+ borderColor: "display.border.muted.default",
42
+ paddingX: "8",
43
+ paddingY: "9",
44
+ backgroundColor: "surface"
45
+ },
46
+ children: [logo && /* @__PURE__ */jsx(Flex, {
47
+ sx: {
48
+ width: "full",
49
+ maxHeight: "90px",
50
+ justifyContent: "center",
51
+ marginBottom: "8"
52
+ },
53
+ children: logo
54
+ }), /* @__PURE__ */jsxs(Flex, {
55
+ sx: {
56
+ flexDirection: "column"
57
+ },
58
+ children: [/* @__PURE__ */jsx(Heading, {
59
+ as: "h2",
60
+ variant: "h2",
61
+ sx: {
62
+ marginBottom: "8"
63
+ },
64
+ children: title
65
+ }), children, /* @__PURE__ */jsxs(Flex, {
66
+ sx: {
67
+ flexDirection: "column",
68
+ width: "100%",
69
+ gap: "7",
70
+ marginTop: "8"
71
+ },
72
+ children: [/* @__PURE__ */jsx(Button, {
73
+ type: "submit",
74
+ variant: "accent",
75
+ disabled: isLoading || !isValidForm,
76
+ sx: {
77
+ textAlign: "center",
78
+ display: "initial"
79
+ },
80
+ loading: isLoading,
81
+ children: buttonLabel
82
+ }), extraButton]
83
+ })]
84
+ })]
85
+ });
86
+ };
87
+
88
+ //#endregion
89
+ //#region src/AuthConfirmSignUpCheckEmail.tsx
90
+ const AuthConfirmSignUpCheckEmail = ({
91
+ onConfirmSignUpCheckEmail
92
+ }) => {
93
+ const {
94
+ intl
95
+ } = useI18n();
96
+ return /* @__PURE__ */jsx("form", {
97
+ onSubmit: e => {
98
+ e.preventDefault();
99
+ onConfirmSignUpCheckEmail();
100
+ },
101
+ children: /* @__PURE__ */jsx(AuthCard, {
102
+ isValidForm: true,
103
+ buttonLabel: intl.formatMessage({
104
+ id: "38HSjr",
105
+ defaultMessage: [{
106
+ "type": 0,
107
+ "value": "Sign In"
108
+ }]
109
+ }),
110
+ title: intl.formatMessage({
111
+ id: "cGR2eI",
112
+ defaultMessage: [{
113
+ "type": 0,
114
+ "value": "Confirmation"
115
+ }]
116
+ }),
117
+ children: /* @__PURE__ */jsx(NotificationCard, {
118
+ type: "info",
119
+ message: intl.formatMessage({
120
+ id: "qTQ4hP",
121
+ defaultMessage: [{
122
+ "type": 0,
123
+ "value": "An email has been sent to your address. Please check your inbox and follow the instructions to confirm your sign up."
124
+ }]
125
+ })
126
+ })
127
+ })
128
+ });
129
+ };
130
+
131
+ //#endregion
132
+ //#region src/AuthConfirmSignUpWithCode.tsx
133
+ const AuthConfirmSignUpWithCode = ({
134
+ email,
135
+ onConfirmSignUpWithCode
136
+ }) => {
137
+ const {
138
+ intl
139
+ } = useI18n();
140
+ const formMethods = useForm({
141
+ resolver: yupResolver(yup.object().shape({
142
+ code: yup.string().required(intl.formatMessage({
143
+ id: "0XOzcH",
144
+ defaultMessage: [{
145
+ "type": 0,
146
+ "value": "Required field"
147
+ }]
148
+ })).max(6, intl.formatMessage({
149
+ id: "S3pjKw",
150
+ defaultMessage: [{
151
+ "type": 0,
152
+ "value": "Minimum "
153
+ }, {
154
+ "type": 1,
155
+ "value": "value"
156
+ }, {
157
+ "type": 0,
158
+ "value": " characters"
159
+ }]
160
+ }, {
161
+ value: 6
162
+ }))
163
+ }).required())
164
+ });
165
+ return /* @__PURE__ */jsx(Form, {
166
+ ...formMethods,
167
+ onSubmit: ({
168
+ code
169
+ }) => {
170
+ return onConfirmSignUpWithCode({
171
+ code,
172
+ email
173
+ });
174
+ },
175
+ children: /* @__PURE__ */jsxs(AuthCard, {
176
+ buttonLabel: intl.formatMessage({
177
+ id: "UNttd+",
178
+ defaultMessage: [{
179
+ "type": 0,
180
+ "value": "Confirm"
181
+ }]
182
+ }),
183
+ isValidForm: formMethods.formState.isValid,
184
+ title: intl.formatMessage({
185
+ id: "cGR2eI",
186
+ defaultMessage: [{
187
+ "type": 0,
188
+ "value": "Confirmation"
189
+ }]
190
+ }),
191
+ children: [/* @__PURE__ */jsx(NotificationCard, {
192
+ type: "info",
193
+ message: intl.formatMessage({
194
+ id: "pwv2cR",
195
+ defaultMessage: [{
196
+ "type": 0,
197
+ "value": "We have sent a confirmation code to your email address. Please enter the code below."
198
+ }]
199
+ })
200
+ }), /* @__PURE__ */jsx(FormFieldInput, {
201
+ sx: {
202
+ marginTop: "6"
203
+ },
204
+ name: "code",
205
+ label: intl.formatMessage({
206
+ id: "KY2T6J",
207
+ defaultMessage: [{
208
+ "type": 0,
209
+ "value": "Code"
210
+ }]
211
+ })
212
+ })]
213
+ })
214
+ });
215
+ };
216
+
217
+ //#endregion
218
+ //#region src/AuthForgotPassword.tsx
219
+ const AuthForgotPassword = props => {
220
+ const {
221
+ intl
222
+ } = useI18n();
223
+ const formMethods = useForm({
224
+ resolver: yupResolver(yup.object().shape({
225
+ email: yup.string().email(intl.formatMessage({
226
+ id: "9cApwd",
227
+ defaultMessage: [{
228
+ "type": 0,
229
+ "value": "Please, insert a valid e-mail"
230
+ }]
231
+ })).required()
232
+ }).required()),
233
+ mode: "onChange"
234
+ });
235
+ return /* @__PURE__ */jsx(Form, {
236
+ ...formMethods,
237
+ sx: {
238
+ maxWidth: "390px"
239
+ },
240
+ onSubmit: ({
241
+ email
242
+ }) => {
243
+ return props.onForgotPassword({
244
+ email
245
+ });
246
+ },
247
+ children: /* @__PURE__ */jsxs(AuthCard, {
248
+ buttonLabel: intl.formatMessage({
249
+ id: "mZzmNV",
250
+ defaultMessage: [{
251
+ "type": 0,
252
+ "value": "Recover Password"
253
+ }]
254
+ }),
255
+ isValidForm: formMethods.formState.isValid,
256
+ title: intl.formatMessage({
257
+ id: "S4bbEj",
258
+ defaultMessage: [{
259
+ "type": 0,
260
+ "value": "Recovering Password"
261
+ }]
262
+ }),
263
+ extraButton: /* @__PURE__ */jsx(Button, {
264
+ sx: {
265
+ textAlign: "center",
266
+ display: "initial"
267
+ },
268
+ variant: "secondary",
269
+ onClick: props.onGoToSignIn,
270
+ children: intl.formatMessage({
271
+ id: "6PdOcy",
272
+ defaultMessage: [{
273
+ "type": 0,
274
+ "value": "Cancel"
275
+ }]
276
+ })
277
+ }),
278
+ children: [/* @__PURE__ */jsx(FormFieldInput, {
279
+ name: "email",
280
+ label: intl.formatMessage({
281
+ id: "XreZg+",
282
+ defaultMessage: [{
283
+ "type": 0,
284
+ "value": "Registered Email"
285
+ }]
286
+ })
287
+ }), /* @__PURE__ */jsx(NotificationsBox, {}), props.onGoToSignUp && /* @__PURE__ */jsx(Text, {
288
+ sx: {
289
+ marginTop: "xl",
290
+ cursor: "pointer"
291
+ },
292
+ as: Link,
293
+ onClick: props.onGoToSignUp,
294
+ children: intl.formatMessage({
295
+ id: "lZvoYL",
296
+ defaultMessage: [{
297
+ "type": 0,
298
+ "value": "Sign up now"
299
+ }]
300
+ })
301
+ })]
302
+ })
303
+ });
304
+ };
305
+
306
+ //#endregion
307
+ //#region src/AuthForgotPasswordResetPassword.tsx
308
+ const AuthForgotPasswordResetPassword = props => {
309
+ const {
310
+ intl
311
+ } = useI18n();
312
+ const codeValidation = yup.string().required(intl.formatMessage({
313
+ id: "0XOzcH",
314
+ defaultMessage: [{
315
+ "type": 0,
316
+ "value": "Required field"
317
+ }]
318
+ }));
319
+ const formMethods = useForm({
320
+ resolver: yupResolver(yup.object().shape({
321
+ code: props.maxCodeLength ? codeValidation.min(props.maxCodeLength, intl.formatMessage({
322
+ id: "S3pjKw",
323
+ defaultMessage: [{
324
+ "type": 0,
325
+ "value": "Minimum "
326
+ }, {
327
+ "type": 1,
328
+ "value": "value"
329
+ }, {
330
+ "type": 0,
331
+ "value": " characters"
332
+ }]
333
+ }, {
334
+ value: props.maxCodeLength
335
+ })).max(props.maxCodeLength, intl.formatMessage({
336
+ id: "SfWKyS",
337
+ defaultMessage: [{
338
+ "type": 0,
339
+ "value": "Maximum "
340
+ }, {
341
+ "type": 1,
342
+ "value": "value"
343
+ }, {
344
+ "type": 0,
345
+ "value": " characters"
346
+ }]
347
+ }, {
348
+ value: props.maxCodeLength
349
+ })) : codeValidation,
350
+ newPassword: yup.string().required(intl.formatMessage({
351
+ id: "kdFYba",
352
+ defaultMessage: [{
353
+ "type": 0,
354
+ "value": "Password field is required"
355
+ }]
356
+ })).min(props.passwordMinimumLength || 8, intl.formatMessage({
357
+ id: "TZ4WUk",
358
+ defaultMessage: [{
359
+ "type": 0,
360
+ "value": "Password requires "
361
+ }, {
362
+ "type": 1,
363
+ "value": "value"
364
+ }, {
365
+ "type": 0,
366
+ "value": " characters"
367
+ }]
368
+ }, {
369
+ value: props.passwordMinimumLength || 8
370
+ })).trim()
371
+ })),
372
+ mode: "onChange"
373
+ });
374
+ return /* @__PURE__ */jsx(Form, {
375
+ ...formMethods,
376
+ sx: {
377
+ maxWidth: "390px"
378
+ },
379
+ onSubmit: ({
380
+ code,
381
+ newPassword
382
+ }) => {
383
+ return props.onForgotPasswordResetPassword({
384
+ email: props.email,
385
+ code,
386
+ newPassword
387
+ });
388
+ },
389
+ children: /* @__PURE__ */jsxs(AuthCard, {
390
+ buttonLabel: intl.formatMessage({
391
+ id: "HT4tSM",
392
+ defaultMessage: [{
393
+ "type": 0,
394
+ "value": "Reset Password"
395
+ }]
396
+ }),
397
+ isValidForm: formMethods.formState.isValid,
398
+ title: intl.formatMessage({
399
+ id: "HT4tSM",
400
+ defaultMessage: [{
401
+ "type": 0,
402
+ "value": "Reset Password"
403
+ }]
404
+ }),
405
+ extraButton: props.onGoToSignIn ? /* @__PURE__ */jsx(Button, {
406
+ type: "button",
407
+ sx: {
408
+ textAlign: "center",
409
+ display: "initial"
410
+ },
411
+ variant: "secondary",
412
+ onClick: props.onGoToSignIn,
413
+ children: intl.formatMessage({
414
+ id: "6PdOcy",
415
+ defaultMessage: [{
416
+ "type": 0,
417
+ "value": "Cancel"
418
+ }]
419
+ })
420
+ }) : null,
421
+ children: [/* @__PURE__ */jsx(FormFieldInput, {
422
+ name: "code",
423
+ label: intl.formatMessage({
424
+ id: "s1OmP0",
425
+ defaultMessage: [{
426
+ "type": 0,
427
+ "value": "Confirmation code"
428
+ }]
429
+ })
430
+ }), /* @__PURE__ */jsx(FormFieldPassword, {
431
+ name: "newPassword",
432
+ label: intl.formatMessage({
433
+ id: "e3IQoc",
434
+ defaultMessage: [{
435
+ "type": 0,
436
+ "value": "New Password"
437
+ }]
438
+ })
439
+ }), /* @__PURE__ */jsx(NotificationsBox, {})]
440
+ })
441
+ });
442
+ };
443
+
444
+ //#endregion
445
+ //#region src/AuthFullScreen.tsx
446
+ const AuthFullScreen = ({
447
+ children
448
+ }) => {
449
+ return /* @__PURE__ */jsx(Flex, {
450
+ sx: {
451
+ height: "100vh",
452
+ width: "100vw",
453
+ justifyContent: "center",
454
+ alignItems: "center",
455
+ margin: 0,
456
+ backgroundPosition: "center",
457
+ backgroundRepeat: "no-repeat",
458
+ backgroundSize: "cover"
459
+ },
460
+ children
461
+ });
462
+ };
463
+
464
+ //#endregion
465
+ //#region src/AuthSignIn.tsx
466
+ const AuthSignIn = ({
467
+ onSignIn,
468
+ onGoToSignUp,
469
+ defaultValues,
470
+ onGoToForgotPassword,
471
+ passwordMinimumLength = 8
472
+ }) => {
473
+ const {
474
+ intl
475
+ } = useI18n();
476
+ const formMethods = useForm({
477
+ defaultValues,
478
+ mode: "onChange",
479
+ resolver: yupResolver(yup.object().shape({
480
+ email: yup.string().required(intl.formatMessage({
481
+ id: "d1YCuH",
482
+ defaultMessage: [{
483
+ "type": 0,
484
+ "value": "Enter your email address"
485
+ }]
486
+ })).email(intl.formatMessage({
487
+ id: "9cApwd",
488
+ defaultMessage: [{
489
+ "type": 0,
490
+ "value": "Please, insert a valid e-mail"
491
+ }]
492
+ })),
493
+ password: yup.string().required(intl.formatMessage({
494
+ id: "kdFYba",
495
+ defaultMessage: [{
496
+ "type": 0,
497
+ "value": "Password field is required"
498
+ }]
499
+ })).min(passwordMinimumLength, intl.formatMessage({
500
+ id: "TZ4WUk",
501
+ defaultMessage: [{
502
+ "type": 0,
503
+ "value": "Password requires "
504
+ }, {
505
+ "type": 1,
506
+ "value": "value"
507
+ }, {
508
+ "type": 0,
509
+ "value": " characters"
510
+ }]
511
+ }, {
512
+ value: passwordMinimumLength
513
+ })).trim()
514
+ }))
515
+ });
516
+ const onSubmitForm = data => {
517
+ return onSignIn(data);
518
+ };
519
+ return /* @__PURE__ */jsx(Form, {
520
+ sx: {
521
+ maxWidth: "390px",
522
+ width: "100%"
523
+ },
524
+ ...formMethods,
525
+ onSubmit: onSubmitForm,
526
+ children: /* @__PURE__ */jsxs(AuthCard, {
527
+ title: intl.formatMessage({
528
+ id: "SQJto2",
529
+ defaultMessage: [{
530
+ "type": 0,
531
+ "value": "Sign in"
532
+ }]
533
+ }),
534
+ buttonLabel: intl.formatMessage({
535
+ id: "SQJto2",
536
+ defaultMessage: [{
537
+ "type": 0,
538
+ "value": "Sign in"
539
+ }]
540
+ }),
541
+ isValidForm: formMethods.formState.isValid,
542
+ extraButton: onGoToSignUp ? /* @__PURE__ */jsx(Button, {
543
+ type: "button",
544
+ variant: "secondary",
545
+ sx: {
546
+ textAlign: "center",
547
+ display: "initial"
548
+ },
549
+ onClick: onGoToSignUp,
550
+ children: intl.formatMessage({
551
+ id: "EZ3YF2",
552
+ defaultMessage: [{
553
+ "type": 0,
554
+ "value": "Sign up"
555
+ }]
556
+ })
557
+ }) : null,
558
+ children: [/* @__PURE__ */jsxs(Flex, {
559
+ sx: {
560
+ flexDirection: "column",
561
+ gap: "xl"
562
+ },
563
+ children: [/* @__PURE__ */jsx(FormFieldInput, {
564
+ name: "email",
565
+ label: intl.formatMessage({
566
+ id: "5E12mO",
567
+ defaultMessage: [{
568
+ "type": 0,
569
+ "value": "Email"
570
+ }]
571
+ })
572
+ }), /* @__PURE__ */jsx(FormFieldPassword, {
573
+ name: "password",
574
+ label: intl.formatMessage({
575
+ id: "PylVqx",
576
+ defaultMessage: [{
577
+ "type": 0,
578
+ "value": "Password"
579
+ }]
580
+ })
581
+ })]
582
+ }), onGoToForgotPassword && /* @__PURE__ */jsx(Flex, {
583
+ sx: {
584
+ justifyContent: "space-between",
585
+ marginTop: "lg"
586
+ },
587
+ children: /* @__PURE__ */jsx(Text, {
588
+ sx: {
589
+ marginLeft: "auto",
590
+ cursor: "pointer"
591
+ },
592
+ as: Link,
593
+ onClick: onGoToForgotPassword,
594
+ children: intl.formatMessage({
595
+ id: "BtK6KR",
596
+ defaultMessage: [{
597
+ "type": 0,
598
+ "value": "Forgot password?"
599
+ }]
600
+ })
601
+ })
602
+ }), /* @__PURE__ */jsx(NotificationsBox, {})]
603
+ })
604
+ });
605
+ };
606
+
607
+ //#endregion
608
+ //#region src/AuthSignUp.tsx
609
+ const AuthSignUp = props => {
610
+ const {
611
+ intl
612
+ } = useI18n();
613
+ const {
614
+ passwordMinimumLength = 8
615
+ } = props;
616
+ const schema = React.useMemo(() => {
617
+ return yup.object().shape({
618
+ email: yup.string().required(intl.formatMessage({
619
+ id: "d1YCuH",
620
+ defaultMessage: [{
621
+ "type": 0,
622
+ "value": "Enter your email address"
623
+ }]
624
+ })).email(intl.formatMessage({
625
+ id: "OhDL0i",
626
+ defaultMessage: [{
627
+ "type": 0,
628
+ "value": "Invalid email"
629
+ }]
630
+ })),
631
+ password: yup.string().required(intl.formatMessage({
632
+ id: "kdFYba",
633
+ defaultMessage: [{
634
+ "type": 0,
635
+ "value": "Password field is required"
636
+ }]
637
+ })).min(passwordMinimumLength, intl.formatMessage({
638
+ id: "TZ4WUk",
639
+ defaultMessage: [{
640
+ "type": 0,
641
+ "value": "Password requires "
642
+ }, {
643
+ "type": 1,
644
+ "value": "value"
645
+ }, {
646
+ "type": 0,
647
+ "value": " characters"
648
+ }]
649
+ }, {
650
+ value: passwordMinimumLength
651
+ })).trim(),
652
+ confirmPassword: yup.string().required(intl.formatMessage({
653
+ id: "NJ57Qj",
654
+ defaultMessage: [{
655
+ "type": 0,
656
+ "value": "Confirm password field is required"
657
+ }]
658
+ })).oneOf([yup.ref("password")], intl.formatMessage({
659
+ id: "WU/CqP",
660
+ defaultMessage: [{
661
+ "type": 0,
662
+ "value": "Passwords are not the same"
663
+ }]
664
+ })),
665
+ signUpTerms: props.signUpTerms?.isRequired ? yup.boolean().required(intl.formatMessage({
666
+ id: "qnQYqN",
667
+ defaultMessage: [{
668
+ "type": 0,
669
+ "value": "You must accept the terms and conditions"
670
+ }]
671
+ })) : yup.boolean()
672
+ });
673
+ }, [intl, props.signUpTerms?.isRequired, passwordMinimumLength]);
674
+ const signUpTermsLabel = React.useMemo(() => {
675
+ if (!props.signUpTerms) return null;
676
+ return /* @__PURE__ */jsxs(Text, {
677
+ children: [intl.formatMessage({
678
+ id: "Co3exe",
679
+ defaultMessage: [{
680
+ "type": 0,
681
+ "value": "By signing up, you agree to the following Terms and Conditions."
682
+ }]
683
+ }), " ", props.signUpTerms.terms.map((term, index, terms) => {
684
+ const finalPunctuation = index === terms.length - 1 ? "." : ", ";
685
+ return /* @__PURE__ */jsxs(React.Fragment, {
686
+ children: [/* @__PURE__ */jsx(Link, {
687
+ href: term.url,
688
+ target: "_blank",
689
+ rel: "noreferrer",
690
+ children: term.label
691
+ }, index), finalPunctuation]
692
+ }, index);
693
+ })]
694
+ });
695
+ }, [intl, props.signUpTerms]);
696
+ const signUpTermsNode = React.useMemo(() => {
697
+ if (!props.signUpTerms) return null;
698
+ if (props.signUpTerms.isRequired) return /* @__PURE__ */jsx(FormFieldCheckbox, {
699
+ name: "signUpTerms",
700
+ label: signUpTermsLabel
701
+ });else return /* @__PURE__ */jsx(Fragment, {
702
+ children: signUpTermsLabel
703
+ });
704
+ }, [props.signUpTerms, signUpTermsLabel]);
705
+ const formMethods = useForm({
706
+ mode: "onChange",
707
+ resolver: yupResolver(schema)
708
+ });
709
+ const onSubmitForm = data => {
710
+ return props.onSignUp(data);
711
+ };
712
+ return /* @__PURE__ */jsx(Form, {
713
+ sx: {
714
+ maxWidth: "390px",
715
+ width: "100%"
716
+ },
717
+ ...formMethods,
718
+ onSubmit: onSubmitForm,
719
+ children: /* @__PURE__ */jsxs(AuthCard, {
720
+ buttonLabel: intl.formatMessage({
721
+ id: "URJDrG",
722
+ defaultMessage: [{
723
+ "type": 0,
724
+ "value": "Sign up"
725
+ }]
726
+ }),
727
+ title: intl.formatMessage({
728
+ id: "oayEC3",
729
+ defaultMessage: [{
730
+ "type": 0,
731
+ "value": "Sign up"
732
+ }]
733
+ }),
734
+ isValidForm: formMethods.formState.isValid,
735
+ extraButton: /* @__PURE__ */jsx(Text, {
736
+ sx: {
737
+ cursor: "pointer"
738
+ },
739
+ onClick: props.onGoToSignIn,
740
+ as: Link,
741
+ children: intl.formatMessage({
742
+ id: "8GMUPk",
743
+ defaultMessage: [{
744
+ "type": 0,
745
+ "value": "I'm already registered"
746
+ }]
747
+ })
748
+ }),
749
+ children: [/* @__PURE__ */jsxs(Flex, {
750
+ sx: {
751
+ flexDirection: "column",
752
+ gap: "xl"
753
+ },
754
+ children: [/* @__PURE__ */jsx(FormFieldInput, {
755
+ name: "email",
756
+ label: intl.formatMessage({
757
+ id: "5E12mO",
758
+ defaultMessage: [{
759
+ "type": 0,
760
+ "value": "Email"
761
+ }]
762
+ })
763
+ }), /* @__PURE__ */jsx(FormFieldPassword, {
764
+ name: "password",
765
+ label: intl.formatMessage({
766
+ id: "PylVqx",
767
+ defaultMessage: [{
768
+ "type": 0,
769
+ "value": "Password"
770
+ }]
771
+ })
772
+ }), /* @__PURE__ */jsx(FormFieldPassword, {
773
+ name: "confirmPassword",
774
+ label: intl.formatMessage({
775
+ id: "lY+cuM",
776
+ defaultMessage: [{
777
+ "type": 0,
778
+ "value": "Confirm password"
779
+ }]
780
+ })
781
+ }), signUpTermsNode]
782
+ }), /* @__PURE__ */jsx(NotificationsBox, {})]
783
+ })
784
+ });
785
+ };
786
+
787
+ //#endregion
788
+ //#region src/ErrorBoundary.tsx
789
+ const ErrorFallback = ({
790
+ resetErrorBoundary
791
+ }) => {
792
+ const {
793
+ intl
794
+ } = useI18n();
795
+ return /* @__PURE__ */jsx("div", {
796
+ role: "alert",
797
+ children: /* @__PURE__ */jsx(NotificationCard, {
798
+ type: "error",
799
+ message: intl.formatMessage({
800
+ id: "khMx/2",
801
+ defaultMessage: [{
802
+ "type": 0,
803
+ "value": "An error occurred with your authentication. Please try again."
804
+ }]
805
+ }),
806
+ onClose: resetErrorBoundary
807
+ })
808
+ });
809
+ };
810
+ const ErrorBoundary = ({
811
+ children
812
+ }) => {
813
+ return /* @__PURE__ */jsx(ErrorBoundary$1, {
814
+ FallbackComponent: ErrorFallback,
815
+ onError: error => {
816
+ notify({
817
+ type: "error",
818
+ title: "Authentication Error",
819
+ message: error instanceof Error ? error.message : String(error)
820
+ });
821
+ },
822
+ children
823
+ });
824
+ };
825
+
826
+ //#endregion
827
+ //#region src/useAuthScreen.ts
828
+ const useAuthScreen = initialScreen => {
829
+ const [screen, setScreen] = React.useState(initialScreen || {
830
+ value: "signIn"
831
+ });
832
+ return {
833
+ screen,
834
+ setScreen
835
+ };
836
+ };
837
+
838
+ //#endregion
839
+ //#region src/Auth.tsx
840
+ const AuthLogic = props => {
841
+ const {
842
+ screen,
843
+ setScreen,
844
+ onSignUp,
845
+ onForgotPassword,
846
+ onSignIn,
847
+ onConfirmSignUpCheckEmail,
848
+ onConfirmSignUpWithCode,
849
+ onForgotPasswordResetPassword,
850
+ passwordMinimumLength,
851
+ signUpTerms,
852
+ maxForgotPasswordCodeLength
853
+ } = props;
854
+ const {
855
+ clearNotifications,
856
+ setLoading
857
+ } = useNotifications();
858
+ /**
859
+ * Clear notifications when the state changes
860
+ */
861
+ React.useEffect(() => {
862
+ clearNotifications();
863
+ }, [screen.value, clearNotifications]);
864
+ /**
865
+ * Clear notifications when the component unmounts
866
+ */
867
+ React.useEffect(() => {
868
+ return clearNotifications;
869
+ }, [clearNotifications]);
870
+ const onGoToSignIn = () => {
871
+ setScreen({
872
+ value: "signIn"
873
+ });
874
+ };
875
+ const onGoToSignUp = React.useCallback(() => {
876
+ if (!onSignUp) return;
877
+ setScreen({
878
+ value: "signUp"
879
+ });
880
+ }, [onSignUp, setScreen]);
881
+ const onGoToForgotPassword = React.useCallback(() => {
882
+ if (!onForgotPassword) return;
883
+ setScreen({
884
+ value: "forgotPassword"
885
+ });
886
+ }, [onForgotPassword, setScreen]);
887
+ const notificationsWrapper = fn => {
888
+ return async input => {
889
+ if (fn) {
890
+ clearNotifications();
891
+ setLoading(true);
892
+ const result = await fn(input);
893
+ setLoading(false);
894
+ return result;
895
+ }
896
+ };
897
+ };
898
+ if (screen.value === "signIn") return /* @__PURE__ */jsx(AuthSignIn, {
899
+ onSignIn: notificationsWrapper(onSignIn),
900
+ onGoToSignUp: onSignUp && onGoToSignUp,
901
+ onGoToForgotPassword: onForgotPassword && onGoToForgotPassword,
902
+ passwordMinimumLength
903
+ });
904
+ if (screen.value === "signUp" && onSignUp) return /* @__PURE__ */jsx(AuthSignUp, {
905
+ onSignUp: notificationsWrapper(onSignUp),
906
+ passwordMinimumLength,
907
+ onGoToSignIn,
908
+ signUpTerms
909
+ });
910
+ if (screen.value === "forgotPassword" && onForgotPassword) return /* @__PURE__ */jsx(AuthForgotPassword, {
911
+ onForgotPassword: notificationsWrapper(onForgotPassword),
912
+ onGoToSignIn,
913
+ onGoToSignUp: onSignUp && onGoToSignUp
914
+ });
915
+ if (screen.value === "confirmResetPassword" && onForgotPasswordResetPassword) return /* @__PURE__ */jsx(AuthForgotPasswordResetPassword, {
916
+ onForgotPasswordResetPassword: notificationsWrapper(onForgotPasswordResetPassword),
917
+ onGoToSignIn,
918
+ email: screen.context.email,
919
+ maxCodeLength: maxForgotPasswordCodeLength
920
+ });
921
+ if (screen.value === "confirmSignUpWithCode" && onConfirmSignUpWithCode) return /* @__PURE__ */jsx(AuthConfirmSignUpWithCode, {
922
+ onConfirmSignUpWithCode: notificationsWrapper(onConfirmSignUpWithCode),
923
+ email: screen.context.email
924
+ });
925
+ if (screen.value === "confirmSignUpCheckEmail" && onConfirmSignUpCheckEmail) return /* @__PURE__ */jsx(AuthConfirmSignUpCheckEmail, {
926
+ onConfirmSignUpCheckEmail
927
+ });
928
+ return null;
929
+ };
930
+ const Auth = props => {
931
+ const {
932
+ layout = {
933
+ fullScreen: true
934
+ }
935
+ } = props;
936
+ const authScreenHook = useAuthScreen(props.initialScreen);
937
+ const screen = props.screen ?? authScreenHook.screen;
938
+ const setScreen = props.setScreen ?? authScreenHook.setScreen;
939
+ const withLogoNode = React.useMemo(() => {
940
+ return /* @__PURE__ */jsx(LogoProvider, {
941
+ logo: props.logo,
942
+ children: /* @__PURE__ */jsx(ErrorBoundary, {
943
+ children: /* @__PURE__ */jsx(AuthLogic, {
944
+ screen,
945
+ setScreen,
946
+ signUpTerms: props.signUpTerms,
947
+ passwordMinimumLength: props.passwordMinimumLength,
948
+ maxForgotPasswordCodeLength: props.maxForgotPasswordCodeLength,
949
+ onSignIn: props.onSignIn,
950
+ onSignUp: props.onSignUp,
951
+ onConfirmSignUpCheckEmail: props.onConfirmSignUpCheckEmail,
952
+ onConfirmSignUpWithCode: props.onConfirmSignUpWithCode,
953
+ onForgotPassword: props.onForgotPassword,
954
+ onForgotPasswordResetPassword: props.onForgotPasswordResetPassword
955
+ })
956
+ })
957
+ });
958
+ }, [props, screen, setScreen]);
959
+ if (layout.fullScreen) {
960
+ if (layout.sideContentPosition) return /* @__PURE__ */jsx(AuthFullScreen, {
961
+ children: /* @__PURE__ */jsxs(Flex, {
962
+ sx: {
963
+ width: "100%",
964
+ height: "100%",
965
+ flexDirection: layout.sideContentPosition === "left" ? "row" : "row-reverse"
966
+ },
967
+ children: [/* @__PURE__ */jsx(Flex, {
968
+ sx: {
969
+ width: "100%",
970
+ height: "100%",
971
+ flex: [0, 0, 1],
972
+ justifyContent: "center",
973
+ alignItems: "center"
974
+ },
975
+ children: layout.sideContent
976
+ }), /* @__PURE__ */jsx(Flex, {
977
+ sx: {
978
+ width: "100%",
979
+ height: "100%",
980
+ flex: [1],
981
+ justifyContent: "center",
982
+ alignItems: "center"
983
+ },
984
+ children: withLogoNode
985
+ })]
986
+ })
987
+ });
988
+ return /* @__PURE__ */jsx(AuthFullScreen, {
989
+ children: withLogoNode
990
+ });
991
+ }
992
+ return withLogoNode;
993
+ };
994
+
995
+ //#endregion
996
+ //#region src/AuthProvider.tsx
997
+ const AuthContext = React.createContext(null);
998
+ const UNAUTHENTICATED_USER = {
999
+ user: null,
1000
+ tokens: null,
1001
+ isAuthenticated: false
1002
+ };
1003
+ const AuthProvider = props => {
1004
+ const {
1005
+ getAuthData,
1006
+ signOut: signOutProp
1007
+ } = props;
1008
+ const {
1009
+ setLoading
1010
+ } = useNotifications();
1011
+ const [authData, setAuthData] = React.useState({
1012
+ user: null,
1013
+ tokens: null,
1014
+ isAuthenticated: void 0
1015
+ });
1016
+ /**
1017
+ * Update the loading state based on authentication status.
1018
+ */
1019
+ React.useEffect(() => {
1020
+ setLoading(authData.isAuthenticated === void 0);
1021
+ }, [authData.isAuthenticated, setLoading]);
1022
+ /**
1023
+ * Fetch the authentication data when the component mounts.
1024
+ */
1025
+ React.useEffect(() => {
1026
+ const fetchAuthData = async () => {
1027
+ try {
1028
+ const data = await getAuthData?.();
1029
+ if (data) setAuthData(data);else setAuthData(UNAUTHENTICATED_USER);
1030
+ } catch {
1031
+ setAuthData(UNAUTHENTICATED_USER);
1032
+ }
1033
+ };
1034
+ fetchAuthData();
1035
+ }, [getAuthData]);
1036
+ const signOut = React.useCallback(async () => {
1037
+ await signOutProp?.();
1038
+ setAuthData(UNAUTHENTICATED_USER);
1039
+ }, [signOutProp]);
1040
+ if (authData.isAuthenticated === void 0) return null;
1041
+ return /* @__PURE__ */jsx(AuthContext.Provider, {
1042
+ value: {
1043
+ signOut,
1044
+ isAuthenticated: authData.isAuthenticated ?? false,
1045
+ user: authData.user,
1046
+ tokens: authData.tokens,
1047
+ setAuthData
1048
+ },
1049
+ children: props.children
1050
+ });
1051
+ };
1052
+ const useAuth = () => {
1053
+ const context = React.useContext(AuthContext);
1054
+ if (!context) throw new Error("useAuth must be used within an AuthProvider");
1055
+ return context;
1056
+ };
1057
+
1058
+ //#endregion
1059
+ export { Auth, AuthConfirmSignUpCheckEmail, AuthConfirmSignUpWithCode, AuthForgotPassword, AuthForgotPasswordResetPassword, AuthFullScreen, AuthProvider, AuthSignIn, AuthSignUp, ErrorBoundary, useAuth, useAuthScreen };