@ttoss/react-auth 1.2.6 → 1.2.8

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";
@@ -206,9 +216,10 @@ var createAuthTemplate = ({
206
216
  autoVerifiedAttributes = ["email"],
207
217
  identityPool = true,
208
218
  roles,
209
- schema
219
+ schema,
220
+ usernameAttributes = ["email"]
210
221
  } = {}) => {
211
- const AutoVerifiedAttributes = Array.isArray(autoVerifiedAttributes) && autoVerifiedAttributes.length > 0 ? autoVerifiedAttributes : void 0;
222
+ const AutoVerifiedAttributes = Array.isArray(autoVerifiedAttributes) && autoVerifiedAttributes.length > 0 ? autoVerifiedAttributes : [];
212
223
  const template = {
213
224
  AWSTemplateFormatVersion: "2010-09-09",
214
225
  Resources: {
@@ -227,7 +238,7 @@ var createAuthTemplate = ({
227
238
  }
228
239
  },
229
240
  Schema: schema,
230
- UsernameAttributes: ["email"],
241
+ UsernameAttributes: usernameAttributes,
231
242
  UsernameConfiguration: {
232
243
  CaseSensitive: false
233
244
  },
@@ -658,6 +669,6 @@ var Auth2 = ({ logo, fullScreen = true }) => {
658
669
  };
659
670
  export {
660
671
  Auth2 as Auth,
661
- AuthProvider_default as AuthProvider,
672
+ AuthProvider,
662
673
  useAuth
663
674
  };
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"));
@@ -239,9 +249,10 @@ var createAuthTemplate = ({
239
249
  autoVerifiedAttributes = ["email"],
240
250
  identityPool = true,
241
251
  roles,
242
- schema
252
+ schema,
253
+ usernameAttributes = ["email"]
243
254
  } = {}) => {
244
- const AutoVerifiedAttributes = Array.isArray(autoVerifiedAttributes) && autoVerifiedAttributes.length > 0 ? autoVerifiedAttributes : void 0;
255
+ const AutoVerifiedAttributes = Array.isArray(autoVerifiedAttributes) && autoVerifiedAttributes.length > 0 ? autoVerifiedAttributes : [];
245
256
  const template = {
246
257
  AWSTemplateFormatVersion: "2010-09-09",
247
258
  Resources: {
@@ -260,7 +271,7 @@ var createAuthTemplate = ({
260
271
  }
261
272
  },
262
273
  Schema: schema,
263
- UsernameAttributes: ["email"],
274
+ UsernameAttributes: usernameAttributes,
264
275
  UsernameConfiguration: {
265
276
  CaseSensitive: false
266
277
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/react-auth",
3
- "version": "1.2.6",
3
+ "version": "1.2.8",
4
4
  "description": "ttoss authentication module for React apps.",
5
5
  "license": "UNLICENSED",
6
6
  "author": "ttoss",
@@ -34,7 +34,7 @@
34
34
  "react": ">=16.8.0"
35
35
  },
36
36
  "devDependencies": {
37
- "@ttoss/cloud-auth": "^0.4.0",
37
+ "@ttoss/cloud-auth": "^0.5.0",
38
38
  "@ttoss/config": "^1.27.0",
39
39
  "@ttoss/i18n-cli": "^0.3.1",
40
40
  "@ttoss/react-i18n": "^1.18.4",
@@ -50,5 +50,5 @@
50
50
  "publishConfig": {
51
51
  "access": "public"
52
52
  },
53
- "gitHead": "2bf304efc726a34b95859bb9b6fe8749c1a16844"
53
+ "gitHead": "6c5898425e2847fed46511ae1e573ff8dc3eb1d4"
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';