@ttoss/react-auth 1.2.5 → 1.2.7

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/esm/index.js CHANGED
@@ -14,24 +14,33 @@ var AuthContext = React.createContext({
14
14
  tokens: null
15
15
  });
16
16
  var AuthProvider = ({ children }) => {
17
- const [user, setUser] = React.useState(null);
18
- const [tokens, setTokens] = React.useState(null);
17
+ const [{ user, tokens, isAuthenticated }, setAuthState] = React.useState({
18
+ user: null,
19
+ tokens: null,
20
+ isAuthenticated: void 0
21
+ });
19
22
  React.useEffect(() => {
20
23
  const updateUser = () => {
21
24
  Auth.currentAuthenticatedUser().then(({ attributes, signInUserSession }) => {
22
- setUser({
23
- id: attributes.sub,
24
- email: attributes.email,
25
- emailVerified: attributes["email_verified"]
26
- });
27
- setTokens({
28
- idToken: signInUserSession.idToken.jwtToken,
29
- accessToken: signInUserSession.accessToken.jwtToken,
30
- refreshToken: signInUserSession.refreshToken.token
25
+ setAuthState({
26
+ user: {
27
+ id: attributes.sub,
28
+ email: attributes.email,
29
+ emailVerified: attributes["email_verified"]
30
+ },
31
+ tokens: {
32
+ idToken: signInUserSession.idToken.jwtToken,
33
+ accessToken: signInUserSession.accessToken.jwtToken,
34
+ refreshToken: signInUserSession.refreshToken.token
35
+ },
36
+ isAuthenticated: true
31
37
  });
32
38
  }).catch(() => {
33
- setUser(null);
34
- setTokens(null);
39
+ setAuthState({
40
+ user: null,
41
+ tokens: null,
42
+ isAuthenticated: false
43
+ });
35
44
  });
36
45
  };
37
46
  const updateUserListener = Hub.listen("auth", updateUser);
@@ -40,13 +49,14 @@ var AuthProvider = ({ children }) => {
40
49
  updateUserListener();
41
50
  };
42
51
  }, []);
43
- const isAuthenticated = !!user;
52
+ if (isAuthenticated === void 0) {
53
+ return null;
54
+ }
44
55
  return /* @__PURE__ */ jsx(AuthContext.Provider, { value: { signOut, isAuthenticated, user, tokens }, children });
45
56
  };
46
57
  var useAuth = () => {
47
58
  return React.useContext(AuthContext);
48
59
  };
49
- var AuthProvider_default = AuthProvider;
50
60
 
51
61
  // src/Auth.tsx
52
62
  import * as React3 from "react";
@@ -199,6 +209,140 @@ import { Form as Form2, FormFieldInput as FormFieldInput2, useForm as useForm2,
199
209
 
200
210
  // ../cloud-auth/dist/esm/index.js
201
211
  var PASSWORD_MINIMUM_LENGTH = 8;
212
+ var CognitoUserPoolLogicalId = "CognitoUserPool";
213
+ var CognitoUserPoolClientLogicalId = "CognitoUserPoolClient";
214
+ var CognitoIdentityPoolLogicalId = "CognitoIdentityPool";
215
+ var createAuthTemplate = ({
216
+ autoVerifiedAttributes = ["email"],
217
+ identityPool = true,
218
+ roles,
219
+ schema
220
+ } = {}) => {
221
+ const AutoVerifiedAttributes = Array.isArray(autoVerifiedAttributes) && autoVerifiedAttributes.length > 0 ? autoVerifiedAttributes : void 0;
222
+ const template = {
223
+ AWSTemplateFormatVersion: "2010-09-09",
224
+ Resources: {
225
+ [CognitoUserPoolLogicalId]: {
226
+ Type: "AWS::Cognito::UserPool",
227
+ Properties: {
228
+ AutoVerifiedAttributes,
229
+ Policies: {
230
+ PasswordPolicy: {
231
+ MinimumLength: PASSWORD_MINIMUM_LENGTH,
232
+ RequireLowercase: false,
233
+ RequireNumbers: false,
234
+ RequireSymbols: false,
235
+ RequireUppercase: false,
236
+ TemporaryPasswordValidityDays: 30
237
+ }
238
+ },
239
+ Schema: schema,
240
+ UsernameAttributes: ["email"],
241
+ UsernameConfiguration: {
242
+ CaseSensitive: false
243
+ },
244
+ UserPoolName: {
245
+ Ref: "AWS::StackName"
246
+ }
247
+ }
248
+ },
249
+ [CognitoUserPoolClientLogicalId]: {
250
+ Type: "AWS::Cognito::UserPoolClient",
251
+ Properties: {
252
+ SupportedIdentityProviders: ["COGNITO"],
253
+ UserPoolId: {
254
+ Ref: "CognitoUserPool"
255
+ }
256
+ }
257
+ }
258
+ },
259
+ Outputs: {
260
+ Region: {
261
+ Description: "You use this value on Amplify Auth `region`.",
262
+ Value: {
263
+ Ref: "AWS::Region"
264
+ },
265
+ Export: {
266
+ Name: {
267
+ "Fn::Join": [":", [{ Ref: "AWS::StackName" }, "Region"]]
268
+ }
269
+ }
270
+ },
271
+ UserPoolId: {
272
+ Description: "You use this value on Amplify Auth `userPoolId`.",
273
+ Value: {
274
+ Ref: CognitoUserPoolLogicalId
275
+ },
276
+ Export: {
277
+ Name: {
278
+ "Fn::Join": [":", [{ Ref: "AWS::StackName" }, "UserPoolId"]]
279
+ }
280
+ }
281
+ },
282
+ AppClientId: {
283
+ Description: "You use this value on Amplify Auth `userPoolWebClientId`.",
284
+ Value: {
285
+ Ref: CognitoUserPoolClientLogicalId
286
+ },
287
+ Export: {
288
+ Name: {
289
+ "Fn::Join": [":", [{ Ref: "AWS::StackName" }, "AppClientId"]]
290
+ }
291
+ }
292
+ }
293
+ }
294
+ };
295
+ if (identityPool) {
296
+ template.Resources[CognitoIdentityPoolLogicalId] = {
297
+ Type: "AWS::Cognito::IdentityPool",
298
+ Properties: {
299
+ AllowUnauthenticatedIdentities: true,
300
+ CognitoIdentityProviders: [
301
+ {
302
+ ClientId: {
303
+ Ref: CognitoUserPoolClientLogicalId
304
+ },
305
+ ProviderName: {
306
+ "Fn::GetAtt": [CognitoUserPoolLogicalId, "ProviderName"]
307
+ }
308
+ }
309
+ ]
310
+ }
311
+ };
312
+ if (roles) {
313
+ template.Resources.CognitoIdentityPoolRoleAttachment = {
314
+ Type: "AWS::Cognito::IdentityPoolRoleAttachment",
315
+ Properties: {
316
+ IdentityPoolId: {
317
+ Ref: CognitoIdentityPoolLogicalId
318
+ },
319
+ Roles: roles
320
+ }
321
+ };
322
+ }
323
+ if (!template.Outputs) {
324
+ template.Outputs = {};
325
+ }
326
+ template.Outputs.IdentityPoolId = {
327
+ Description: "You use this value on Amplify Auth `identityPoolId`.",
328
+ Value: {
329
+ Ref: CognitoIdentityPoolLogicalId
330
+ },
331
+ Export: {
332
+ Name: {
333
+ "Fn::Join": [
334
+ ":",
335
+ [{ Ref: "AWS::StackName" }, "CognitoIdentityPoolId"]
336
+ ]
337
+ }
338
+ }
339
+ };
340
+ }
341
+ return template;
342
+ };
343
+ createAuthTemplate.CognitoUserPoolLogicalId = CognitoUserPoolLogicalId;
344
+ createAuthTemplate.CognitoUserPoolClientLogicalId = CognitoUserPoolClientLogicalId;
345
+ createAuthTemplate.CognitoIdentityPoolLogicalId = CognitoIdentityPoolLogicalId;
202
346
 
203
347
  // src/AuthSignIn.tsx
204
348
  import { useI18n as useI18n2 } from "@ttoss/react-i18n";
@@ -524,6 +668,6 @@ var Auth2 = ({ logo, fullScreen = true }) => {
524
668
  };
525
669
  export {
526
670
  Auth2 as Auth,
527
- AuthProvider_default as AuthProvider,
671
+ AuthProvider,
528
672
  useAuth
529
673
  };
package/dist/index.d.ts CHANGED
@@ -12,7 +12,7 @@ type Tokens = {
12
12
  } | null;
13
13
  declare const AuthProvider: ({ children }: {
14
14
  children: React.ReactNode;
15
- }) => JSX.Element;
15
+ }) => JSX.Element | null;
16
16
  declare const useAuth: () => {
17
17
  signOut: () => Promise<any>;
18
18
  isAuthenticated: boolean;
package/dist/index.js CHANGED
@@ -28,7 +28,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
28
28
  var src_exports = {};
29
29
  __export(src_exports, {
30
30
  Auth: () => Auth2,
31
- AuthProvider: () => AuthProvider_default,
31
+ AuthProvider: () => AuthProvider,
32
32
  useAuth: () => useAuth
33
33
  });
34
34
  module.exports = __toCommonJS(src_exports);
@@ -47,24 +47,33 @@ var AuthContext = React.createContext({
47
47
  tokens: null
48
48
  });
49
49
  var AuthProvider = ({ children }) => {
50
- const [user, setUser] = React.useState(null);
51
- const [tokens, setTokens] = React.useState(null);
50
+ const [{ user, tokens, isAuthenticated }, setAuthState] = React.useState({
51
+ user: null,
52
+ tokens: null,
53
+ isAuthenticated: void 0
54
+ });
52
55
  React.useEffect(() => {
53
56
  const updateUser = () => {
54
57
  import_aws_amplify.Auth.currentAuthenticatedUser().then(({ attributes, signInUserSession }) => {
55
- setUser({
56
- id: attributes.sub,
57
- email: attributes.email,
58
- emailVerified: attributes["email_verified"]
59
- });
60
- setTokens({
61
- idToken: signInUserSession.idToken.jwtToken,
62
- accessToken: signInUserSession.accessToken.jwtToken,
63
- refreshToken: signInUserSession.refreshToken.token
58
+ setAuthState({
59
+ user: {
60
+ id: attributes.sub,
61
+ email: attributes.email,
62
+ emailVerified: attributes["email_verified"]
63
+ },
64
+ tokens: {
65
+ idToken: signInUserSession.idToken.jwtToken,
66
+ accessToken: signInUserSession.accessToken.jwtToken,
67
+ refreshToken: signInUserSession.refreshToken.token
68
+ },
69
+ isAuthenticated: true
64
70
  });
65
71
  }).catch(() => {
66
- setUser(null);
67
- setTokens(null);
72
+ setAuthState({
73
+ user: null,
74
+ tokens: null,
75
+ isAuthenticated: false
76
+ });
68
77
  });
69
78
  };
70
79
  const updateUserListener = import_aws_amplify.Hub.listen("auth", updateUser);
@@ -73,13 +82,14 @@ var AuthProvider = ({ children }) => {
73
82
  updateUserListener();
74
83
  };
75
84
  }, []);
76
- const isAuthenticated = !!user;
85
+ if (isAuthenticated === void 0) {
86
+ return null;
87
+ }
77
88
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AuthContext.Provider, { value: { signOut, isAuthenticated, user, tokens }, children });
78
89
  };
79
90
  var useAuth = () => {
80
91
  return React.useContext(AuthContext);
81
92
  };
82
- var AuthProvider_default = AuthProvider;
83
93
 
84
94
  // src/Auth.tsx
85
95
  var React3 = __toESM(require("react"));
@@ -232,6 +242,140 @@ var import_forms2 = require("@ttoss/forms");
232
242
 
233
243
  // ../cloud-auth/dist/esm/index.js
234
244
  var PASSWORD_MINIMUM_LENGTH = 8;
245
+ var CognitoUserPoolLogicalId = "CognitoUserPool";
246
+ var CognitoUserPoolClientLogicalId = "CognitoUserPoolClient";
247
+ var CognitoIdentityPoolLogicalId = "CognitoIdentityPool";
248
+ var createAuthTemplate = ({
249
+ autoVerifiedAttributes = ["email"],
250
+ identityPool = true,
251
+ roles,
252
+ schema
253
+ } = {}) => {
254
+ const AutoVerifiedAttributes = Array.isArray(autoVerifiedAttributes) && autoVerifiedAttributes.length > 0 ? autoVerifiedAttributes : void 0;
255
+ const template = {
256
+ AWSTemplateFormatVersion: "2010-09-09",
257
+ Resources: {
258
+ [CognitoUserPoolLogicalId]: {
259
+ Type: "AWS::Cognito::UserPool",
260
+ Properties: {
261
+ AutoVerifiedAttributes,
262
+ Policies: {
263
+ PasswordPolicy: {
264
+ MinimumLength: PASSWORD_MINIMUM_LENGTH,
265
+ RequireLowercase: false,
266
+ RequireNumbers: false,
267
+ RequireSymbols: false,
268
+ RequireUppercase: false,
269
+ TemporaryPasswordValidityDays: 30
270
+ }
271
+ },
272
+ Schema: schema,
273
+ UsernameAttributes: ["email"],
274
+ UsernameConfiguration: {
275
+ CaseSensitive: false
276
+ },
277
+ UserPoolName: {
278
+ Ref: "AWS::StackName"
279
+ }
280
+ }
281
+ },
282
+ [CognitoUserPoolClientLogicalId]: {
283
+ Type: "AWS::Cognito::UserPoolClient",
284
+ Properties: {
285
+ SupportedIdentityProviders: ["COGNITO"],
286
+ UserPoolId: {
287
+ Ref: "CognitoUserPool"
288
+ }
289
+ }
290
+ }
291
+ },
292
+ Outputs: {
293
+ Region: {
294
+ Description: "You use this value on Amplify Auth `region`.",
295
+ Value: {
296
+ Ref: "AWS::Region"
297
+ },
298
+ Export: {
299
+ Name: {
300
+ "Fn::Join": [":", [{ Ref: "AWS::StackName" }, "Region"]]
301
+ }
302
+ }
303
+ },
304
+ UserPoolId: {
305
+ Description: "You use this value on Amplify Auth `userPoolId`.",
306
+ Value: {
307
+ Ref: CognitoUserPoolLogicalId
308
+ },
309
+ Export: {
310
+ Name: {
311
+ "Fn::Join": [":", [{ Ref: "AWS::StackName" }, "UserPoolId"]]
312
+ }
313
+ }
314
+ },
315
+ AppClientId: {
316
+ Description: "You use this value on Amplify Auth `userPoolWebClientId`.",
317
+ Value: {
318
+ Ref: CognitoUserPoolClientLogicalId
319
+ },
320
+ Export: {
321
+ Name: {
322
+ "Fn::Join": [":", [{ Ref: "AWS::StackName" }, "AppClientId"]]
323
+ }
324
+ }
325
+ }
326
+ }
327
+ };
328
+ if (identityPool) {
329
+ template.Resources[CognitoIdentityPoolLogicalId] = {
330
+ Type: "AWS::Cognito::IdentityPool",
331
+ Properties: {
332
+ AllowUnauthenticatedIdentities: true,
333
+ CognitoIdentityProviders: [
334
+ {
335
+ ClientId: {
336
+ Ref: CognitoUserPoolClientLogicalId
337
+ },
338
+ ProviderName: {
339
+ "Fn::GetAtt": [CognitoUserPoolLogicalId, "ProviderName"]
340
+ }
341
+ }
342
+ ]
343
+ }
344
+ };
345
+ if (roles) {
346
+ template.Resources.CognitoIdentityPoolRoleAttachment = {
347
+ Type: "AWS::Cognito::IdentityPoolRoleAttachment",
348
+ Properties: {
349
+ IdentityPoolId: {
350
+ Ref: CognitoIdentityPoolLogicalId
351
+ },
352
+ Roles: roles
353
+ }
354
+ };
355
+ }
356
+ if (!template.Outputs) {
357
+ template.Outputs = {};
358
+ }
359
+ template.Outputs.IdentityPoolId = {
360
+ Description: "You use this value on Amplify Auth `identityPoolId`.",
361
+ Value: {
362
+ Ref: CognitoIdentityPoolLogicalId
363
+ },
364
+ Export: {
365
+ Name: {
366
+ "Fn::Join": [
367
+ ":",
368
+ [{ Ref: "AWS::StackName" }, "CognitoIdentityPoolId"]
369
+ ]
370
+ }
371
+ }
372
+ };
373
+ }
374
+ return template;
375
+ };
376
+ createAuthTemplate.CognitoUserPoolLogicalId = CognitoUserPoolLogicalId;
377
+ createAuthTemplate.CognitoUserPoolClientLogicalId = CognitoUserPoolClientLogicalId;
378
+ createAuthTemplate.CognitoIdentityPoolLogicalId = CognitoIdentityPoolLogicalId;
235
379
 
236
380
  // src/AuthSignIn.tsx
237
381
  var import_react_i18n2 = require("@ttoss/react-i18n");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/react-auth",
3
- "version": "1.2.5",
3
+ "version": "1.2.7",
4
4
  "description": "ttoss authentication module for React apps.",
5
5
  "license": "UNLICENSED",
6
6
  "author": "ttoss",
@@ -22,7 +22,7 @@
22
22
  "sideEffects": false,
23
23
  "typings": "./dist/index.d.ts",
24
24
  "dependencies": {
25
- "@ttoss/forms": "^0.12.1",
25
+ "@ttoss/forms": "^0.13.0",
26
26
  "@xstate/react": "^3.0.1",
27
27
  "xstate": "^4.35.0"
28
28
  },
@@ -34,13 +34,13 @@
34
34
  "react": ">=16.8.0"
35
35
  },
36
36
  "devDependencies": {
37
- "@ttoss/cloud-auth": "^0.3.1",
38
- "@ttoss/config": "^1.26.0",
39
- "@ttoss/i18n-cli": "^0.3.0",
40
- "@ttoss/react-i18n": "^1.18.3",
41
- "@ttoss/react-notifications": "^1.19.4",
42
- "@ttoss/test-utils": "^1.20.0",
43
- "@ttoss/ui": "^1.28.0",
37
+ "@ttoss/cloud-auth": "^0.4.0",
38
+ "@ttoss/config": "^1.27.0",
39
+ "@ttoss/i18n-cli": "^0.3.1",
40
+ "@ttoss/react-i18n": "^1.18.4",
41
+ "@ttoss/react-notifications": "^1.19.5",
42
+ "@ttoss/test-utils": "^1.20.1",
43
+ "@ttoss/ui": "^1.29.0",
44
44
  "aws-amplify": "^5.0.7"
45
45
  },
46
46
  "keywords": [
@@ -50,5 +50,5 @@
50
50
  "publishConfig": {
51
51
  "access": "public"
52
52
  },
53
- "gitHead": "56960002b641a5b5c875d2f35a0171ed2f88d40e"
53
+ "gitHead": "0067587c116050873db5c57eb124fe38ec0bc9d2"
54
54
  }
@@ -29,30 +29,41 @@ const AuthContext = React.createContext<{
29
29
  tokens: null,
30
30
  });
31
31
 
32
- const AuthProvider = ({ children }: { children: React.ReactNode }) => {
33
- const [user, setUser] = React.useState<User>(null);
34
-
35
- const [tokens, setTokens] = React.useState<Tokens>(null);
32
+ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
33
+ const [{ user, tokens, isAuthenticated }, setAuthState] = React.useState<{
34
+ user: User;
35
+ tokens: Tokens;
36
+ isAuthenticated: boolean | undefined;
37
+ }>({
38
+ user: null,
39
+ tokens: null,
40
+ isAuthenticated: undefined,
41
+ });
36
42
 
37
43
  React.useEffect(() => {
38
44
  const updateUser = () => {
39
45
  Auth.currentAuthenticatedUser()
40
46
  .then(({ attributes, signInUserSession }) => {
41
- setUser({
42
- id: attributes.sub,
43
- email: attributes.email,
44
- emailVerified: attributes['email_verified'],
45
- });
46
-
47
- setTokens({
48
- idToken: signInUserSession.idToken.jwtToken,
49
- accessToken: signInUserSession.accessToken.jwtToken,
50
- refreshToken: signInUserSession.refreshToken.token,
47
+ setAuthState({
48
+ user: {
49
+ id: attributes.sub,
50
+ email: attributes.email,
51
+ emailVerified: attributes['email_verified'],
52
+ },
53
+ tokens: {
54
+ idToken: signInUserSession.idToken.jwtToken,
55
+ accessToken: signInUserSession.accessToken.jwtToken,
56
+ refreshToken: signInUserSession.refreshToken.token,
57
+ },
58
+ isAuthenticated: true,
51
59
  });
52
60
  })
53
61
  .catch(() => {
54
- setUser(null);
55
- setTokens(null);
62
+ setAuthState({
63
+ user: null,
64
+ tokens: null,
65
+ isAuthenticated: false,
66
+ });
56
67
  });
57
68
  };
58
69
 
@@ -68,7 +79,9 @@ const AuthProvider = ({ children }: { children: React.ReactNode }) => {
68
79
  };
69
80
  }, []);
70
81
 
71
- const isAuthenticated = !!user;
82
+ if (isAuthenticated === undefined) {
83
+ return null;
84
+ }
72
85
 
73
86
  return (
74
87
  <AuthContext.Provider value={{ signOut, isAuthenticated, user, tokens }}>
@@ -80,5 +93,3 @@ const AuthProvider = ({ children }: { children: React.ReactNode }) => {
80
93
  export const useAuth = () => {
81
94
  return React.useContext(AuthContext);
82
95
  };
83
-
84
- export default AuthProvider;
package/src/index.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { default as AuthProvider, useAuth } from './AuthProvider';
1
+ export { AuthProvider, useAuth } from './AuthProvider';
2
2
  export { Auth } from './Auth';
3
3
  export * from './types';