@ttoss/react-auth 1.7.5 → 1.7.6

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 DELETED
@@ -1,1389 +0,0 @@
1
- /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
- "use strict";
3
-
4
- var __create = Object.create;
5
- var __defProp = Object.defineProperty;
6
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
- var __getOwnPropNames = Object.getOwnPropertyNames;
8
- var __getProtoOf = Object.getPrototypeOf;
9
- var __hasOwnProp = Object.prototype.hasOwnProperty;
10
- var __export = (target, all) => {
11
- for (var name in all) __defProp(target, name, {
12
- get: all[name],
13
- enumerable: true
14
- });
15
- };
16
- var __copyProps = (to, from, except, desc) => {
17
- if (from && typeof from === "object" || typeof from === "function") {
18
- for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
19
- get: () => from[key],
20
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
21
- });
22
- }
23
- return to;
24
- };
25
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
26
- // If the importer is in node compatibility mode or this is not an ESM
27
- // file that has been converted to a CommonJS file using a Babel-
28
- // compatible transform (i.e. "__esModule" has not been set), then set
29
- // "default" to the CommonJS "module.exports" for node compatibility.
30
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
31
- value: mod,
32
- enumerable: true
33
- }) : target, mod));
34
- var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
35
- value: true
36
- }), mod);
37
-
38
- // src/index.ts
39
- var src_exports = {};
40
- __export(src_exports, {
41
- Auth: () => Auth2,
42
- AuthProvider: () => AuthProvider,
43
- useAuth: () => useAuth
44
- });
45
- module.exports = __toCommonJS(src_exports);
46
-
47
- // src/AuthProvider.tsx
48
- var React = __toESM(require("react"));
49
- var import_aws_amplify = require("aws-amplify");
50
- var import_jsx_runtime = require("react/jsx-runtime");
51
- var signOut = () => {
52
- return import_aws_amplify.Auth.signOut();
53
- };
54
- var AuthContext = React.createContext({
55
- signOut,
56
- isAuthenticated: false,
57
- user: null,
58
- tokens: null
59
- });
60
- var AuthProvider = ({
61
- children
62
- }) => {
63
- const [{
64
- user,
65
- tokens,
66
- isAuthenticated
67
- }, setAuthState] = React.useState({
68
- user: null,
69
- tokens: null,
70
- isAuthenticated: void 0
71
- });
72
- React.useEffect(() => {
73
- const updateUser = () => {
74
- import_aws_amplify.Auth.currentAuthenticatedUser().then(({
75
- attributes,
76
- signInUserSession
77
- }) => {
78
- setAuthState({
79
- user: {
80
- id: attributes.sub,
81
- email: attributes.email,
82
- emailVerified: attributes["email_verified"]
83
- },
84
- tokens: {
85
- idToken: signInUserSession.idToken.jwtToken,
86
- accessToken: signInUserSession.accessToken.jwtToken,
87
- refreshToken: signInUserSession.refreshToken.token
88
- },
89
- isAuthenticated: true
90
- });
91
- }).catch(() => {
92
- setAuthState({
93
- user: null,
94
- tokens: null,
95
- isAuthenticated: false
96
- });
97
- });
98
- };
99
- const updateUserListener = import_aws_amplify.Hub.listen("auth", updateUser);
100
- updateUser();
101
- return () => {
102
- updateUserListener();
103
- };
104
- }, []);
105
- if (isAuthenticated === void 0) {
106
- return null;
107
- }
108
- return /* @__PURE__ */(0, import_jsx_runtime.jsx)(AuthContext.Provider, {
109
- value: {
110
- signOut,
111
- isAuthenticated,
112
- user,
113
- tokens
114
- },
115
- children
116
- });
117
- };
118
- var useAuth = () => {
119
- return React.useContext(AuthContext);
120
- };
121
-
122
- // src/Auth.tsx
123
- var React6 = __toESM(require("react"));
124
- var import_aws_amplify2 = require("aws-amplify");
125
-
126
- // src/AuthCard.tsx
127
- var React2 = __toESM(require("react"));
128
- var import_ui = require("@ttoss/ui");
129
- var import_react_notifications = require("@ttoss/react-notifications");
130
- var import_jsx_runtime2 = require("react/jsx-runtime");
131
- var LogoContext = React2.createContext({});
132
- var LogoProvider = ({
133
- children,
134
- ...values
135
- }) => {
136
- return /* @__PURE__ */(0, import_jsx_runtime2.jsx)(LogoContext.Provider, {
137
- value: values,
138
- children
139
- });
140
- };
141
- var AuthCard = ({
142
- children,
143
- title,
144
- buttonLabel,
145
- extraButton,
146
- isValidForm
147
- }) => {
148
- const {
149
- logo
150
- } = React2.useContext(LogoContext);
151
- const {
152
- isLoading
153
- } = (0, import_react_notifications.useNotifications)();
154
- return /* @__PURE__ */(0, import_jsx_runtime2.jsxs)(import_ui.Box, {
155
- sx: {
156
- width: "100%",
157
- border: "default",
158
- borderColor: "primary",
159
- paddingX: "2xl",
160
- paddingY: "3xl",
161
- backgroundColor: "surface"
162
- },
163
- children: [logo && /* @__PURE__ */(0, import_jsx_runtime2.jsx)(import_ui.Flex, {
164
- sx: {
165
- width: "100%",
166
- maxHeight: "90px",
167
- justifyContent: "center",
168
- marginBottom: "2xl"
169
- },
170
- children: logo
171
- }), /* @__PURE__ */(0, import_jsx_runtime2.jsxs)(import_ui.Flex, {
172
- sx: {
173
- flexDirection: "column"
174
- },
175
- children: [/* @__PURE__ */(0, import_jsx_runtime2.jsx)(import_ui.Heading, {
176
- as: "h2",
177
- variant: "h2",
178
- sx: {
179
- marginBottom: "2xl"
180
- },
181
- children: title
182
- }), children, /* @__PURE__ */(0, import_jsx_runtime2.jsxs)(import_ui.Flex, {
183
- sx: {
184
- flexDirection: "column",
185
- width: "100%",
186
- gap: "xl",
187
- marginTop: "2xl"
188
- },
189
- children: [/* @__PURE__ */(0, import_jsx_runtime2.jsx)(import_ui.Button, {
190
- type: "submit",
191
- "aria-label": "submit-button",
192
- variant: "accent",
193
- disabled: isLoading || !isValidForm,
194
- sx: {
195
- textAlign: "center",
196
- display: "initial"
197
- },
198
- loading: isLoading,
199
- children: buttonLabel
200
- }), extraButton]
201
- })]
202
- })]
203
- });
204
- };
205
-
206
- // src/AuthConfirmSignUp.tsx
207
- var import_forms = require("@ttoss/forms");
208
- var import_react_i18n = require("@ttoss/react-i18n");
209
- var import_jsx_runtime3 = require("react/jsx-runtime");
210
- var AuthConfirmSignUp = ({
211
- email,
212
- onConfirmSignUp
213
- }) => {
214
- const {
215
- intl
216
- } = (0, import_react_i18n.useI18n)();
217
- const schema = import_forms.yup.object().shape({
218
- code: import_forms.yup.string().required(intl.formatMessage({
219
- id: "0XOzcH",
220
- defaultMessage: [{
221
- "type": 0,
222
- "value": "Required field"
223
- }]
224
- })).max(6, intl.formatMessage({
225
- id: "S3pjKw",
226
- defaultMessage: [{
227
- "type": 0,
228
- "value": "Minimum "
229
- }, {
230
- "type": 1,
231
- "value": "value"
232
- }, {
233
- "type": 0,
234
- "value": " characters"
235
- }]
236
- }, {
237
- value: 6
238
- }))
239
- }).required();
240
- const formMethods = (0, import_forms.useForm)({
241
- resolver: (0, import_forms.yupResolver)(schema)
242
- });
243
- return /* @__PURE__ */(0, import_jsx_runtime3.jsx)(import_forms.Form, {
244
- ...formMethods,
245
- onSubmit: ({
246
- code
247
- }) => {
248
- return onConfirmSignUp({
249
- code,
250
- email
251
- });
252
- },
253
- children: /* @__PURE__ */(0, import_jsx_runtime3.jsx)(AuthCard, {
254
- buttonLabel: intl.formatMessage({
255
- id: "UNttd+",
256
- defaultMessage: [{
257
- "type": 0,
258
- "value": "Confirm"
259
- }]
260
- }),
261
- isValidForm: formMethods.formState.isValid,
262
- title: intl.formatMessage({
263
- id: "cGR2eI",
264
- defaultMessage: [{
265
- "type": 0,
266
- "value": "Confirmation"
267
- }]
268
- }),
269
- children: /* @__PURE__ */(0, import_jsx_runtime3.jsx)(import_forms.FormFieldInput, {
270
- name: "code",
271
- label: intl.formatMessage({
272
- id: "KY2T6J",
273
- defaultMessage: [{
274
- "type": 0,
275
- "value": "Code"
276
- }]
277
- })
278
- })
279
- })
280
- });
281
- };
282
-
283
- // src/AuthForgotPassword.tsx
284
- var import_ui2 = require("@ttoss/ui");
285
- var import_forms2 = require("@ttoss/forms");
286
- var import_react_notifications2 = require("@ttoss/react-notifications");
287
- var import_react_i18n2 = require("@ttoss/react-i18n");
288
- var import_jsx_runtime4 = require("react/jsx-runtime");
289
- var AuthForgotPassword = ({
290
- onForgotPassword,
291
- onCancel,
292
- onSignUp
293
- }) => {
294
- const {
295
- intl
296
- } = (0, import_react_i18n2.useI18n)();
297
- const schema = import_forms2.yup.object().shape({
298
- email: import_forms2.yup.string().required(intl.formatMessage({
299
- id: "5oOshZ",
300
- defaultMessage: [{
301
- "type": 0,
302
- "value": "Enter your email address"
303
- }]
304
- })).email(intl.formatMessage({
305
- id: "SnONhb",
306
- defaultMessage: [{
307
- "type": 0,
308
- "value": "Please, insert a valid e-mail"
309
- }]
310
- }))
311
- }).required();
312
- const formMethods = (0, import_forms2.useForm)({
313
- resolver: (0, import_forms2.yupResolver)(schema),
314
- mode: "onBlur"
315
- });
316
- return /* @__PURE__ */(0, import_jsx_runtime4.jsx)(import_forms2.Form, {
317
- ...formMethods,
318
- sx: {
319
- maxWidth: "390px"
320
- },
321
- onSubmit: ({
322
- email
323
- }) => {
324
- return onForgotPassword({
325
- email
326
- });
327
- },
328
- children: /* @__PURE__ */(0, import_jsx_runtime4.jsxs)(AuthCard, {
329
- buttonLabel: intl.formatMessage({
330
- id: "mZzmNV",
331
- defaultMessage: [{
332
- "type": 0,
333
- "value": "Recover Password"
334
- }]
335
- }),
336
- isValidForm: formMethods.formState.isValid,
337
- title: intl.formatMessage({
338
- id: "S4bbEj",
339
- defaultMessage: [{
340
- "type": 0,
341
- "value": "Recovering Password"
342
- }]
343
- }),
344
- extraButton: /* @__PURE__ */(0, import_jsx_runtime4.jsx)(import_ui2.Button, {
345
- sx: {
346
- textAlign: "center",
347
- display: "initial"
348
- },
349
- variant: "secondary",
350
- onClick: onCancel,
351
- children: intl.formatMessage({
352
- id: "6PdOcy",
353
- defaultMessage: [{
354
- "type": 0,
355
- "value": "Cancel"
356
- }]
357
- })
358
- }),
359
- children: [/* @__PURE__ */(0, import_jsx_runtime4.jsx)(import_forms2.FormFieldInput, {
360
- name: "email",
361
- label: intl.formatMessage({
362
- id: "XreZg+",
363
- defaultMessage: [{
364
- "type": 0,
365
- "value": "Registered Email"
366
- }]
367
- })
368
- }), /* @__PURE__ */(0, import_jsx_runtime4.jsx)(import_react_notifications2.NotificationsBox, {}), /* @__PURE__ */(0, import_jsx_runtime4.jsx)(import_ui2.Text, {
369
- sx: {
370
- marginTop: "xl",
371
- cursor: "pointer"
372
- },
373
- as: import_ui2.Link,
374
- onClick: onSignUp,
375
- children: intl.formatMessage({
376
- id: "lZvoYL",
377
- defaultMessage: [{
378
- "type": 0,
379
- "value": "Sign up now"
380
- }]
381
- })
382
- })]
383
- })
384
- });
385
- };
386
-
387
- // src/AuthForgotPasswordResetPassword.tsx
388
- var React3 = __toESM(require("react"));
389
- var import_ui3 = require("@ttoss/ui");
390
- var import_forms3 = require("@ttoss/forms");
391
- var import_react_notifications3 = require("@ttoss/react-notifications");
392
-
393
- // ../cloud-auth/dist/esm/index.js
394
- var PASSWORD_MINIMUM_LENGTH = 8;
395
- var CognitoUserPoolLogicalId = "CognitoUserPool";
396
- var CognitoUserPoolClientLogicalId = "CognitoUserPoolClient";
397
- var CognitoIdentityPoolLogicalId = "CognitoIdentityPool";
398
- var IdentityPoolAuthenticatedIAMRoleLogicalId = "IdentityPoolAuthenticatedIAMRole";
399
- var IdentityPoolUnauthenticatedIAMRoleLogicalId = "IdentityPoolUnauthenticatedIAMRole";
400
- var DenyStatement = {
401
- Effect: "Deny",
402
- Action: ["*"],
403
- Resource: ["*"]
404
- };
405
- var createAuthTemplate = ({
406
- autoVerifiedAttributes = ["email"],
407
- identityPool,
408
- schema,
409
- usernameAttributes = ["email"]
410
- } = {}) => {
411
- const AutoVerifiedAttributes = Array.isArray(autoVerifiedAttributes) && autoVerifiedAttributes.length > 0 ? autoVerifiedAttributes : [];
412
- const template = {
413
- AWSTemplateFormatVersion: "2010-09-09",
414
- Resources: {
415
- [CognitoUserPoolLogicalId]: {
416
- Type: "AWS::Cognito::UserPool",
417
- Properties: {
418
- AutoVerifiedAttributes,
419
- Policies: {
420
- PasswordPolicy: {
421
- MinimumLength: PASSWORD_MINIMUM_LENGTH,
422
- RequireLowercase: false,
423
- RequireNumbers: false,
424
- RequireSymbols: false,
425
- RequireUppercase: false,
426
- TemporaryPasswordValidityDays: 30
427
- }
428
- },
429
- UsernameAttributes: usernameAttributes,
430
- UsernameConfiguration: {
431
- CaseSensitive: false
432
- },
433
- UserPoolName: {
434
- Ref: "AWS::StackName"
435
- }
436
- }
437
- },
438
- [CognitoUserPoolClientLogicalId]: {
439
- Type: "AWS::Cognito::UserPoolClient",
440
- Properties: {
441
- SupportedIdentityProviders: ["COGNITO"],
442
- UserPoolId: {
443
- Ref: "CognitoUserPool"
444
- }
445
- }
446
- }
447
- },
448
- Outputs: {
449
- Region: {
450
- Description: "You use this value on Amplify Auth `region`.",
451
- Value: {
452
- Ref: "AWS::Region"
453
- },
454
- Export: {
455
- Name: {
456
- "Fn::Join": [":", [{
457
- Ref: "AWS::StackName"
458
- }, "Region"]]
459
- }
460
- }
461
- },
462
- UserPoolId: {
463
- Description: "You use this value on Amplify Auth `userPoolId`.",
464
- Value: {
465
- Ref: CognitoUserPoolLogicalId
466
- },
467
- Export: {
468
- Name: {
469
- "Fn::Join": [":", [{
470
- Ref: "AWS::StackName"
471
- }, "UserPoolId"]]
472
- }
473
- }
474
- },
475
- AppClientId: {
476
- Description: "You use this value on Amplify Auth `userPoolWebClientId`.",
477
- Value: {
478
- Ref: CognitoUserPoolClientLogicalId
479
- },
480
- Export: {
481
- Name: {
482
- "Fn::Join": [":", [{
483
- Ref: "AWS::StackName"
484
- }, "AppClientId"]]
485
- }
486
- }
487
- }
488
- }
489
- };
490
- if (schema) {
491
- const Schema = schema.map(attribute => {
492
- let NumberAttributeConstraints = void 0;
493
- if (attribute.numberAttributeConstraints) {
494
- NumberAttributeConstraints = {
495
- MaxValue: attribute.numberAttributeConstraints?.maxValue,
496
- MinValue: attribute.numberAttributeConstraints?.minValue
497
- };
498
- }
499
- let StringAttributeConstraints = void 0;
500
- if (attribute.stringAttributeConstraints) {
501
- StringAttributeConstraints = {
502
- MaxLength: attribute.stringAttributeConstraints?.maxLength,
503
- MinLength: attribute.stringAttributeConstraints?.minLength
504
- };
505
- }
506
- return {
507
- AttributeDataType: attribute.attributeDataType,
508
- DeveloperOnlyAttribute: attribute.developerOnlyAttribute,
509
- Mutable: attribute.mutable,
510
- Name: attribute.name,
511
- NumberAttributeConstraints,
512
- Required: attribute.required,
513
- StringAttributeConstraints
514
- };
515
- });
516
- template.Resources[CognitoUserPoolLogicalId].Properties.Schema = Schema;
517
- }
518
- if (identityPool?.enabled) {
519
- template.Resources[CognitoIdentityPoolLogicalId] = {
520
- Type: "AWS::Cognito::IdentityPool",
521
- Properties: {
522
- AllowUnauthenticatedIdentities: true,
523
- CognitoIdentityProviders: [{
524
- ClientId: {
525
- Ref: CognitoUserPoolClientLogicalId
526
- },
527
- ProviderName: {
528
- "Fn::GetAtt": [CognitoUserPoolLogicalId, "ProviderName"]
529
- }
530
- }]
531
- }
532
- };
533
- template.Resources[IdentityPoolAuthenticatedIAMRoleLogicalId] = {
534
- Type: "AWS::IAM::Role",
535
- Properties: {
536
- AssumeRolePolicyDocument: {
537
- Version: "2012-10-17",
538
- Statement: [{
539
- Effect: "Allow",
540
- Principal: {
541
- Federated: "cognito-identity.amazonaws.com"
542
- },
543
- Action: ["sts:AssumeRoleWithWebIdentity", "sts:TagSession"],
544
- Condition: {
545
- StringEquals: {
546
- "cognito-identity.amazonaws.com:aud": {
547
- Ref: CognitoIdentityPoolLogicalId
548
- }
549
- },
550
- "ForAnyValue:StringLike": {
551
- "cognito-identity.amazonaws.com:amr": "authenticated"
552
- }
553
- }
554
- }]
555
- },
556
- Policies: identityPool.authenticatedPolicies || [{
557
- PolicyName: "IdentityPoolAuthenticatedIAMRolePolicyName",
558
- PolicyDocument: {
559
- Version: "2012-10-17",
560
- Statement: [DenyStatement]
561
- }
562
- }]
563
- }
564
- };
565
- template.Resources[IdentityPoolUnauthenticatedIAMRoleLogicalId] = {
566
- Type: "AWS::IAM::Role",
567
- Properties: {
568
- AssumeRolePolicyDocument: {
569
- Version: "2012-10-17",
570
- Statement: [{
571
- Effect: "Allow",
572
- Principal: {
573
- Federated: "cognito-identity.amazonaws.com"
574
- },
575
- Action: "sts:AssumeRoleWithWebIdentity",
576
- Condition: {
577
- StringEquals: {
578
- "cognito-identity.amazonaws.com:aud": {
579
- Ref: CognitoIdentityPoolLogicalId
580
- }
581
- },
582
- "ForAnyValue:StringLike": {
583
- "cognito-identity.amazonaws.com:amr": "unauthenticated"
584
- }
585
- }
586
- }]
587
- },
588
- Policies: identityPool.authenticatedPolicies || [{
589
- PolicyName: "IdentityPoolUnauthenticatedIAMRolePolicyName",
590
- PolicyDocument: {
591
- Version: "2012-10-17",
592
- Statement: [DenyStatement]
593
- }
594
- }]
595
- }
596
- };
597
- template.Resources.CognitoIdentityPoolRoleAttachment = {
598
- Type: "AWS::Cognito::IdentityPoolRoleAttachment",
599
- Properties: {
600
- IdentityPoolId: {
601
- Ref: CognitoIdentityPoolLogicalId
602
- },
603
- Roles: {
604
- authenticated: {
605
- "Fn::GetAtt": [IdentityPoolAuthenticatedIAMRoleLogicalId, "Arn"]
606
- },
607
- unauthenticated: {
608
- "Fn::GetAtt": [IdentityPoolUnauthenticatedIAMRoleLogicalId, "Arn"]
609
- }
610
- }
611
- }
612
- };
613
- if (!template.Outputs) {
614
- template.Outputs = {};
615
- }
616
- template.Outputs.IdentityPoolId = {
617
- Description: "You use this value on Amplify Auth `identityPoolId`.",
618
- Value: {
619
- Ref: CognitoIdentityPoolLogicalId
620
- },
621
- Export: {
622
- Name: {
623
- "Fn::Join": [":", [{
624
- Ref: "AWS::StackName"
625
- }, "CognitoIdentityPoolId"]]
626
- }
627
- }
628
- };
629
- }
630
- return template;
631
- };
632
- createAuthTemplate.CognitoUserPoolLogicalId = CognitoUserPoolLogicalId;
633
- createAuthTemplate.CognitoUserPoolClientLogicalId = CognitoUserPoolClientLogicalId;
634
- createAuthTemplate.CognitoIdentityPoolLogicalId = CognitoIdentityPoolLogicalId;
635
-
636
- // src/AuthForgotPasswordResetPassword.tsx
637
- var import_react_i18n3 = require("@ttoss/react-i18n");
638
- var import_jsx_runtime5 = require("react/jsx-runtime");
639
- var AuthForgotPasswordResetPassword = ({
640
- email,
641
- onForgotPasswordResetPassword,
642
- onCancel
643
- }) => {
644
- const {
645
- intl
646
- } = (0, import_react_i18n3.useI18n)();
647
- const schema = React3.useMemo(() => {
648
- return import_forms3.yup.object().shape({
649
- code: import_forms3.yup.string().required(intl.formatMessage({
650
- id: "0XOzcH",
651
- defaultMessage: [{
652
- "type": 0,
653
- "value": "Required field"
654
- }]
655
- })).max(6, intl.formatMessage({
656
- id: "S3pjKw",
657
- defaultMessage: [{
658
- "type": 0,
659
- "value": "Minimum "
660
- }, {
661
- "type": 1,
662
- "value": "value"
663
- }, {
664
- "type": 0,
665
- "value": " characters"
666
- }]
667
- }, {
668
- value: 6
669
- })),
670
- password: import_forms3.yup.string().required(intl.formatMessage({
671
- id: "kdFYba",
672
- defaultMessage: [{
673
- "type": 0,
674
- "value": "Password field is required"
675
- }]
676
- })).min(PASSWORD_MINIMUM_LENGTH, intl.formatMessage({
677
- id: "TZ4WUk",
678
- defaultMessage: [{
679
- "type": 0,
680
- "value": "Password requires "
681
- }, {
682
- "type": 1,
683
- "value": "value"
684
- }, {
685
- "type": 0,
686
- "value": " characters"
687
- }]
688
- }, {
689
- value: PASSWORD_MINIMUM_LENGTH
690
- })).trim(),
691
- confirmPassword: import_forms3.yup.string().required(intl.formatMessage({
692
- id: "NJ57Qj",
693
- defaultMessage: [{
694
- "type": 0,
695
- "value": "Confirm password field is required"
696
- }]
697
- })).oneOf([import_forms3.yup.ref("password")], intl.formatMessage({
698
- id: "WU/CqP",
699
- defaultMessage: [{
700
- "type": 0,
701
- "value": "Passwords are not the same"
702
- }]
703
- }))
704
- }).required();
705
- }, [intl]);
706
- const formMethods = (0, import_forms3.useForm)({
707
- resolver: (0, import_forms3.yupResolver)(schema),
708
- mode: "onBlur"
709
- });
710
- return /* @__PURE__ */(0, import_jsx_runtime5.jsx)(import_forms3.Form, {
711
- ...formMethods,
712
- sx: {
713
- maxWidth: "390px"
714
- },
715
- onSubmit: ({
716
- code,
717
- password
718
- }) => {
719
- return onForgotPasswordResetPassword({
720
- email,
721
- code,
722
- newPassword: password
723
- });
724
- },
725
- children: /* @__PURE__ */(0, import_jsx_runtime5.jsxs)(AuthCard, {
726
- buttonLabel: intl.formatMessage({
727
- id: "mZzmNV",
728
- defaultMessage: [{
729
- "type": 0,
730
- "value": "Recover Password"
731
- }]
732
- }),
733
- isValidForm: formMethods.formState.isValid,
734
- title: intl.formatMessage({
735
- id: "S4bbEj",
736
- defaultMessage: [{
737
- "type": 0,
738
- "value": "Recovering Password"
739
- }]
740
- }),
741
- extraButton: /* @__PURE__ */(0, import_jsx_runtime5.jsx)(import_ui3.Button, {
742
- sx: {
743
- textAlign: "center",
744
- display: "initial"
745
- },
746
- variant: "secondary",
747
- onClick: onCancel,
748
- children: intl.formatMessage({
749
- id: "6PdOcy",
750
- defaultMessage: [{
751
- "type": 0,
752
- "value": "Cancel"
753
- }]
754
- })
755
- }),
756
- children: [/* @__PURE__ */(0, import_jsx_runtime5.jsx)(import_forms3.FormFieldInput, {
757
- name: "code",
758
- label: intl.formatMessage({
759
- id: "42HafR",
760
- defaultMessage: [{
761
- "type": 0,
762
- "value": "Code"
763
- }]
764
- })
765
- }), /* @__PURE__ */(0, import_jsx_runtime5.jsx)(import_forms3.FormFieldPassword, {
766
- name: "password",
767
- label: intl.formatMessage({
768
- id: "PylVqx",
769
- defaultMessage: [{
770
- "type": 0,
771
- "value": "Password"
772
- }]
773
- })
774
- }), /* @__PURE__ */(0, import_jsx_runtime5.jsx)(import_forms3.FormFieldPassword, {
775
- name: "confirmPassword",
776
- label: intl.formatMessage({
777
- id: "lY+cuM",
778
- defaultMessage: [{
779
- "type": 0,
780
- "value": "Confirm password"
781
- }]
782
- })
783
- }), /* @__PURE__ */(0, import_jsx_runtime5.jsx)(import_react_notifications3.NotificationsBox, {})]
784
- })
785
- });
786
- };
787
-
788
- // src/AuthFullScreen.tsx
789
- var import_ui4 = require("@ttoss/ui");
790
- var import_jsx_runtime6 = require("react/jsx-runtime");
791
- var AuthFullScreen = ({
792
- children
793
- }) => {
794
- return /* @__PURE__ */(0, import_jsx_runtime6.jsx)(import_ui4.Flex, {
795
- sx: {
796
- height: "100vh",
797
- width: "100vw",
798
- justifyContent: "center",
799
- alignItems: "center",
800
- margin: 0,
801
- backgroundPosition: "center",
802
- backgroundRepeat: "no-repeat",
803
- backgroundSize: "cover"
804
- },
805
- children
806
- });
807
- };
808
-
809
- // src/AuthSignIn.tsx
810
- var React4 = __toESM(require("react"));
811
- var import_ui5 = require("@ttoss/ui");
812
- var import_forms4 = require("@ttoss/forms");
813
- var import_react_notifications4 = require("@ttoss/react-notifications");
814
- var import_react_i18n4 = require("@ttoss/react-i18n");
815
- var import_jsx_runtime7 = require("react/jsx-runtime");
816
- var AuthSignIn = ({
817
- onSignIn,
818
- onSignUp,
819
- defaultValues,
820
- onForgotPassword
821
- }) => {
822
- const {
823
- intl
824
- } = (0, import_react_i18n4.useI18n)();
825
- const {
826
- setNotifications
827
- } = (0, import_react_notifications4.useNotifications)();
828
- React4.useEffect(() => {
829
- setNotifications(void 0);
830
- }, [setNotifications]);
831
- const schema = import_forms4.yup.object().shape({
832
- email: import_forms4.yup.string().required(intl.formatMessage({
833
- id: "d1YCuH",
834
- defaultMessage: [{
835
- "type": 0,
836
- "value": "Enter your email address"
837
- }]
838
- })).email(intl.formatMessage({
839
- id: "9cApwd",
840
- defaultMessage: [{
841
- "type": 0,
842
- "value": "Please, insert a valid e-mail"
843
- }]
844
- })),
845
- password: import_forms4.yup.string().required(intl.formatMessage({
846
- id: "kdFYba",
847
- defaultMessage: [{
848
- "type": 0,
849
- "value": "Password field is required"
850
- }]
851
- })).min(PASSWORD_MINIMUM_LENGTH, intl.formatMessage({
852
- id: "TZ4WUk",
853
- defaultMessage: [{
854
- "type": 0,
855
- "value": "Password requires "
856
- }, {
857
- "type": 1,
858
- "value": "value"
859
- }, {
860
- "type": 0,
861
- "value": " characters"
862
- }]
863
- }, {
864
- value: PASSWORD_MINIMUM_LENGTH
865
- })).trim()
866
- // remember: yup.boolean(),
867
- });
868
-
869
- const formMethods = (0, import_forms4.useForm)({
870
- defaultValues,
871
- mode: "onBlur",
872
- resolver: (0, import_forms4.yupResolver)(schema)
873
- });
874
- const onSubmitForm = data => {
875
- return onSignIn(data);
876
- };
877
- return /* @__PURE__ */(0, import_jsx_runtime7.jsx)(import_forms4.Form, {
878
- sx: {
879
- maxWidth: "390px",
880
- width: "100%"
881
- },
882
- ...formMethods,
883
- onSubmit: onSubmitForm,
884
- children: /* @__PURE__ */(0, import_jsx_runtime7.jsxs)(AuthCard, {
885
- title: intl.formatMessage({
886
- id: "F2iS37",
887
- defaultMessage: [{
888
- "type": 0,
889
- "value": "Log in"
890
- }]
891
- }),
892
- buttonLabel: intl.formatMessage({
893
- id: "/J+02x",
894
- defaultMessage: [{
895
- "type": 0,
896
- "value": "Log in"
897
- }]
898
- }),
899
- isValidForm: formMethods.formState.isValid,
900
- extraButton: /* @__PURE__ */(0, import_jsx_runtime7.jsx)(import_ui5.Button, {
901
- type: "button",
902
- variant: "secondary",
903
- sx: {
904
- textAlign: "center",
905
- display: "initial"
906
- },
907
- onClick: onSignUp,
908
- "aria-label": "sign-up",
909
- children: intl.formatMessage({
910
- id: "EZ3YF2",
911
- defaultMessage: [{
912
- "type": 0,
913
- "value": "Sign up"
914
- }]
915
- })
916
- }),
917
- children: [/* @__PURE__ */(0, import_jsx_runtime7.jsxs)(import_ui5.Flex, {
918
- sx: {
919
- flexDirection: "column",
920
- gap: "xl"
921
- },
922
- children: [/* @__PURE__ */(0, import_jsx_runtime7.jsx)(import_forms4.FormFieldInput, {
923
- name: "email",
924
- label: intl.formatMessage({
925
- id: "5E12mO",
926
- defaultMessage: [{
927
- "type": 0,
928
- "value": "Email"
929
- }]
930
- })
931
- }), /* @__PURE__ */(0, import_jsx_runtime7.jsx)(import_forms4.FormFieldPassword, {
932
- name: "password",
933
- label: intl.formatMessage({
934
- id: "PylVqx",
935
- defaultMessage: [{
936
- "type": 0,
937
- "value": "Password"
938
- }]
939
- })
940
- })]
941
- }), /* @__PURE__ */(0, import_jsx_runtime7.jsx)(import_ui5.Flex, {
942
- sx: {
943
- justifyContent: "space-between",
944
- marginTop: "lg"
945
- },
946
- children: /* @__PURE__ */(0, import_jsx_runtime7.jsx)(import_ui5.Text, {
947
- sx: {
948
- marginLeft: "auto",
949
- cursor: "pointer"
950
- },
951
- as: import_ui5.Link,
952
- onClick: onForgotPassword,
953
- children: intl.formatMessage({
954
- id: "BtK6KR",
955
- defaultMessage: [{
956
- "type": 0,
957
- "value": "Forgot password?"
958
- }]
959
- })
960
- })
961
- }), /* @__PURE__ */(0, import_jsx_runtime7.jsx)(import_react_notifications4.NotificationsBox, {})]
962
- })
963
- });
964
- };
965
-
966
- // src/AuthSignUp.tsx
967
- var React5 = __toESM(require("react"));
968
- var import_ui6 = require("@ttoss/ui");
969
- var import_forms5 = require("@ttoss/forms");
970
- var import_react_notifications5 = require("@ttoss/react-notifications");
971
- var import_react_i18n5 = require("@ttoss/react-i18n");
972
- var import_jsx_runtime8 = require("react/jsx-runtime");
973
- var AuthSignUp = ({
974
- onSignUp,
975
- onReturnToSignIn
976
- }) => {
977
- const {
978
- intl
979
- } = (0, import_react_i18n5.useI18n)();
980
- const {
981
- setNotifications
982
- } = (0, import_react_notifications5.useNotifications)();
983
- React5.useEffect(() => {
984
- setNotifications(void 0);
985
- }, [setNotifications]);
986
- const schema = import_forms5.yup.object().shape({
987
- email: import_forms5.yup.string().required(intl.formatMessage({
988
- id: "d1YCuH",
989
- defaultMessage: [{
990
- "type": 0,
991
- "value": "Enter your email address"
992
- }]
993
- })).email(intl.formatMessage({
994
- id: "OhDL0i",
995
- defaultMessage: [{
996
- "type": 0,
997
- "value": "Invalid email"
998
- }]
999
- })),
1000
- password: import_forms5.yup.string().required(intl.formatMessage({
1001
- id: "kdFYba",
1002
- defaultMessage: [{
1003
- "type": 0,
1004
- "value": "Password field is required"
1005
- }]
1006
- })).min(PASSWORD_MINIMUM_LENGTH, intl.formatMessage({
1007
- id: "TZ4WUk",
1008
- defaultMessage: [{
1009
- "type": 0,
1010
- "value": "Password requires "
1011
- }, {
1012
- "type": 1,
1013
- "value": "value"
1014
- }, {
1015
- "type": 0,
1016
- "value": " characters"
1017
- }]
1018
- }, {
1019
- value: PASSWORD_MINIMUM_LENGTH
1020
- })).trim(),
1021
- confirmPassword: import_forms5.yup.string().required(intl.formatMessage({
1022
- id: "NJ57Qj",
1023
- defaultMessage: [{
1024
- "type": 0,
1025
- "value": "Confirm password field is required"
1026
- }]
1027
- })).oneOf([import_forms5.yup.ref("password")], intl.formatMessage({
1028
- id: "WU/CqP",
1029
- defaultMessage: [{
1030
- "type": 0,
1031
- "value": "Passwords are not the same"
1032
- }]
1033
- }))
1034
- });
1035
- const formMethods = (0, import_forms5.useForm)({
1036
- mode: "all",
1037
- resolver: (0, import_forms5.yupResolver)(schema)
1038
- });
1039
- const onSubmitForm = data => {
1040
- return onSignUp(data);
1041
- };
1042
- return /* @__PURE__ */(0, import_jsx_runtime8.jsx)(import_forms5.Form, {
1043
- sx: {
1044
- maxWidth: "390px",
1045
- width: "100%"
1046
- },
1047
- ...formMethods,
1048
- onSubmit: onSubmitForm,
1049
- children: /* @__PURE__ */(0, import_jsx_runtime8.jsxs)(AuthCard, {
1050
- buttonLabel: intl.formatMessage({
1051
- id: "URJDrG",
1052
- defaultMessage: [{
1053
- "type": 0,
1054
- "value": "Sign up"
1055
- }]
1056
- }),
1057
- title: intl.formatMessage({
1058
- id: "oayEC3",
1059
- defaultMessage: [{
1060
- "type": 0,
1061
- "value": "Sign up"
1062
- }]
1063
- }),
1064
- isValidForm: formMethods.formState.isValid,
1065
- extraButton: /* @__PURE__ */(0, import_jsx_runtime8.jsx)(import_ui6.Text, {
1066
- sx: {
1067
- cursor: "pointer"
1068
- },
1069
- onClick: onReturnToSignIn,
1070
- as: import_ui6.Link,
1071
- children: intl.formatMessage({
1072
- id: "rx8HF/",
1073
- defaultMessage: [{
1074
- "type": 0,
1075
- "value": "I’m already registered"
1076
- }]
1077
- })
1078
- }),
1079
- children: [/* @__PURE__ */(0, import_jsx_runtime8.jsxs)(import_ui6.Flex, {
1080
- sx: {
1081
- flexDirection: "column",
1082
- gap: "xl"
1083
- },
1084
- children: [/* @__PURE__ */(0, import_jsx_runtime8.jsx)(import_forms5.FormFieldInput, {
1085
- name: "email",
1086
- label: intl.formatMessage({
1087
- id: "5E12mO",
1088
- defaultMessage: [{
1089
- "type": 0,
1090
- "value": "Email"
1091
- }]
1092
- })
1093
- }), /* @__PURE__ */(0, import_jsx_runtime8.jsx)(import_forms5.FormFieldPassword, {
1094
- name: "password",
1095
- label: intl.formatMessage({
1096
- id: "PylVqx",
1097
- defaultMessage: [{
1098
- "type": 0,
1099
- "value": "Password"
1100
- }]
1101
- })
1102
- }), /* @__PURE__ */(0, import_jsx_runtime8.jsx)(import_forms5.FormFieldPassword, {
1103
- name: "confirmPassword",
1104
- label: intl.formatMessage({
1105
- id: "lY+cuM",
1106
- defaultMessage: [{
1107
- "type": 0,
1108
- "value": "Confirm password"
1109
- }]
1110
- })
1111
- })]
1112
- }), /* @__PURE__ */(0, import_jsx_runtime8.jsx)(import_react_notifications5.NotificationsBox, {})]
1113
- })
1114
- });
1115
- };
1116
-
1117
- // src/Auth.tsx
1118
- var import_xstate = require("xstate");
1119
- var import_react = require("@xstate/react");
1120
- var import_react_notifications6 = require("@ttoss/react-notifications");
1121
- var import_jsx_runtime9 = require("react/jsx-runtime");
1122
- var authMachine = (0, import_xstate.createMachine)({
1123
- predictableActionArguments: true,
1124
- initial: "signIn",
1125
- states: {
1126
- signIn: {
1127
- on: {
1128
- SIGN_UP: {
1129
- target: "signUp"
1130
- },
1131
- SIGN_UP_RESEND_CONFIRMATION: {
1132
- actions: ["assignEmail"],
1133
- target: "signUpConfirm"
1134
- },
1135
- FORGOT_PASSWORD: {
1136
- target: "forgotPassword"
1137
- }
1138
- }
1139
- },
1140
- signUp: {
1141
- on: {
1142
- SIGN_UP_CONFIRM: {
1143
- actions: ["assignEmail"],
1144
- target: "signUpConfirm"
1145
- },
1146
- RETURN_TO_SIGN_IN: {
1147
- target: "signIn"
1148
- }
1149
- }
1150
- },
1151
- signUpConfirm: {
1152
- on: {
1153
- SIGN_UP_CONFIRMED: {
1154
- actions: ["assignEmail"],
1155
- target: "signIn"
1156
- }
1157
- }
1158
- },
1159
- forgotPassword: {
1160
- on: {
1161
- RETURN_TO_SIGN_IN: {
1162
- target: "signIn"
1163
- },
1164
- SIGN_UP: {
1165
- target: "signUp"
1166
- },
1167
- FORGOT_PASSWORD_RESET_PASSWORD: {
1168
- actions: ["assignEmail"],
1169
- target: "forgotPasswordResetPassword"
1170
- }
1171
- }
1172
- },
1173
- forgotPasswordResetPassword: {
1174
- on: {
1175
- FORGOT_PASSWORD_CONFIRMED: {
1176
- actions: ["assignEmail"],
1177
- target: "signIn"
1178
- },
1179
- RETURN_TO_SIGN_IN: {
1180
- target: "signIn"
1181
- }
1182
- }
1183
- }
1184
- }
1185
- }, {
1186
- actions: {
1187
- assignEmail: (0, import_xstate.assign)({
1188
- email: (_, event) => {
1189
- if ("email" in event) {
1190
- return event.email;
1191
- }
1192
- return void 0;
1193
- }
1194
- })
1195
- }
1196
- });
1197
- var AuthLogic = () => {
1198
- const {
1199
- isAuthenticated
1200
- } = useAuth();
1201
- const [state, send] = (0, import_react.useMachine)(authMachine);
1202
- const {
1203
- setLoading,
1204
- setNotifications
1205
- } = (0, import_react_notifications6.useNotifications)();
1206
- const onSignIn = React6.useCallback(async ({
1207
- email,
1208
- password
1209
- }) => {
1210
- try {
1211
- setLoading(true);
1212
- await import_aws_amplify2.Auth.signIn(email, password);
1213
- } catch (error) {
1214
- switch (error.code) {
1215
- case "UserNotConfirmedException":
1216
- await import_aws_amplify2.Auth.resendSignUp(email);
1217
- send({
1218
- type: "SIGN_UP_RESEND_CONFIRMATION",
1219
- email
1220
- });
1221
- break;
1222
- default:
1223
- }
1224
- setNotifications({
1225
- type: "error",
1226
- message: error.message
1227
- });
1228
- } finally {
1229
- setLoading(false);
1230
- }
1231
- }, [send, setLoading, setNotifications]);
1232
- const onSignUp = React6.useCallback(async ({
1233
- email,
1234
- password
1235
- }) => {
1236
- try {
1237
- setLoading(true);
1238
- await import_aws_amplify2.Auth.signUp({
1239
- username: email,
1240
- password,
1241
- attributes: {
1242
- email
1243
- }
1244
- });
1245
- send({
1246
- type: "SIGN_UP_CONFIRM",
1247
- email
1248
- });
1249
- } catch (error) {
1250
- setNotifications({
1251
- type: "error",
1252
- message: error.message
1253
- });
1254
- } finally {
1255
- setLoading(false);
1256
- }
1257
- }, [send, setLoading, setNotifications]);
1258
- const onConfirmSignUp = React6.useCallback(async ({
1259
- email,
1260
- code
1261
- }) => {
1262
- try {
1263
- setLoading(true);
1264
- await import_aws_amplify2.Auth.confirmSignUp(email, code);
1265
- send({
1266
- type: "SIGN_UP_CONFIRMED",
1267
- email
1268
- });
1269
- } catch (error) {
1270
- setNotifications({
1271
- type: "error",
1272
- message: error.message
1273
- });
1274
- } finally {
1275
- setLoading(false);
1276
- }
1277
- }, [send, setLoading, setNotifications]);
1278
- const onReturnToSignIn = React6.useCallback(() => {
1279
- send({
1280
- type: "RETURN_TO_SIGN_IN"
1281
- });
1282
- }, [send]);
1283
- const onForgotPassword = React6.useCallback(async ({
1284
- email
1285
- }) => {
1286
- try {
1287
- setLoading(true);
1288
- await import_aws_amplify2.Auth.forgotPassword(email);
1289
- send({
1290
- type: "FORGOT_PASSWORD_RESET_PASSWORD",
1291
- email
1292
- });
1293
- } catch (error) {
1294
- setNotifications({
1295
- type: "error",
1296
- message: error.message
1297
- });
1298
- } finally {
1299
- setLoading(false);
1300
- }
1301
- }, [send, setLoading, setNotifications]);
1302
- const onForgotPasswordResetPassword = React6.useCallback(async ({
1303
- email,
1304
- code,
1305
- newPassword
1306
- }) => {
1307
- try {
1308
- setLoading(true);
1309
- await import_aws_amplify2.Auth.forgotPasswordSubmit(email, code, newPassword);
1310
- send({
1311
- type: "FORGOT_PASSWORD_CONFIRMED",
1312
- email
1313
- });
1314
- } catch (error) {
1315
- setNotifications({
1316
- type: "error",
1317
- message: error.message
1318
- });
1319
- } finally {
1320
- setLoading(false);
1321
- }
1322
- }, [send, setLoading, setNotifications]);
1323
- if (isAuthenticated) {
1324
- return null;
1325
- }
1326
- if (state.matches("signUp")) {
1327
- return /* @__PURE__ */(0, import_jsx_runtime9.jsx)(AuthSignUp, {
1328
- onSignUp,
1329
- onReturnToSignIn
1330
- });
1331
- }
1332
- if (state.matches("signUpConfirm")) {
1333
- return /* @__PURE__ */(0, import_jsx_runtime9.jsx)(AuthConfirmSignUp, {
1334
- onConfirmSignUp,
1335
- email: state.context.email
1336
- });
1337
- }
1338
- if (state.matches("forgotPassword")) {
1339
- return /* @__PURE__ */(0, import_jsx_runtime9.jsx)(AuthForgotPassword, {
1340
- onForgotPassword,
1341
- onCancel: onReturnToSignIn,
1342
- onSignUp: () => {
1343
- return send("SIGN_UP");
1344
- }
1345
- });
1346
- }
1347
- if (state.matches("forgotPasswordResetPassword")) {
1348
- return /* @__PURE__ */(0, import_jsx_runtime9.jsx)(AuthForgotPasswordResetPassword, {
1349
- email: state.context.email,
1350
- onForgotPasswordResetPassword,
1351
- onCancel: onReturnToSignIn
1352
- });
1353
- }
1354
- return /* @__PURE__ */(0, import_jsx_runtime9.jsx)(AuthSignIn, {
1355
- onSignIn,
1356
- onSignUp: () => {
1357
- return send("SIGN_UP");
1358
- },
1359
- onForgotPassword: () => {
1360
- return send("FORGOT_PASSWORD");
1361
- },
1362
- defaultValues: {
1363
- email: state.context.email
1364
- }
1365
- });
1366
- };
1367
- var Auth2 = ({
1368
- logo,
1369
- fullScreen = true
1370
- }) => {
1371
- const withLogoNode = React6.useMemo(() => {
1372
- return /* @__PURE__ */(0, import_jsx_runtime9.jsx)(LogoProvider, {
1373
- logo,
1374
- children: /* @__PURE__ */(0, import_jsx_runtime9.jsx)(AuthLogic, {})
1375
- });
1376
- }, [logo]);
1377
- if (fullScreen) {
1378
- return /* @__PURE__ */(0, import_jsx_runtime9.jsx)(AuthFullScreen, {
1379
- children: withLogoNode
1380
- });
1381
- }
1382
- return withLogoNode;
1383
- };
1384
- // Annotate the CommonJS export names for ESM import in node:
1385
- 0 && (module.exports = {
1386
- Auth,
1387
- AuthProvider,
1388
- useAuth
1389
- });