@ttoss/react-auth 2.0.9 → 2.1.1

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.
@@ -0,0 +1,58 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import * as React from 'react';
3
+
4
+ type User = {
5
+ id: string;
6
+ email: string;
7
+ emailVerified: string;
8
+ } | null;
9
+ type Tokens = {
10
+ idToken: string;
11
+ accessToken: string;
12
+ refreshToken: string;
13
+ } | null;
14
+ declare const AuthProvider: ({ children }: {
15
+ children: React.ReactNode;
16
+ }) => react_jsx_runtime.JSX.Element | null;
17
+ declare const useAuth: () => {
18
+ signOut: () => Promise<any>;
19
+ isAuthenticated: boolean;
20
+ user: User;
21
+ tokens: Tokens;
22
+ };
23
+
24
+ type LogoContextProps = {
25
+ logo?: React.ReactNode;
26
+ children?: React.ReactNode;
27
+ };
28
+
29
+ type AuthProps = LogoContextProps & {
30
+ fullScreen?: boolean;
31
+ };
32
+ declare const Auth: ({ logo, fullScreen }: AuthProps) => react_jsx_runtime.JSX.Element;
33
+
34
+ type OnSignInInput = {
35
+ email: string;
36
+ password: string;
37
+ };
38
+ type OnSignIn = (input: OnSignInInput) => void;
39
+ type OnSignUpInput = {
40
+ email: string;
41
+ password: string;
42
+ confirmPassword: string;
43
+ };
44
+ type OnSignUp = (input: OnSignUpInput) => void;
45
+ type OnConfirmSignUp = (input: {
46
+ email: string;
47
+ code: string;
48
+ }) => void;
49
+ type OnForgotPassword = (input: {
50
+ email: string;
51
+ }) => void;
52
+ type OnForgotPasswordResetPassword = (input: {
53
+ email: string;
54
+ code: string;
55
+ newPassword: string;
56
+ }) => void;
57
+
58
+ export { Auth, AuthProvider, type OnConfirmSignUp, type OnForgotPassword, type OnForgotPasswordResetPassword, type OnSignIn, type OnSignInInput, type OnSignUp, type OnSignUpInput, useAuth };
@@ -0,0 +1,228 @@
1
+ {
2
+ "/J+02x": [
3
+ {
4
+ "type": 0,
5
+ "value": "Log in"
6
+ }
7
+ ],
8
+ "0XOzcH": [
9
+ {
10
+ "type": 0,
11
+ "value": "Required field"
12
+ }
13
+ ],
14
+ "42HafR": [
15
+ {
16
+ "type": 0,
17
+ "value": "Code"
18
+ }
19
+ ],
20
+ "5E12mO": [
21
+ {
22
+ "type": 0,
23
+ "value": "Email"
24
+ }
25
+ ],
26
+ "5oOshZ": [
27
+ {
28
+ "type": 0,
29
+ "value": "Enter your email address"
30
+ }
31
+ ],
32
+ "6PdOcy": [
33
+ {
34
+ "type": 0,
35
+ "value": "Cancel"
36
+ }
37
+ ],
38
+ "8GMUPk": [
39
+ {
40
+ "type": 0,
41
+ "value": "I'm already registered"
42
+ }
43
+ ],
44
+ "9cApwd": [
45
+ {
46
+ "type": 0,
47
+ "value": "Please, insert a valid e-mail"
48
+ }
49
+ ],
50
+ "BtK6KR": [
51
+ {
52
+ "type": 0,
53
+ "value": "Forgot password?"
54
+ }
55
+ ],
56
+ "D1C6fR": [
57
+ {
58
+ "type": 0,
59
+ "value": "Field must be at least "
60
+ },
61
+ {
62
+ "type": 1,
63
+ "value": "min"
64
+ },
65
+ {
66
+ "type": 0,
67
+ "value": " characters"
68
+ }
69
+ ],
70
+ "EZ3YF2": [
71
+ {
72
+ "type": 0,
73
+ "value": "Sign up"
74
+ }
75
+ ],
76
+ "F2iS37": [
77
+ {
78
+ "type": 0,
79
+ "value": "Log in"
80
+ }
81
+ ],
82
+ "KY2T6J": [
83
+ {
84
+ "type": 0,
85
+ "value": "Code"
86
+ }
87
+ ],
88
+ "MfWGyg": [
89
+ {
90
+ "type": 0,
91
+ "value": "Field is required"
92
+ }
93
+ ],
94
+ "NJ57Qj": [
95
+ {
96
+ "type": 0,
97
+ "value": "Confirm password field is required"
98
+ }
99
+ ],
100
+ "OhDL0i": [
101
+ {
102
+ "type": 0,
103
+ "value": "Invalid email"
104
+ }
105
+ ],
106
+ "PylVqx": [
107
+ {
108
+ "type": 0,
109
+ "value": "Password"
110
+ }
111
+ ],
112
+ "S3pjKw": [
113
+ {
114
+ "type": 0,
115
+ "value": "Minimum "
116
+ },
117
+ {
118
+ "type": 1,
119
+ "value": "value"
120
+ },
121
+ {
122
+ "type": 0,
123
+ "value": " characters"
124
+ }
125
+ ],
126
+ "S4bbEj": [
127
+ {
128
+ "type": 0,
129
+ "value": "Recovering Password"
130
+ }
131
+ ],
132
+ "SnONhb": [
133
+ {
134
+ "type": 0,
135
+ "value": "Please, insert a valid e-mail"
136
+ }
137
+ ],
138
+ "TZ4WUk": [
139
+ {
140
+ "type": 0,
141
+ "value": "Password requires "
142
+ },
143
+ {
144
+ "type": 1,
145
+ "value": "value"
146
+ },
147
+ {
148
+ "type": 0,
149
+ "value": " characters"
150
+ }
151
+ ],
152
+ "UNttd+": [
153
+ {
154
+ "type": 0,
155
+ "value": "Confirm"
156
+ }
157
+ ],
158
+ "URJDrG": [
159
+ {
160
+ "type": 0,
161
+ "value": "Sign up"
162
+ }
163
+ ],
164
+ "WU/CqP": [
165
+ {
166
+ "type": 0,
167
+ "value": "Passwords are not the same"
168
+ }
169
+ ],
170
+ "XreZg+": [
171
+ {
172
+ "type": 0,
173
+ "value": "Registered Email"
174
+ }
175
+ ],
176
+ "ZhaPt0": [
177
+ {
178
+ "type": 0,
179
+ "value": "Invalid Value for Field of type "
180
+ },
181
+ {
182
+ "type": 1,
183
+ "value": "type"
184
+ }
185
+ ],
186
+ "cGR2eI": [
187
+ {
188
+ "type": 0,
189
+ "value": "Confirmation"
190
+ }
191
+ ],
192
+ "d1YCuH": [
193
+ {
194
+ "type": 0,
195
+ "value": "Enter your email address"
196
+ }
197
+ ],
198
+ "kdFYba": [
199
+ {
200
+ "type": 0,
201
+ "value": "Password field is required"
202
+ }
203
+ ],
204
+ "lY+cuM": [
205
+ {
206
+ "type": 0,
207
+ "value": "Confirm password"
208
+ }
209
+ ],
210
+ "lZvoYL": [
211
+ {
212
+ "type": 0,
213
+ "value": "Sign up now"
214
+ }
215
+ ],
216
+ "mZzmNV": [
217
+ {
218
+ "type": 0,
219
+ "value": "Recover Password"
220
+ }
221
+ ],
222
+ "oayEC3": [
223
+ {
224
+ "type": 0,
225
+ "value": "Sign up"
226
+ }
227
+ ]
228
+ }
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@ttoss/react-auth",
3
- "version": "2.0.9",
3
+ "version": "2.1.1",
4
4
  "description": "ttoss authentication module for React apps.",
5
+ "license": "MIT",
5
6
  "author": "ttoss",
6
7
  "contributors": [
7
8
  "Pedro Arantes <pedro@arantespp.com> (https://arantespp.com/contact)"
@@ -20,21 +21,20 @@
20
21
  },
21
22
  "files": [
22
23
  "dist",
23
- "i18n",
24
- "src"
24
+ "i18n"
25
25
  ],
26
26
  "sideEffects": false,
27
27
  "dependencies": {
28
28
  "@xstate/react": "^3.2.2",
29
29
  "xstate": "^4.38.3",
30
- "@ttoss/forms": "^0.25.6"
30
+ "@ttoss/forms": "^0.26.1"
31
31
  },
32
32
  "peerDependencies": {
33
33
  "aws-amplify": "^6.0.0",
34
34
  "react": ">=16.8.0",
35
- "@ttoss/react-i18n": "^2.0.2",
36
- "@ttoss/ui": "^5.0.6",
37
- "@ttoss/react-notifications": "^1.24.56"
35
+ "@ttoss/react-i18n": "^2.0.4",
36
+ "@ttoss/ui": "^5.0.8",
37
+ "@ttoss/react-notifications": "^1.24.58"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@jest/globals": "^29.7.0",
@@ -43,13 +43,13 @@
43
43
  "jest": "^29.7.0",
44
44
  "react": "^18.3.1",
45
45
  "tsup": "^8.3.0",
46
- "@ttoss/cloud-auth": "^0.12.20",
47
- "@ttoss/config": "^1.33.0",
48
- "@ttoss/react-notifications": "^1.24.56",
49
- "@ttoss/i18n-cli": "^0.7.20",
50
- "@ttoss/test-utils": "^2.1.15",
51
- "@ttoss/react-i18n": "^2.0.2",
52
- "@ttoss/ui": "^5.0.6"
46
+ "@ttoss/cloud-auth": "^0.12.22",
47
+ "@ttoss/config": "^1.34.1",
48
+ "@ttoss/i18n-cli": "^0.7.22",
49
+ "@ttoss/react-notifications": "^1.24.58",
50
+ "@ttoss/react-i18n": "^2.0.4",
51
+ "@ttoss/test-utils": "^2.1.17",
52
+ "@ttoss/ui": "^5.0.8"
53
53
  },
54
54
  "keywords": [
55
55
  "React",
package/src/Auth.tsx DELETED
@@ -1,328 +0,0 @@
1
- import * as React from 'react';
2
- import { AuthConfirmSignUp } from './AuthConfirmSignUp';
3
- import { AuthForgotPassword } from './AuthForgotPassword';
4
- import { AuthForgotPasswordResetPassword } from './AuthForgotPasswordResetPassword';
5
- import { AuthFullScreen } from './AuthFullScreen';
6
- import { AuthSignIn } from './AuthSignIn';
7
- import { AuthSignUp } from './AuthSignUp';
8
- import { LogoContextProps, LogoProvider } from './AuthCard';
9
- import { assign, createMachine } from 'xstate';
10
- import {
11
- confirmResetPassword,
12
- confirmSignUp,
13
- resendSignUpCode,
14
- resetPassword,
15
- signIn,
16
- signUp,
17
- } from 'aws-amplify/auth';
18
- import { useAuth } from './AuthProvider';
19
- import { useMachine } from '@xstate/react';
20
- import { useNotifications } from '@ttoss/react-notifications';
21
- import type {
22
- OnConfirmSignUp,
23
- OnForgotPassword,
24
- OnForgotPasswordResetPassword,
25
- OnSignIn,
26
- OnSignUp,
27
- } from './types';
28
-
29
- type AuthState =
30
- | {
31
- value: 'signIn';
32
- context: { email?: string };
33
- }
34
- | {
35
- value: 'signUp';
36
- context: Record<string, never>;
37
- }
38
- | {
39
- value: 'signUpConfirm';
40
- context: { email: string };
41
- }
42
- | {
43
- value: 'signUpResendConfirmation';
44
- context: { email: string };
45
- }
46
- | {
47
- value: 'forgotPassword';
48
- context: Record<string, never>;
49
- }
50
- | {
51
- value: 'forgotPasswordResetPassword';
52
- context: { email: string };
53
- };
54
-
55
- type AuthEvent =
56
- | { type: 'SIGN_UP' }
57
- | { type: 'SIGN_UP_CONFIRM'; email: string }
58
- | { type: 'SIGN_UP_CONFIRMED'; email: string }
59
- | { type: 'SIGN_UP_RESEND_CONFIRMATION'; email: string }
60
- | { type: 'RETURN_TO_SIGN_IN' }
61
- | { type: 'FORGOT_PASSWORD' }
62
- | { type: 'FORGOT_PASSWORD_RESET_PASSWORD'; email: string }
63
- | { type: 'FORGOT_PASSWORD_CONFIRMED'; email: string };
64
-
65
- type AuthContext = { email?: string };
66
-
67
- const authMachine = createMachine<AuthContext, AuthEvent, AuthState>(
68
- {
69
- predictableActionArguments: true,
70
- initial: 'signIn',
71
- states: {
72
- signIn: {
73
- on: {
74
- SIGN_UP: { target: 'signUp' },
75
- SIGN_UP_RESEND_CONFIRMATION: {
76
- actions: ['assignEmail'],
77
- target: 'signUpConfirm',
78
- },
79
- FORGOT_PASSWORD: { target: 'forgotPassword' },
80
- },
81
- },
82
- signUp: {
83
- on: {
84
- SIGN_UP_CONFIRM: {
85
- actions: ['assignEmail'],
86
- target: 'signUpConfirm',
87
- },
88
- RETURN_TO_SIGN_IN: { target: 'signIn' },
89
- },
90
- },
91
- signUpConfirm: {
92
- on: {
93
- SIGN_UP_CONFIRMED: {
94
- actions: ['assignEmail'],
95
- target: 'signIn',
96
- },
97
- },
98
- },
99
- forgotPassword: {
100
- on: {
101
- RETURN_TO_SIGN_IN: { target: 'signIn' },
102
- SIGN_UP: { target: 'signUp' },
103
- FORGOT_PASSWORD_RESET_PASSWORD: {
104
- actions: ['assignEmail'],
105
- target: 'forgotPasswordResetPassword',
106
- },
107
- },
108
- },
109
- forgotPasswordResetPassword: {
110
- on: {
111
- FORGOT_PASSWORD_CONFIRMED: {
112
- actions: ['assignEmail'],
113
- target: 'signIn',
114
- },
115
- RETURN_TO_SIGN_IN: { target: 'signIn' },
116
- },
117
- },
118
- },
119
- },
120
- {
121
- actions: {
122
- assignEmail: assign({
123
- email: (_, event) => {
124
- if ('email' in event) {
125
- return event.email;
126
- }
127
-
128
- return undefined;
129
- },
130
- }),
131
- },
132
- }
133
- );
134
-
135
- const AuthLogic = () => {
136
- const { isAuthenticated } = useAuth();
137
-
138
- const [state, send] = useMachine(authMachine);
139
-
140
- const { setLoading, setNotifications } = useNotifications();
141
-
142
- const onSignIn = React.useCallback<OnSignIn>(
143
- async ({ email, password }) => {
144
- try {
145
- setLoading(true);
146
- await signIn({ username: email, password });
147
- // toast('Signed In');
148
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
149
- } catch (error: any) {
150
- switch (error.code) {
151
- case 'UserNotConfirmedException':
152
- await resendSignUpCode({ username: email });
153
- send({ type: 'SIGN_UP_RESEND_CONFIRMATION', email });
154
- break;
155
- default:
156
- // toast(JSON.stringify(error, null, 2));
157
- }
158
- setNotifications({ type: 'error', message: error.message });
159
- } finally {
160
- setLoading(false);
161
- }
162
- },
163
- [send, setLoading, setNotifications]
164
- );
165
-
166
- const onSignUp = React.useCallback<OnSignUp>(
167
- async ({ email, password }) => {
168
- try {
169
- setLoading(true);
170
- await signUp({
171
- username: email,
172
- password,
173
- options: {
174
- userAttributes: {
175
- email,
176
- },
177
- },
178
- });
179
- // toast('Signed Up');
180
- send({ type: 'SIGN_UP_CONFIRM', email });
181
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
182
- } catch (error: any) {
183
- setNotifications({ type: 'error', message: error.message });
184
- // toast(JSON.stringify(error, null, 2));
185
- } finally {
186
- setLoading(false);
187
- }
188
- },
189
- [send, setLoading, setNotifications]
190
- );
191
-
192
- const onConfirmSignUp = React.useCallback<OnConfirmSignUp>(
193
- async ({ email, code }) => {
194
- try {
195
- setLoading(true);
196
- await confirmSignUp({ confirmationCode: code, username: email });
197
- // toast('Confirmed Signed In');
198
- send({ type: 'SIGN_UP_CONFIRMED', email });
199
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
200
- } catch (error: any) {
201
- setNotifications({ type: 'error', message: error.message });
202
- // toast(JSON.stringify(error, null, 2));
203
- } finally {
204
- setLoading(false);
205
- }
206
- },
207
- [send, setLoading, setNotifications]
208
- );
209
-
210
- const onReturnToSignIn = React.useCallback(() => {
211
- send({ type: 'RETURN_TO_SIGN_IN' });
212
- }, [send]);
213
-
214
- const onForgotPassword = React.useCallback<OnForgotPassword>(
215
- async ({ email }) => {
216
- try {
217
- setLoading(true);
218
- await resetPassword({ username: email });
219
- // toast('Forgot Password');
220
- send({ type: 'FORGOT_PASSWORD_RESET_PASSWORD', email });
221
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
222
- } catch (error: any) {
223
- setNotifications({ type: 'error', message: error.message });
224
- // toast(JSON.stringify(error, null, 2));
225
- } finally {
226
- setLoading(false);
227
- }
228
- },
229
- [send, setLoading, setNotifications]
230
- );
231
-
232
- const onForgotPasswordResetPassword =
233
- React.useCallback<OnForgotPasswordResetPassword>(
234
- async ({ email, code, newPassword }) => {
235
- try {
236
- setLoading(true);
237
- await confirmResetPassword({
238
- confirmationCode: code,
239
- username: email,
240
- newPassword,
241
- });
242
- // toast('Forgot Password Reset Password');
243
- send({ type: 'FORGOT_PASSWORD_CONFIRMED', email });
244
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
245
- } catch (error: any) {
246
- setNotifications({ type: 'error', message: error.message });
247
- // toast(JSON.stringify(error, null, 2));
248
- } finally {
249
- setLoading(false);
250
- }
251
- },
252
- [send, setLoading, setNotifications]
253
- );
254
-
255
- if (isAuthenticated) {
256
- return null;
257
- }
258
-
259
- if (state.matches('signUp')) {
260
- return (
261
- <AuthSignUp onSignUp={onSignUp} onReturnToSignIn={onReturnToSignIn} />
262
- );
263
- }
264
-
265
- if (state.matches('signUpConfirm')) {
266
- return (
267
- <AuthConfirmSignUp
268
- onConfirmSignUp={onConfirmSignUp}
269
- email={state.context.email}
270
- />
271
- );
272
- }
273
-
274
- if (state.matches('forgotPassword')) {
275
- return (
276
- <AuthForgotPassword
277
- onForgotPassword={onForgotPassword}
278
- onCancel={onReturnToSignIn}
279
- onSignUp={() => {
280
- return send('SIGN_UP');
281
- }}
282
- />
283
- );
284
- }
285
-
286
- if (state.matches('forgotPasswordResetPassword')) {
287
- return (
288
- <AuthForgotPasswordResetPassword
289
- email={state.context.email}
290
- onForgotPasswordResetPassword={onForgotPasswordResetPassword}
291
- onCancel={onReturnToSignIn}
292
- />
293
- );
294
- }
295
-
296
- return (
297
- <AuthSignIn
298
- onSignIn={onSignIn}
299
- onSignUp={() => {
300
- return send('SIGN_UP');
301
- }}
302
- onForgotPassword={() => {
303
- return send('FORGOT_PASSWORD');
304
- }}
305
- defaultValues={{ email: state.context.email }}
306
- />
307
- );
308
- };
309
-
310
- type AuthProps = LogoContextProps & {
311
- fullScreen?: boolean;
312
- };
313
-
314
- export const Auth = ({ logo, fullScreen = true }: AuthProps) => {
315
- const withLogoNode = React.useMemo(() => {
316
- return (
317
- <LogoProvider logo={logo}>
318
- <AuthLogic />
319
- </LogoProvider>
320
- );
321
- }, [logo]);
322
-
323
- if (fullScreen) {
324
- return <AuthFullScreen>{withLogoNode}</AuthFullScreen>;
325
- }
326
-
327
- return withLogoNode;
328
- };