datacenter-lib-react-ts 1.0.0

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.
Files changed (39) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +3 -0
  3. package/dist/cjs/auth/authCheck.js +51 -0
  4. package/dist/cjs/auth/authCheck.js.map +1 -0
  5. package/dist/cjs/auth/authRequired.js +27 -0
  6. package/dist/cjs/auth/authRequired.js.map +1 -0
  7. package/dist/cjs/auth/authUtils.js +8 -0
  8. package/dist/cjs/auth/authUtils.js.map +1 -0
  9. package/dist/cjs/auth/authentication.slice.js +72 -0
  10. package/dist/cjs/auth/authentication.slice.js.map +1 -0
  11. package/dist/cjs/auth/userOptions.js +35 -0
  12. package/dist/cjs/auth/userOptions.js.map +1 -0
  13. package/dist/cjs/index.js +19 -0
  14. package/dist/cjs/index.js.map +1 -0
  15. package/dist/esm/auth/authCheck.js +46 -0
  16. package/dist/esm/auth/authCheck.js.map +1 -0
  17. package/dist/esm/auth/authRequired.js +22 -0
  18. package/dist/esm/auth/authRequired.js.map +1 -0
  19. package/dist/esm/auth/authUtils.js +5 -0
  20. package/dist/esm/auth/authUtils.js.map +1 -0
  21. package/dist/esm/auth/authentication.slice.js +68 -0
  22. package/dist/esm/auth/authentication.slice.js.map +1 -0
  23. package/dist/esm/auth/userOptions.js +30 -0
  24. package/dist/esm/auth/userOptions.js.map +1 -0
  25. package/dist/esm/index.js +7 -0
  26. package/dist/esm/index.js.map +1 -0
  27. package/dist/types/auth/authCheck.d.ts +8 -0
  28. package/dist/types/auth/authRequired.d.ts +9 -0
  29. package/dist/types/auth/authUtils.d.ts +4 -0
  30. package/dist/types/auth/authentication.slice.d.ts +35 -0
  31. package/dist/types/auth/userOptions.d.ts +8 -0
  32. package/dist/types/index.d.ts +6 -0
  33. package/package.json +71 -0
  34. package/src/auth/authCheck.tsx +58 -0
  35. package/src/auth/authRequired.tsx +35 -0
  36. package/src/auth/authUtils.tsx +4 -0
  37. package/src/auth/authentication.slice.tsx +97 -0
  38. package/src/auth/userOptions.tsx +54 -0
  39. package/src/index.tsx +13 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 joabssilveira
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # datacenter-lib-react-ts
2
+
3
+ Library intended for applications that require datacenter react ui resources in typescript
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthCheckComponent = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const react_1 = tslib_1.__importStar(require("react"));
6
+ const react_router_dom_1 = require("react-router-dom");
7
+ const authentication_slice_1 = require("./authentication.slice");
8
+ const authUtils_1 = require("./authUtils");
9
+ const react_router_dom_2 = require("react-router-dom");
10
+ const react_redux_1 = require("react-redux");
11
+ const datacenter_lib_common_ts_1 = require("datacenter-lib-common-ts");
12
+ const AuthCheckComponent = ({ authenticationState, /*authUrlBase, redirectUrl,*/ baseApiUrl, children }) => {
13
+ const navigate = (0, react_router_dom_2.useNavigate)();
14
+ const location = (0, react_router_dom_1.useLocation)();
15
+ const dispatch = (0, react_redux_1.useDispatch)();
16
+ const queryParams = new URLSearchParams(location.search);
17
+ const authUuid = queryParams.get(authUtils_1.AuthUtils.authUuidQueryStringName);
18
+ const onGetAuthFinish = () => {
19
+ queryParams.delete(authUtils_1.AuthUtils.authUuidQueryStringName);
20
+ const newSearch = queryParams.toString();
21
+ const newPath = `${location.pathname}${newSearch ? `?${newSearch}` : ''}`;
22
+ navigate(newPath, { replace: true });
23
+ };
24
+ (0, react_1.useEffect)(() => {
25
+ if (!authenticationState.payload && !authenticationState.options?.loading) {
26
+ if (authUuid) {
27
+ dispatch((0, authentication_slice_1.authenticationStateLoadFromApi)({
28
+ baseApiUrl,
29
+ requestPayload: {
30
+ type: datacenter_lib_common_ts_1.AuthenticationType.uuid,
31
+ uuid: authUuid
32
+ },
33
+ onSuccess: onGetAuthFinish,
34
+ onError: onGetAuthFinish
35
+ }));
36
+ }
37
+ }
38
+ else if (authenticationState.payload && authUuid) {
39
+ // caso o usuario ja tenha logado e tente acessar a pagina de autenticacao, o redirect manda o authUuid
40
+ // nesse cenario, deve-se eliminar o authUuid da url
41
+ // isso vale para autenticacao em um dominio diferente do dominio do autenticador
42
+ // para recursos no mesmo dominio do autenticador, o redirect quando o usuario já tem autenticado esta correto, ou seja,
43
+ // ele nao manda o authUuid caso o usuario já esteja logado, em dominio diferentes nao tem como o autenticador saber se o app esta logado
44
+ onGetAuthFinish();
45
+ }
46
+ // }, [authenticationState, location]);
47
+ }, [authenticationState]);
48
+ return react_1.default.createElement(react_1.default.Fragment, null, children);
49
+ };
50
+ exports.AuthCheckComponent = AuthCheckComponent;
51
+ //# sourceMappingURL=authCheck.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authCheck.js","sourceRoot":"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,IAAI,EAAE,QAAQ;qBACuB;oBACvC,SAAS,EAAE,eAAe;oBAC1B,OAAO,EAAE,eAAe;iBACzB,CAAC,CAAC,CAAA;YACL,CAAC;QACH,CAAC;aACI,IAAI,mBAAmB,CAAC,OAAO,IAAI,QAAQ,EAAE,CAAC;YACjD,uGAAuG;YACvG,oDAAoD;YACpD,iFAAiF;YACjF,yHAAyH;YACzH,yIAAyI;YACzI,eAAe,EAAE,CAAA;QACnB,CAAC;QACD,uCAAuC;IACzC,CAAC,EAAE,CAAC,mBAAmB,CAAC,CAAC,CAAC;IAE1B,OAAO,8DAAG,QAAQ,CAAI,CAAA;AACxB,CAAC,CAAC;AAxCW,QAAA,kBAAkB,sBAwC7B"}
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthRequiredComponent = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const react_1 = tslib_1.__importStar(require("react"));
6
+ const react_router_dom_1 = require("react-router-dom");
7
+ const authUtils_1 = require("./authUtils");
8
+ const AuthRequiredComponent = ({ authenticationState, authUrlBase, redirectUrl, /*baseApiUrl,*/ children }) => {
9
+ const location = (0, react_router_dom_1.useLocation)();
10
+ const queryParams = new URLSearchParams(location.search);
11
+ const authUuid = queryParams.get(authUtils_1.AuthUtils.authUuidQueryStringName);
12
+ (0, react_1.useEffect)(() => {
13
+ if (!authenticationState.payload && !authenticationState.options?.loading) {
14
+ if (!authUuid) {
15
+ if (!authUrlBase.includes('/login'))
16
+ authUrlBase = `${authUrlBase}/login`;
17
+ window.location.href = `${authUrlBase}?${authUtils_1.AuthUtils.authToRedirectQueryStringName}=${encodeURIComponent(redirectUrl)}`;
18
+ }
19
+ }
20
+ }, [authenticationState]);
21
+ if (!authenticationState.payload) {
22
+ return null;
23
+ }
24
+ return react_1.default.createElement(react_1.default.Fragment, null, children);
25
+ };
26
+ exports.AuthRequiredComponent = AuthRequiredComponent;
27
+ //# sourceMappingURL=authRequired.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authRequired.js","sourceRoot":"","sources":["../../../src/auth/authRequired.tsx"],"names":[],"mappings":";;;;AACA,uDAAoD;AACpD,uDAA+C;AAE/C,2CAAwC;AAUjC,MAAM,qBAAqB,GAA0C,CAAC,EAAE,mBAAmB,EAAE,WAAW,EAAE,WAAW,EAAE,eAAe,CAAC,QAAQ,EAAE,EAAE,EAAE;IAC1J,MAAM,QAAQ,GAAG,IAAA,8BAAW,GAAE,CAAA;IAC9B,MAAM,WAAW,GAAG,IAAI,eAAe,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAA;IACxD,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,qBAAS,CAAC,uBAAuB,CAAC,CAAA;IAEnE,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,IAAI,CAAC,mBAAmB,CAAC,OAAO,IAAI,CAAC,mBAAmB,CAAC,OAAO,EAAE,OAAO,EAAE,CAAC;YAC1E,IAAI,CAAC,QAAQ,EAAE,CAAC;gBACd,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,QAAQ,CAAC;oBACjC,WAAW,GAAG,GAAG,WAAW,QAAQ,CAAA;gBACtC,MAAM,CAAC,QAAQ,CAAC,IAAI,GAAG,GAAG,WAAW,IAAI,qBAAS,CAAC,6BAA6B,IAAI,kBAAkB,CAAC,WAAW,CAAC,EAAE,CAAA;YACvH,CAAC;QACH,CAAC;IACH,CAAC,EAAE,CAAC,mBAAmB,CAAC,CAAC,CAAC;IAE1B,IAAI,CAAC,mBAAmB,CAAC,OAAO,EAAE,CAAC;QACjC,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,8DAAG,QAAQ,CAAI,CAAA;AACxB,CAAC,CAAC;AApBW,QAAA,qBAAqB,yBAoBhC"}
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthUtils = void 0;
4
+ exports.AuthUtils = {
5
+ authUuidQueryStringName: 'au',
6
+ authToRedirectQueryStringName: 'to',
7
+ };
8
+ //# sourceMappingURL=authUtils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authUtils.js","sourceRoot":"","sources":["../../../src/auth/authUtils.tsx"],"names":[],"mappings":";;;AAAa,QAAA,SAAS,GAAG;IACvB,uBAAuB,EAAE,IAAI;IAC7B,6BAA6B,EAAE,IAAI;CACpC,CAAA"}
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var _a;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.authenticationStateSet = exports.authenticationStateLogout = exports.authenticationSlice = exports.authenticationStateLoadFromApi = void 0;
5
+ const toolkit_1 = require("@reduxjs/toolkit");
6
+ const datacenter_lib_common_ts_1 = require("datacenter-lib-common-ts");
7
+ const src_1 = require("fwork-jsts-common/src");
8
+ const jwt_decode_1 = require("jwt-decode"); // dont use jsonwebtokens package here, its only for node projects
9
+ const cookie = src_1.WebUtils.getCookie('@authenticationState');
10
+ const cookieObj = cookie ? JSON.parse(cookie) : null;
11
+ const initState = cookieObj || {};
12
+ exports.authenticationStateLoadFromApi = (0, toolkit_1.createAsyncThunk)('authenticationStateLoadFromApi', async (arg) => {
13
+ try {
14
+ let result = {};
15
+ const response = await new datacenter_lib_common_ts_1.AuthenticationsApiClient({ baseApiUrl: arg.baseApiUrl, }).post({
16
+ data: arg.requestPayload,
17
+ });
18
+ if (response?.success && response.data?.token) {
19
+ if (arg.onSuccess)
20
+ arg.onSuccess(response.data);
21
+ var decToken = (0, jwt_decode_1.jwtDecode)(response.data.token);
22
+ result.payload = {
23
+ ...response.data,
24
+ user: decToken.user
25
+ };
26
+ }
27
+ else if (arg.onError)
28
+ arg.onError(response?.msg);
29
+ return result;
30
+ }
31
+ catch (error) {
32
+ if (arg.onError)
33
+ arg.onError('Erro ao tentar autenticar');
34
+ return;
35
+ }
36
+ });
37
+ exports.authenticationSlice = (0, toolkit_1.createSlice)({
38
+ name: 'authenticationSlice',
39
+ initialState: initState,
40
+ reducers: {
41
+ set: (state, action) => {
42
+ state.payload = action.payload.payload;
43
+ src_1.WebUtils.setCookie('@authenticationState', JSON.stringify(state), 1);
44
+ },
45
+ logout: (state) => {
46
+ state.options = undefined;
47
+ state.payload = undefined;
48
+ src_1.WebUtils.setCookie('@authenticationState', '', -1);
49
+ },
50
+ },
51
+ extraReducers: (builder) => {
52
+ builder.addCase(exports.authenticationStateLoadFromApi.pending, (state) => {
53
+ if (!state.options)
54
+ state.options = {};
55
+ state.options.loading = true;
56
+ });
57
+ builder.addCase(exports.authenticationStateLoadFromApi.fulfilled, (state, response) => {
58
+ if (state.options)
59
+ state.options.loading = false;
60
+ state.payload = response.payload?.payload;
61
+ if (state.payload)
62
+ src_1.WebUtils.setCookie('@authenticationState', JSON.stringify(state), 1);
63
+ });
64
+ builder.addCase(exports.authenticationStateLoadFromApi.rejected, (state) => {
65
+ if (state.options)
66
+ state.options.loading = false;
67
+ });
68
+ }
69
+ });
70
+ _a = exports.authenticationSlice.actions, exports.authenticationStateLogout = _a.logout, exports.authenticationStateSet = _a.set;
71
+ exports.default = exports.authenticationSlice.reducer;
72
+ //# sourceMappingURL=authentication.slice.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authentication.slice.js","sourceRoot":"","sources":["../../../src/auth/authentication.slice.tsx"],"names":[],"mappings":";;;;AAAA,8CAAiE;AACjE,uEAAwO;AACxO,+CAAiD;AACjD,2CAAuC,CAAC,kEAAkE;AAa1G,MAAM,MAAM,GAAG,cAAQ,CAAC,SAAS,CAAC,sBAAsB,CAAC,CAAA;AACzD,MAAM,SAAS,GAAG,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;AAEpD,MAAM,SAAS,GAAyB,SAAS,IAAI,EAAE,CAAA;AAE1C,QAAA,8BAA8B,GAAG,IAAA,0BAAgB,EAC5D,gCAAgC,EAChC,KAAK,EAAE,GAON,EAAE,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e,2BAAmB,CAAC,OAAO,CAAA"}
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UserOptionsComponent = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const Logout_1 = tslib_1.__importDefault(require("@mui/icons-material/Logout"));
6
+ const material_1 = require("@mui/material");
7
+ const react_1 = tslib_1.__importStar(require("react"));
8
+ const react_redux_1 = require("react-redux");
9
+ const authentication_slice_1 = require("./authentication.slice");
10
+ const UserOptionsComponent = ({ user, datacenterAuthBaseUrl, onLogOut }) => {
11
+ const dispatch = (0, react_redux_1.useDispatch)();
12
+ const [menuAnchor, setAnchorElemnt] = (0, react_1.useState)(null);
13
+ const openMenu = Boolean(menuAnchor);
14
+ return react_1.default.createElement(react_1.default.Fragment, null,
15
+ react_1.default.createElement(material_1.Menu, { id: "basic-menu", anchorEl: menuAnchor, open: openMenu, onClose: () => setAnchorElemnt(null), MenuListProps: {
16
+ 'aria-labelledby': 'basic-button',
17
+ } },
18
+ react_1.default.createElement(material_1.MenuItem, { style: { display: 'flex', gap: 10 }, onClick: () => {
19
+ let authUrl = `${datacenterAuthBaseUrl}/profile`;
20
+ window.open(authUrl, '_blank')?.focus();
21
+ setAnchorElemnt(null);
22
+ } }, user.name),
23
+ react_1.default.createElement(material_1.MenuItem, { style: { display: 'flex', gap: 10 }, onClick: () => {
24
+ dispatch((0, authentication_slice_1.authenticationStateLogout)());
25
+ if (onLogOut)
26
+ onLogOut();
27
+ setAnchorElemnt(null);
28
+ } },
29
+ react_1.default.createElement(Logout_1.default, null),
30
+ " Sair")),
31
+ react_1.default.createElement(material_1.IconButton, { onClick: e => setAnchorElemnt(e.currentTarget) },
32
+ react_1.default.createElement(material_1.Avatar, { src: user.imageUrl })));
33
+ };
34
+ exports.UserOptionsComponent = UserOptionsComponent;
35
+ //# sourceMappingURL=userOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"userOptions.js","sourceRoot":"","sources":["../../../src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OAAG;wBACL,CACN;QAEP,8BAAC,qBAAU,IACT,OAAO,EAAE,CAAC,CAAC,EAAE,CAAC,eAAe,CAAC,CAAC,CAAC,aAAa,CAAC;YAC/C,8BAAC,iBAAM,IAAC,GAAG,EAAE,IAAI,CAAC,QAAQ,GAAI,CAAa,CAC3C,CAAA;AACL,CAAC,CAAA;AAxCY,QAAA,oBAAoB,wBAwChC"}
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UserOptionsComponent = exports.AuthUtils = exports.AuthRequiredComponent = exports.authenticationStateSet = exports.authenticationSlice = exports.authenticationStateLogout = exports.authenticationStateLoadFromApi = exports.authenticationReducer = exports.AuthCheckComponent = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const authCheck_1 = require("./auth/authCheck");
6
+ Object.defineProperty(exports, "AuthCheckComponent", { enumerable: true, get: function () { return authCheck_1.AuthCheckComponent; } });
7
+ const authentication_slice_1 = tslib_1.__importStar(require("./auth/authentication.slice"));
8
+ exports.authenticationReducer = authentication_slice_1.default;
9
+ Object.defineProperty(exports, "authenticationStateLoadFromApi", { enumerable: true, get: function () { return authentication_slice_1.authenticationStateLoadFromApi; } });
10
+ Object.defineProperty(exports, "authenticationStateLogout", { enumerable: true, get: function () { return authentication_slice_1.authenticationStateLogout; } });
11
+ Object.defineProperty(exports, "authenticationSlice", { enumerable: true, get: function () { return authentication_slice_1.authenticationSlice; } });
12
+ Object.defineProperty(exports, "authenticationStateSet", { enumerable: true, get: function () { return authentication_slice_1.authenticationStateSet; } });
13
+ const authRequired_1 = require("./auth/authRequired");
14
+ Object.defineProperty(exports, "AuthRequiredComponent", { enumerable: true, get: function () { return authRequired_1.AuthRequiredComponent; } });
15
+ const authUtils_1 = require("./auth/authUtils");
16
+ Object.defineProperty(exports, "AuthUtils", { enumerable: true, get: function () { return authUtils_1.AuthUtils; } });
17
+ const userOptions_1 = require("./auth/userOptions");
18
+ Object.defineProperty(exports, "UserOptionsComponent", { enumerable: true, get: function () { return userOptions_1.UserOptionsComponent; } });
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":";;;;AAAA,gDAAgF;AAO9E,mGAPO,8BAAkB,OAOP;AANpB,4FAAsN;AAOpN,gCAPK,8BAAqB,CAOL;AAAE,+GAPO,qDAA8B,OAOP;AAA4C,0GAPO,gDAAyB,OAOP;AAAE,oGAPO,0CAAmB,OAOP;AAAE,uGAPO,6CAAsB,OAOP;AANzK,sDAAyF;AAOvF,sGAPO,oCAAqB,OAOP;AANvB,gDAA6C;AAO3C,0FAPO,qBAAS,OAOP;AANX,oDAAsF;AAOpF,qGAPO,kCAAoB,OAOP"}
@@ -0,0 +1,46 @@
1
+ import React, { useEffect } from "react";
2
+ import { useLocation } from "react-router-dom";
3
+ import { authenticationStateLoadFromApi } from "./authentication.slice";
4
+ import { AuthUtils } from "./authUtils";
5
+ import { useNavigate } from 'react-router-dom';
6
+ import { useDispatch } from "react-redux";
7
+ import { AuthenticationType } from "datacenter-lib-common-ts";
8
+ export const AuthCheckComponent = ({ authenticationState, /*authUrlBase, redirectUrl,*/ baseApiUrl, children }) => {
9
+ const navigate = useNavigate();
10
+ const location = useLocation();
11
+ const dispatch = useDispatch();
12
+ const queryParams = new URLSearchParams(location.search);
13
+ const authUuid = queryParams.get(AuthUtils.authUuidQueryStringName);
14
+ const onGetAuthFinish = () => {
15
+ queryParams.delete(AuthUtils.authUuidQueryStringName);
16
+ const newSearch = queryParams.toString();
17
+ const newPath = `${location.pathname}${newSearch ? `?${newSearch}` : ''}`;
18
+ navigate(newPath, { replace: true });
19
+ };
20
+ useEffect(() => {
21
+ if (!authenticationState.payload && !authenticationState.options?.loading) {
22
+ if (authUuid) {
23
+ dispatch(authenticationStateLoadFromApi({
24
+ baseApiUrl,
25
+ requestPayload: {
26
+ type: AuthenticationType.uuid,
27
+ uuid: authUuid
28
+ },
29
+ onSuccess: onGetAuthFinish,
30
+ onError: onGetAuthFinish
31
+ }));
32
+ }
33
+ }
34
+ else if (authenticationState.payload && authUuid) {
35
+ // caso o usuario ja tenha logado e tente acessar a pagina de autenticacao, o redirect manda o authUuid
36
+ // nesse cenario, deve-se eliminar o authUuid da url
37
+ // isso vale para autenticacao em um dominio diferente do dominio do autenticador
38
+ // para recursos no mesmo dominio do autenticador, o redirect quando o usuario já tem autenticado esta correto, ou seja,
39
+ // ele nao manda o authUuid caso o usuario já esteja logado, em dominio diferentes nao tem como o autenticador saber se o app esta logado
40
+ onGetAuthFinish();
41
+ }
42
+ // }, [authenticationState, location]);
43
+ }, [authenticationState]);
44
+ return React.createElement(React.Fragment, null, children);
45
+ };
46
+ //# sourceMappingURL=authCheck.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authCheck.js","sourceRoot":"","sources":["../../../src/auth/authCheck.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,EAAa,SAAS,EAAE,MAAM,OAAO,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,8BAA8B,EAAwB,MAAM,wBAAwB,CAAC;AAC9F,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,kBAAkB,EAAsC,MAAM,0BAA0B,CAAC;AAUlG,MAAM,CAAC,MAAM,kBAAkB,GAAuC,CAAC,EAAE,mBAAmB,EAAE,6BAA6B,CAAC,UAAU,EAAE,QAAQ,EAAE,EAAE,EAAE;IACpJ,MAAM,QAAQ,GAAG,WAAW,EAAE,CAAA;IAC9B,MAAM,QAAQ,GAAG,WAAW,EAAE,CAAA;IAC9B,MAAM,QAAQ,GAAG,WAAW,EAAO,CAAA;IACnC,MAAM,WAAW,GAAG,IAAI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2
+ import { useLocation } from "react-router-dom";
3
+ import { AuthUtils } from "./authUtils";
4
+ export const AuthRequiredComponent = ({ authenticationState, authUrlBase, redirectUrl, /*baseApiUrl,*/ children }) => {
5
+ const location = useLocation();
6
+ const queryParams = new URLSearchParams(location.search);
7
+ const authUuid = queryParams.get(AuthUtils.authUuidQueryStringName);
8
+ useEffect(() => {
9
+ if (!authenticationState.payload && !authenticationState.options?.loading) {
10
+ if (!authUuid) {
11
+ if (!authUrlBase.includes('/login'))
12
+ authUrlBase = `${authUrlBase}/login`;
13
+ window.location.href = `${authUrlBase}?${AuthUtils.authToRedirectQueryStringName}=${encodeURIComponent(redirectUrl)}`;
14
+ }
15
+ }
16
+ }, [authenticationState]);
17
+ if (!authenticationState.payload) {
18
+ return null;
19
+ }
20
+ return React.createElement(React.Fragment, null, children);
21
+ };
22
+ //# sourceMappingURL=authRequired.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authRequired.js","sourceRoot":"","sources":["../../../src/auth/authRequired.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,EAAa,SAAS,EAAE,MAAM,OAAO,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAUxC,MAAM,CAAC,MAAM,qBAAqB,GAA0C,CAAC,EAAE,mBAAmB,EAAE,WAAW,EAAE,WAAW,EAAE,eAAe,CAAC,QAAQ,EAAE,EAAE,EAAE;IAC1J,MAAM,QAAQ,GAAG,WAAW,EAAE,CAAA;IAC9B,MAAM,WAAW,GAAG,IAAI,eAAe,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAA;IACxD,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,SAAS,CAAC,uBAAuB,CAAC,CAAA;IAEnE,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,CAAC,mBAAmB,CAAC,OAAO,IAAI,CAAC,mBAAmB,CAAC,OAAO,EAAE,OAAO,EAAE,CAAC;YAC1E,IAAI,CAAC,QAAQ,EAAE,CAAC;gBACd,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,QAAQ,CAAC;oBACjC,WAAW,GAAG,GAAG,WAAW,QAAQ,CAAA;gBACtC,MAAM,CAAC,QAAQ,CAAC,IAAI,GAAG,GAAG,WAAW,IAAI,SAAS,CAAC,6BAA6B,IAAI,kBAAkB,CAAC,WAAW,CAAC,EAAE,CAAA;YACvH,CAAC;QACH,CAAC;IACH,CAAC,EAAE,CAAC,mBAAmB,CAAC,CAAC,CAAC;IAE1B,IAAI,CAAC,mBAAmB,CAAC,OAAO,EAAE,CAAC;QACjC,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,0CAAG,QAAQ,CAAI,CAAA;AACxB,CAAC,CAAC"}
@@ -0,0 +1,5 @@
1
+ export const AuthUtils = {
2
+ authUuidQueryStringName: 'au',
3
+ authToRedirectQueryStringName: 'to',
4
+ };
5
+ //# sourceMappingURL=authUtils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authUtils.js","sourceRoot":"","sources":["../../../src/auth/authUtils.tsx"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB,uBAAuB,EAAE,IAAI;IAC7B,6BAA6B,EAAE,IAAI;CACpC,CAAA"}
@@ -0,0 +1,68 @@
1
+ import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
2
+ import { AuthenticationsApiClient } from 'datacenter-lib-common-ts';
3
+ import { WebUtils } from 'fwork-jsts-common/src';
4
+ import { jwtDecode } from 'jwt-decode'; // dont use jsonwebtokens package here, its only for node projects
5
+ const cookie = WebUtils.getCookie('@authenticationState');
6
+ const cookieObj = cookie ? JSON.parse(cookie) : null;
7
+ const initState = cookieObj || {};
8
+ export const authenticationStateLoadFromApi = createAsyncThunk('authenticationStateLoadFromApi', async (arg) => {
9
+ try {
10
+ let result = {};
11
+ const response = await new AuthenticationsApiClient({ baseApiUrl: arg.baseApiUrl, }).post({
12
+ data: arg.requestPayload,
13
+ });
14
+ if (response?.success && response.data?.token) {
15
+ if (arg.onSuccess)
16
+ arg.onSuccess(response.data);
17
+ var decToken = jwtDecode(response.data.token);
18
+ result.payload = {
19
+ ...response.data,
20
+ user: decToken.user
21
+ };
22
+ }
23
+ else if (arg.onError)
24
+ arg.onError(response?.msg);
25
+ return result;
26
+ }
27
+ catch (error) {
28
+ if (arg.onError)
29
+ arg.onError('Erro ao tentar autenticar');
30
+ return;
31
+ }
32
+ });
33
+ export const authenticationSlice = createSlice({
34
+ name: 'authenticationSlice',
35
+ initialState: initState,
36
+ reducers: {
37
+ set: (state, action) => {
38
+ state.payload = action.payload.payload;
39
+ WebUtils.setCookie('@authenticationState', JSON.stringify(state), 1);
40
+ },
41
+ logout: (state) => {
42
+ state.options = undefined;
43
+ state.payload = undefined;
44
+ WebUtils.setCookie('@authenticationState', '', -1);
45
+ },
46
+ },
47
+ extraReducers: (builder) => {
48
+ builder.addCase(authenticationStateLoadFromApi.pending, (state) => {
49
+ if (!state.options)
50
+ state.options = {};
51
+ state.options.loading = true;
52
+ });
53
+ builder.addCase(authenticationStateLoadFromApi.fulfilled, (state, response) => {
54
+ if (state.options)
55
+ state.options.loading = false;
56
+ state.payload = response.payload?.payload;
57
+ if (state.payload)
58
+ WebUtils.setCookie('@authenticationState', JSON.stringify(state), 1);
59
+ });
60
+ builder.addCase(authenticationStateLoadFromApi.rejected, (state) => {
61
+ if (state.options)
62
+ state.options.loading = false;
63
+ });
64
+ }
65
+ });
66
+ export const { logout: authenticationStateLogout, set: authenticationStateSet } = authenticationSlice.actions;
67
+ export default authenticationSlice.reducer;
68
+ //# sourceMappingURL=authentication.slice.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authentication.slice.js","sourceRoot":"","sources":["../../../src/auth/authentication.slice.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACjE,OAA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AAO,EAAE,OAAO,CAAA;YACzC,IAAI,KAAK,CAAC,OAAO;gBACf,QAAQ,CAAC,SAAS,CAAC,sBAAsB,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAA;QACxE,CAAC,CAAC,CAAA;QACF,OAAO,CAAC,OAAO,CAAC,8BAA8B,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,EAAE;YACjE,IAAI,KAAK,CAAC,OAAO;gBACf,KAAK,CAAC,OAAO,CAAC,OAAO,GAAG,KAAK,CAAA;QACjC,CAAC,CAAC,CAAA;IACJ,CAAC;CACF,CAAC,CAAA;AAEF,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,yBAAyB,EAAE,GAAG,EAAE,sBAAsB,EAAE,GAAG,mBAAmB,CAAC,OAAO,CAAA;AAE7G,eAAe,mBAAmB,CAAC,OAAO,CAAA"}
@@ -0,0 +1,30 @@
1
+ import LogoutIcon from '@mui/icons-material/Logout';
2
+ import { Avatar, IconButton, Menu, MenuItem } from "@mui/material";
3
+ import React, { useState } from "react";
4
+ import { useDispatch } from 'react-redux';
5
+ import { authenticationStateLogout } from './authentication.slice';
6
+ export const UserOptionsComponent = ({ user, datacenterAuthBaseUrl, onLogOut }) => {
7
+ const dispatch = useDispatch();
8
+ const [menuAnchor, setAnchorElemnt] = useState(null);
9
+ const openMenu = Boolean(menuAnchor);
10
+ return React.createElement(React.Fragment, null,
11
+ React.createElement(Menu, { id: "basic-menu", anchorEl: menuAnchor, open: openMenu, onClose: () => setAnchorElemnt(null), MenuListProps: {
12
+ 'aria-labelledby': 'basic-button',
13
+ } },
14
+ React.createElement(MenuItem, { style: { display: 'flex', gap: 10 }, onClick: () => {
15
+ let authUrl = `${datacenterAuthBaseUrl}/profile`;
16
+ window.open(authUrl, '_blank')?.focus();
17
+ setAnchorElemnt(null);
18
+ } }, user.name),
19
+ React.createElement(MenuItem, { style: { display: 'flex', gap: 10 }, onClick: () => {
20
+ dispatch(authenticationStateLogout());
21
+ if (onLogOut)
22
+ onLogOut();
23
+ setAnchorElemnt(null);
24
+ } },
25
+ React.createElement(LogoutIcon, null),
26
+ " Sair")),
27
+ React.createElement(IconButton, { onClick: e => setAnchorElemnt(e.currentTarget) },
28
+ React.createElement(Avatar, { src: user.imageUrl })));
29
+ };
30
+ //# sourceMappingURL=userOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"userOptions.js","sourceRoot":"","sources":["../../../src/auth/userOptions.tsx"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,4BAA4B,CAAA;AACnD,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAElE,OAAO,KAAK,EAAE,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,EAAE,yBAAyB,EAAE,MAAM,wBAAwB,CAAA;AAQlE,MAAM,CAAC,MAAM,oBAAoB,GAAyC,CAAC,EAAE,IAAI,EAAE,qBAAqB,EAAE,QAAQ,EAAE,EAAE,EAAE;IACtH,MAAM,QAAQ,GAAG,WAAW,EAAO,CAAA;IACnC,MAAM,CAAC,UAAU,EAAE,eAAe,CAAC,GAAG,QAAQ,CAA+C,IAAI,CAAC,CAAA;IAClG,MAAM,QAAQ,GAAG,OAAO,CAAC,UAAU,CAAC,CAAA;IAEpC,OAAO;QACL,oBAAC,IAAI,IACH,EAAE,EAAC,YAAY,EACf,QAAQ,EAAE,UAAU,EACpB,IAAI,EAAE,QAAQ,EACd,OAAO,EAAE,GAAG,EAAE,CAAC,eAAe,CAAC,IAAI,CAAC,EACpC,aAAa,EAAE;gBACb,iBAAiB,EAAE,cAAc;aAClC;YACD,oBAAC,QAAQ,IACP,KAAK,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,EAAE,EAAE,EACnC,OAAO,EAAE,GAAG,EAAE;oBACZ,IAAI,OAAO,GAAG,GAAG,qBAAqB,UAAU,CAAA;oBAChD,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,EAAE,KAAK,EAAE,CAAA;oBACvC,eAAe,CAAC,IAAI,CAAC,CAAA;gBACvB,CAAC,IAEA,IAAI,CAAC,IAAI,CACD;YACX,oBAAC,QAAQ,IACP,KAAK,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,EAAE,EAAE,EACnC,OAAO,EAAE,GAAG,EAAE;oBACZ,QAAQ,CAAC,yBAAyB,EAAE,CAAC,CAAA;oBACrC,IAAI,QAAQ;wBAAE,QAAQ,EAAE,CAAA;oBACxB,eAAe,CAAC,IAAI,CAAC,CAAA;gBACvB,CAAC;gBAED,oBAAC,UAAU,OAAG;wBACL,CACN;QAEP,oBAAC,UAAU,IACT,OAAO,EAAE,CAAC,CAAC,EAAE,CAAC,eAAe,CAAC,CAAC,CAAC,aAAa,CAAC;YAC/C,oBAAC,MAAM,IAAC,GAAG,EAAE,IAAI,CAAC,QAAQ,GAAI,CAAa,CAC3C,CAAA;AACL,CAAC,CAAA"}
@@ -0,0 +1,7 @@
1
+ import { AuthCheckComponent, } from './auth/authCheck';
2
+ import authenticationReducer, { authenticationStateLoadFromApi, authenticationStateLogout, authenticationSlice, authenticationStateSet, } from './auth/authentication.slice';
3
+ import { AuthRequiredComponent, } from './auth/authRequired';
4
+ import { AuthUtils, } from './auth/authUtils';
5
+ import { UserOptionsComponent, } from './auth/userOptions';
6
+ export { AuthCheckComponent, authenticationReducer, authenticationStateLoadFromApi, authenticationStateLogout, authenticationSlice, authenticationStateSet, AuthRequiredComponent, AuthUtils, UserOptionsComponent, };
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,GAA6B,MAAM,kBAAkB,CAAA;AAChF,OAAO,qBAAqB,EAAE,EAAE,8BAA8B,EAA4C,yBAAyB,EAAE,mBAAmB,EAAE,sBAAsB,GAAG,MAAM,6BAA6B,CAAA;AACtN,OAAO,EAAE,qBAAqB,GAAgC,MAAM,qBAAqB,CAAA;AACzF,OAAO,EAAE,SAAS,GAAG,MAAM,kBAAkB,CAAA;AAC7C,OAAO,EAAE,oBAAoB,GAA+B,MAAM,oBAAoB,CAAA;AAEtF,OAAO,EACL,kBAAkB,EAClB,qBAAqB,EAAE,8BAA8B,EAA4C,yBAAyB,EAAE,mBAAmB,EAAE,sBAAsB,EACvK,qBAAqB,EACrB,SAAS,EACT,oBAAoB,GACrB,CAAA"}
@@ -0,0 +1,8 @@
1
+ import React, { ReactNode } from "react";
2
+ import { IAuthenticationState } from "./authentication.slice";
3
+ export interface IAuthCheckComponentProps {
4
+ authenticationState: IAuthenticationState;
5
+ baseApiUrl: string;
6
+ children: ReactNode;
7
+ }
8
+ export declare const AuthCheckComponent: React.FC<IAuthCheckComponentProps>;
@@ -0,0 +1,9 @@
1
+ import React, { ReactNode } from "react";
2
+ import { IAuthenticationState } from "./authentication.slice";
3
+ export interface IAuthRequiredComponentProps {
4
+ authenticationState: IAuthenticationState;
5
+ authUrlBase: string;
6
+ redirectUrl: string;
7
+ children: ReactNode;
8
+ }
9
+ export declare const AuthRequiredComponent: React.FC<IAuthRequiredComponentProps>;
@@ -0,0 +1,4 @@
1
+ export declare const AuthUtils: {
2
+ authUuidQueryStringName: string;
3
+ authToRedirectQueryStringName: string;
4
+ };
@@ -0,0 +1,35 @@
1
+ import { IAuthentication, IAuthenticationRequestBodyDefault, IAuthenticationRequestBodyFromGoogleToken, IAuthenticationRequestBodyFromUuid, IUser } from 'datacenter-lib-common-ts';
2
+ export interface IAuthenticationExt extends IAuthentication {
3
+ user: IUser;
4
+ }
5
+ export interface IAuthenticationState {
6
+ options?: {
7
+ loading?: boolean;
8
+ };
9
+ payload?: IAuthenticationExt;
10
+ }
11
+ export declare const authenticationStateLoadFromApi: import("@reduxjs/toolkit").AsyncThunk<IAuthenticationState | undefined, {
12
+ baseApiUrl: string;
13
+ requestPayload: IAuthenticationRequestBodyDefault | IAuthenticationRequestBodyFromUuid | IAuthenticationRequestBodyFromGoogleToken;
14
+ onError?: (msg?: string) => void;
15
+ onSuccess?: (authentication?: IAuthentication) => void;
16
+ }, {
17
+ state?: unknown;
18
+ dispatch?: import("redux").Dispatch;
19
+ extra?: unknown;
20
+ rejectValue?: unknown;
21
+ serializedErrorType?: unknown;
22
+ pendingMeta?: unknown;
23
+ fulfilledMeta?: unknown;
24
+ rejectedMeta?: unknown;
25
+ }>;
26
+ export declare const authenticationSlice: import("@reduxjs/toolkit").Slice<IAuthenticationState, {
27
+ set: (state: import("immer/dist/internal").WritableDraft<IAuthenticationState>, action: {
28
+ payload: any;
29
+ type: string;
30
+ }) => void;
31
+ logout: (state: import("immer/dist/internal").WritableDraft<IAuthenticationState>) => void;
32
+ }, "authenticationSlice">;
33
+ export declare const authenticationStateLogout: import("@reduxjs/toolkit").ActionCreatorWithoutPayload<"authenticationSlice/logout">, authenticationStateSet: import("@reduxjs/toolkit").ActionCreatorWithPayload<any, "authenticationSlice/set">;
34
+ declare const _default: import("redux").Reducer<IAuthenticationState>;
35
+ export default _default;
@@ -0,0 +1,8 @@
1
+ import { IUser } from "datacenter-lib-common-ts";
2
+ import React from "react";
3
+ export interface IUserOptionsComponentProps {
4
+ user: IUser;
5
+ datacenterAuthBaseUrl: string;
6
+ onLogOut?: () => void;
7
+ }
8
+ export declare const UserOptionsComponent: React.FC<IUserOptionsComponentProps>;
@@ -0,0 +1,6 @@
1
+ import { AuthCheckComponent, IAuthCheckComponentProps } from './auth/authCheck';
2
+ import authenticationReducer, { authenticationStateLoadFromApi, IAuthenticationState, IAuthenticationExt, authenticationStateLogout, authenticationSlice, authenticationStateSet } from './auth/authentication.slice';
3
+ import { AuthRequiredComponent, IAuthRequiredComponentProps } from './auth/authRequired';
4
+ import { AuthUtils } from './auth/authUtils';
5
+ import { UserOptionsComponent, IUserOptionsComponentProps } from './auth/userOptions';
6
+ export { AuthCheckComponent, IAuthCheckComponentProps, authenticationReducer, authenticationStateLoadFromApi, IAuthenticationState, IAuthenticationExt, authenticationStateLogout, authenticationSlice, authenticationStateSet, AuthRequiredComponent, IAuthRequiredComponentProps, AuthUtils, UserOptionsComponent, IUserOptionsComponentProps, };
package/package.json ADDED
@@ -0,0 +1,71 @@
1
+ {
2
+ "author": "joabssilveira",
3
+ "dependencies": {
4
+ "@emotion/react": "^11.13.5",
5
+ "@emotion/styled": "^11.13.5",
6
+ "@mui/icons-material": "^6.3.1",
7
+ "@mui/material": "^6.1.8",
8
+ "@reduxjs/toolkit": "^1.8.6",
9
+ "@types/react": "^18.3.12",
10
+ "@types/react-dom": "^18.3.1",
11
+ "datacenter-lib-common-ts": "^1.0.0",
12
+ "jwt-decode": "^4.0.0",
13
+ "react": "^17.0.0 || ^18.0.0",
14
+ "react-dom": "^17.0.0 || ^18.0.0",
15
+ "react-icons": "^4.6.0",
16
+ "react-redux": "^8.0.4",
17
+ "react-router-dom": "^6.4.3",
18
+ "redux-persist": "^6.0.0",
19
+ "redux-thunk": "^2.4.1"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "^22.10.5",
23
+ "standard-version": "^9.5.0",
24
+ "typescript": "^5.6.3"
25
+ },
26
+ "engines": {
27
+ "node": ">=10"
28
+ },
29
+ "files": [
30
+ "dist",
31
+ "src"
32
+ ],
33
+ "homepage": "https://github.com/joabssilveira/datacenter-lib-react-ts",
34
+ "keywords": [
35
+ "react",
36
+ "mui",
37
+ "material",
38
+ "autocomplete",
39
+ "breadcrumbs",
40
+ "floatbuttom",
41
+ "table"
42
+ ],
43
+ "license": "MIT",
44
+ "main": "dist/cjs/index.js",
45
+ "module": "dist/esm/index.js",
46
+ "types": "dist/types/index.d.ts",
47
+ "name": "datacenter-lib-react-ts",
48
+ "peerDependencies": {
49
+ "react": "^17.0.0 || ^18.0.0",
50
+ "react-dom": "^17.0.0 || ^18.0.0"
51
+ },
52
+ "prettier": {
53
+ "printWidth": 80,
54
+ "semi": true,
55
+ "singleQuote": true,
56
+ "trailingComma": "es5"
57
+ },
58
+ "scripts": {
59
+ "build": "tsc --project tsconfig.cjs.json && tsc --project tsconfig.esm.json",
60
+ "release:major": "standard-version -M && git add CHANGELOG.md && git commit -m \"updated CHANGELOG.md\" && npm version major && git push origin && git push origin --tags && npm publish --access public",
61
+ "release:minor": "standard-version -m && git add CHANGELOG.md && git commit -m \"updated CHANGELOG.md\" && npm version minor && git push origin && git push origin --tags && npm publish --access public",
62
+ "release:patch": "standard-version -p && git add CHANGELOG.md && git commit -m \"updated CHANGELOG.md\" && npm version patch && git push origin && git push origin --tags && npm publish --access public"
63
+ },
64
+ "version": "1.0.0",
65
+ "exports": {
66
+ ".": {
67
+ "require": "./dist/cjs/index.js",
68
+ "import": "./dist/esm/index.js"
69
+ }
70
+ }
71
+ }
@@ -0,0 +1,58 @@
1
+
2
+ import React, { ReactNode, useEffect } from "react";
3
+ import { useLocation } from "react-router-dom";
4
+ import { authenticationStateLoadFromApi, IAuthenticationState } from "./authentication.slice";
5
+ import { AuthUtils } from "./authUtils";
6
+ import { useNavigate } from 'react-router-dom'
7
+ import { useDispatch } from "react-redux";
8
+ import { AuthenticationType, IAuthenticationRequestBodyFromUuid } from "datacenter-lib-common-ts";
9
+
10
+ export interface IAuthCheckComponentProps {
11
+ authenticationState: IAuthenticationState,
12
+ baseApiUrl: string,
13
+ // authUrlBase: string,
14
+ // redirectUrl: string,
15
+ children: ReactNode;
16
+ }
17
+
18
+ export const AuthCheckComponent: React.FC<IAuthCheckComponentProps> = ({ authenticationState, /*authUrlBase, redirectUrl,*/ baseApiUrl, children }) => {
19
+ const navigate = useNavigate()
20
+ const location = useLocation()
21
+ const dispatch = useDispatch<any>()
22
+ const queryParams = new URLSearchParams(location.search)
23
+ const authUuid = queryParams.get(AuthUtils.authUuidQueryStringName)
24
+
25
+ const onGetAuthFinish = () => {
26
+ queryParams.delete(AuthUtils.authUuidQueryStringName);
27
+ const newSearch = queryParams.toString();
28
+ const newPath = `${location.pathname}${newSearch ? `?${newSearch}` : ''}`;
29
+ navigate(newPath, { replace: true });
30
+ }
31
+
32
+ useEffect(() => {
33
+ if (!authenticationState.payload && !authenticationState.options?.loading) {
34
+ if (authUuid) {
35
+ dispatch(authenticationStateLoadFromApi({
36
+ baseApiUrl,
37
+ requestPayload: {
38
+ type: AuthenticationType.uuid,
39
+ uuid: authUuid
40
+ } as IAuthenticationRequestBodyFromUuid,
41
+ onSuccess: onGetAuthFinish,
42
+ onError: onGetAuthFinish
43
+ }))
44
+ }
45
+ }
46
+ else if (authenticationState.payload && authUuid) {
47
+ // caso o usuario ja tenha logado e tente acessar a pagina de autenticacao, o redirect manda o authUuid
48
+ // nesse cenario, deve-se eliminar o authUuid da url
49
+ // isso vale para autenticacao em um dominio diferente do dominio do autenticador
50
+ // para recursos no mesmo dominio do autenticador, o redirect quando o usuario já tem autenticado esta correto, ou seja,
51
+ // ele nao manda o authUuid caso o usuario já esteja logado, em dominio diferentes nao tem como o autenticador saber se o app esta logado
52
+ onGetAuthFinish()
53
+ }
54
+ // }, [authenticationState, location]);
55
+ }, [authenticationState]);
56
+
57
+ return <>{children}</>
58
+ };
@@ -0,0 +1,35 @@
1
+
2
+ import React, { ReactNode, useEffect } from "react";
3
+ import { useLocation } from "react-router-dom";
4
+ import { IAuthenticationState } from "./authentication.slice";
5
+ import { AuthUtils } from "./authUtils";
6
+
7
+ export interface IAuthRequiredComponentProps {
8
+ authenticationState: IAuthenticationState,
9
+ // baseApiUrl: string,
10
+ authUrlBase: string,
11
+ redirectUrl: string,
12
+ children: ReactNode;
13
+ }
14
+
15
+ export const AuthRequiredComponent: React.FC<IAuthRequiredComponentProps> = ({ authenticationState, authUrlBase, redirectUrl, /*baseApiUrl,*/ children }) => {
16
+ const location = useLocation()
17
+ const queryParams = new URLSearchParams(location.search)
18
+ const authUuid = queryParams.get(AuthUtils.authUuidQueryStringName)
19
+
20
+ useEffect(() => {
21
+ if (!authenticationState.payload && !authenticationState.options?.loading) {
22
+ if (!authUuid) {
23
+ if (!authUrlBase.includes('/login'))
24
+ authUrlBase = `${authUrlBase}/login`
25
+ window.location.href = `${authUrlBase}?${AuthUtils.authToRedirectQueryStringName}=${encodeURIComponent(redirectUrl)}`
26
+ }
27
+ }
28
+ }, [authenticationState]);
29
+
30
+ if (!authenticationState.payload) {
31
+ return null;
32
+ }
33
+
34
+ return <>{children}</>
35
+ };
@@ -0,0 +1,4 @@
1
+ export const AuthUtils = {
2
+ authUuidQueryStringName: 'au',
3
+ authToRedirectQueryStringName: 'to',
4
+ }
@@ -0,0 +1,97 @@
1
+ import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
2
+ import { AuthenticationsApiClient, IAuthentication, IAuthenticationRequestBodyDefault, IAuthenticationRequestBodyFromGoogleToken, IAuthenticationRequestBodyFromUuid, IAuthenticationTokenData, IUser } from 'datacenter-lib-common-ts';
3
+ import { WebUtils } from 'fwork-jsts-common/src';
4
+ import { jwtDecode } from 'jwt-decode'; // dont use jsonwebtokens package here, its only for node projects
5
+
6
+ export interface IAuthenticationExt extends IAuthentication {
7
+ user: IUser
8
+ }
9
+
10
+ export interface IAuthenticationState {
11
+ options?: {
12
+ loading?: boolean,
13
+ },
14
+ payload?: IAuthenticationExt
15
+ }
16
+
17
+ const cookie = WebUtils.getCookie('@authenticationState')
18
+ const cookieObj = cookie ? JSON.parse(cookie) : null
19
+
20
+ const initState: IAuthenticationState = cookieObj || {}
21
+
22
+ export const authenticationStateLoadFromApi = createAsyncThunk(
23
+ 'authenticationStateLoadFromApi',
24
+ async (arg: {
25
+ baseApiUrl: string,
26
+ requestPayload: IAuthenticationRequestBodyDefault | IAuthenticationRequestBodyFromUuid | IAuthenticationRequestBodyFromGoogleToken,
27
+
28
+ onError?: (msg?: string) => void,
29
+ onSuccess?: (authentication?: IAuthentication) => void,
30
+ // }, { getState }) => {
31
+ }) => {
32
+ try {
33
+ let result: IAuthenticationState = {}
34
+
35
+ const response = await new AuthenticationsApiClient({ baseApiUrl: arg.baseApiUrl, }).post({
36
+ data: arg.requestPayload,
37
+ })
38
+
39
+ if (response?.success && response.data?.token) {
40
+ if (arg.onSuccess)
41
+ arg.onSuccess(response.data)
42
+ var decToken: IAuthenticationTokenData = jwtDecode(response.data.token)
43
+ result.payload = {
44
+ ...response.data,
45
+ user: decToken.user
46
+ }
47
+ }
48
+ else if (arg.onError)
49
+ arg.onError(response?.msg)
50
+
51
+ return result
52
+ } catch (error) {
53
+ if (arg.onError)
54
+ arg.onError('Erro ao tentar autenticar')
55
+
56
+ return
57
+ }
58
+ }
59
+ )
60
+
61
+ export const authenticationSlice = createSlice({
62
+ name: 'authenticationSlice',
63
+ initialState: initState,
64
+ reducers: {
65
+ set: (state, action) => {
66
+ state.payload = action.payload.payload
67
+ WebUtils.setCookie('@authenticationState', JSON.stringify(state), 1)
68
+ },
69
+ logout: (state) => {
70
+ state.options = undefined
71
+ state.payload = undefined
72
+ WebUtils.setCookie('@authenticationState', '', -1)
73
+ },
74
+ },
75
+ extraReducers: (builder) => {
76
+ builder.addCase(authenticationStateLoadFromApi.pending, (state) => {
77
+ if (!state.options)
78
+ state.options = {}
79
+ state.options.loading = true
80
+ })
81
+ builder.addCase(authenticationStateLoadFromApi.fulfilled, (state, response) => {
82
+ if (state.options)
83
+ state.options.loading = false
84
+ state.payload = response.payload?.payload
85
+ if (state.payload)
86
+ WebUtils.setCookie('@authenticationState', JSON.stringify(state), 1)
87
+ })
88
+ builder.addCase(authenticationStateLoadFromApi.rejected, (state) => {
89
+ if (state.options)
90
+ state.options.loading = false
91
+ })
92
+ }
93
+ })
94
+
95
+ export const { logout: authenticationStateLogout, set: authenticationStateSet } = authenticationSlice.actions
96
+
97
+ export default authenticationSlice.reducer
@@ -0,0 +1,54 @@
1
+ import LogoutIcon from '@mui/icons-material/Logout'
2
+ import { Avatar, IconButton, Menu, MenuItem } from "@mui/material"
3
+ import { IUser } from "datacenter-lib-common-ts"
4
+ import React, { useState } from "react"
5
+ import { useDispatch } from 'react-redux'
6
+ import { authenticationStateLogout } from './authentication.slice'
7
+
8
+ export interface IUserOptionsComponentProps {
9
+ user: IUser,
10
+ datacenterAuthBaseUrl: string,
11
+ onLogOut?: () => void
12
+ }
13
+
14
+ export const UserOptionsComponent: React.FC<IUserOptionsComponentProps> = ({ user, datacenterAuthBaseUrl, onLogOut }) => {
15
+ const dispatch = useDispatch<any>()
16
+ const [menuAnchor, setAnchorElemnt] = useState<Element | (() => Element) | null | undefined>(null)
17
+ const openMenu = Boolean(menuAnchor)
18
+
19
+ return <>
20
+ <Menu
21
+ id="basic-menu"
22
+ anchorEl={menuAnchor}
23
+ open={openMenu}
24
+ onClose={() => setAnchorElemnt(null)}
25
+ MenuListProps={{
26
+ 'aria-labelledby': 'basic-button',
27
+ }}>
28
+ <MenuItem
29
+ style={{ display: 'flex', gap: 10 }}
30
+ onClick={() => {
31
+ let authUrl = `${datacenterAuthBaseUrl}/profile`
32
+ window.open(authUrl, '_blank')?.focus()
33
+ setAnchorElemnt(null)
34
+ }}
35
+ >
36
+ {user.name}
37
+ </MenuItem>
38
+ <MenuItem
39
+ style={{ display: 'flex', gap: 10 }}
40
+ onClick={() => {
41
+ dispatch(authenticationStateLogout())
42
+ if (onLogOut) onLogOut()
43
+ setAnchorElemnt(null)
44
+ }}
45
+ >
46
+ <LogoutIcon /> Sair
47
+ </MenuItem>
48
+ </Menu>
49
+
50
+ <IconButton
51
+ onClick={e => setAnchorElemnt(e.currentTarget)}
52
+ ><Avatar src={user.imageUrl} /></IconButton>
53
+ </>
54
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,13 @@
1
+ import { AuthCheckComponent, IAuthCheckComponentProps, } from './auth/authCheck'
2
+ import authenticationReducer, { authenticationStateLoadFromApi, IAuthenticationState, IAuthenticationExt, authenticationStateLogout, authenticationSlice, authenticationStateSet, } from './auth/authentication.slice'
3
+ import { AuthRequiredComponent, IAuthRequiredComponentProps, } from './auth/authRequired'
4
+ import { AuthUtils, } from './auth/authUtils'
5
+ import { UserOptionsComponent, IUserOptionsComponentProps, } from './auth/userOptions'
6
+
7
+ export {
8
+ AuthCheckComponent, IAuthCheckComponentProps,
9
+ authenticationReducer, authenticationStateLoadFromApi, IAuthenticationState, IAuthenticationExt, authenticationStateLogout, authenticationSlice, authenticationStateSet,
10
+ AuthRequiredComponent, IAuthRequiredComponentProps,
11
+ AuthUtils,
12
+ UserOptionsComponent, IUserOptionsComponentProps,
13
+ }