@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 +26 -16
- package/dist/index.d.ts +1 -1
- package/dist/index.js +26 -16
- package/package.json +2 -2
- package/src/AuthProvider.tsx +30 -19
- package/src/index.ts +1 -1
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,
|
|
18
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
34
|
-
|
|
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
|
-
|
|
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
|
-
|
|
671
|
+
AuthProvider,
|
|
662
672
|
useAuth
|
|
663
673
|
};
|
package/dist/index.d.ts
CHANGED
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: () =>
|
|
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,
|
|
51
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
67
|
-
|
|
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
|
-
|
|
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.
|
|
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": "
|
|
53
|
+
"gitHead": "0067587c116050873db5c57eb124fe38ec0bc9d2"
|
|
54
54
|
}
|
package/src/AuthProvider.tsx
CHANGED
|
@@ -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,
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
55
|
-
|
|
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
|
-
|
|
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