@ttoss/react-auth 1.2.6 → 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";
@@ -658,6 +668,6 @@ var Auth2 = ({ logo, fullScreen = true }) => {
658
668
  };
659
669
  export {
660
670
  Auth2 as Auth,
661
- AuthProvider_default as AuthProvider,
671
+ AuthProvider,
662
672
  useAuth
663
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"));
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.7",
4
4
  "description": "ttoss authentication module for React apps.",
5
5
  "license": "UNLICENSED",
6
6
  "author": "ttoss",
@@ -50,5 +50,5 @@
50
50
  "publishConfig": {
51
51
  "access": "public"
52
52
  },
53
- "gitHead": "2bf304efc726a34b95859bb9b6fe8749c1a16844"
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';