datacenter-lib-react-ts 1.0.4 → 1.0.5
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/cjs/auth/authCheck.js +8 -7
- package/dist/cjs/auth/authCheck.js.map +1 -1
- package/dist/cjs/auth/authRequired.js +5 -2
- package/dist/cjs/auth/authRequired.js.map +1 -1
- package/dist/cjs/auth/authentication.slice.js +21 -1
- package/dist/cjs/auth/authentication.slice.js.map +1 -1
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/auth/authCheck.js +9 -8
- package/dist/esm/auth/authCheck.js.map +1 -1
- package/dist/esm/auth/authRequired.js +5 -2
- package/dist/esm/auth/authRequired.js.map +1 -1
- package/dist/esm/auth/authentication.slice.js +18 -0
- package/dist/esm/auth/authentication.slice.js.map +1 -1
- package/dist/esm/index.js +2 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/types/auth/authentication.slice.d.ts +7 -0
- package/dist/types/index.d.ts +2 -2
- package/package.json +1 -1
- package/src/auth/authCheck.tsx +9 -10
- package/src/auth/authRequired.tsx +5 -3
- package/src/auth/authentication.slice.tsx +29 -4
- package/src/index.tsx +2 -2
|
@@ -2,15 +2,14 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AuthCheckComponent = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
+
const datacenter_lib_common_ts_1 = require("datacenter-lib-common-ts");
|
|
5
6
|
const react_1 = tslib_1.__importStar(require("react"));
|
|
7
|
+
const react_redux_1 = require("react-redux");
|
|
6
8
|
const react_router_dom_1 = require("react-router-dom");
|
|
7
9
|
const authentication_slice_1 = require("./authentication.slice");
|
|
8
10
|
const authUtils_1 = require("./authUtils");
|
|
9
|
-
const
|
|
10
|
-
const
|
|
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)();
|
|
11
|
+
const AuthCheckComponent = ({ authenticationState, baseApiUrl, children }) => {
|
|
12
|
+
const navigate = (0, react_router_dom_1.useNavigate)();
|
|
14
13
|
const location = (0, react_router_dom_1.useLocation)();
|
|
15
14
|
const dispatch = (0, react_redux_1.useDispatch)();
|
|
16
15
|
const queryParams = new URLSearchParams(location.search);
|
|
@@ -22,7 +21,8 @@ const AuthCheckComponent = ({ authenticationState, /*authUrlBase, redirectUrl,*/
|
|
|
22
21
|
navigate(newPath, { replace: true });
|
|
23
22
|
};
|
|
24
23
|
(0, react_1.useEffect)(() => {
|
|
25
|
-
if (!authenticationState.payload && !authenticationState.options?.loading) {
|
|
24
|
+
// if (!authenticationState.payload && !authenticationState.options?.loading) {
|
|
25
|
+
if (!new authentication_slice_1.AuthenticationStateHelper({ state: authenticationState }).authenticated && !authenticationState.options?.loading) {
|
|
26
26
|
if (authUuid) {
|
|
27
27
|
dispatch((0, authentication_slice_1.authenticationStateLoadFromApi)({
|
|
28
28
|
baseApiUrl,
|
|
@@ -35,7 +35,8 @@ const AuthCheckComponent = ({ authenticationState, /*authUrlBase, redirectUrl,*/
|
|
|
35
35
|
}));
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
-
else if (authenticationState.payload && authUuid) {
|
|
38
|
+
// else if (authenticationState.payload && authUuid) {
|
|
39
|
+
else if (new authentication_slice_1.AuthenticationStateHelper({ state: authenticationState }).authenticated && authUuid) {
|
|
39
40
|
// caso o usuario ja tenha logado e tente acessar a pagina de autenticacao, o redirect manda o authUuid
|
|
40
41
|
// nesse cenario, deve-se eliminar o authUuid da url
|
|
41
42
|
// isso vale para autenticacao em um dominio diferente do dominio do autenticador
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authCheck.js","sourceRoot":"","sources":["../../../src/auth/authCheck.tsx"],"names":[],"mappings":";;;;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AAA,qDAA8B,EAAC;oBACtC,UAAU;oBACV,cAAc,EAAE;wBACd,IAAI,EAAE,6CAAkB,CAAC,IAAI;wBAC7B,IAAI,EAAE,QAAQ;qBACuB;oBACvC,SAAS,EAAE,eAAe;oBAC1B,OAAO,EAAE,eAAe;iBACzB,CAAC,CAAC,CAAA;YACL,CAAC;QACH,CAAC;QACD,sDAAsD;aACjD,IAAI,IAAI,gDAAyB,CAAC,EAAE,KAAK,EAAE,mBAAmB,EAAE,CAAC,CAAC,aAAa,IAAI,QAAQ,EAAE,CAAC;YACjG,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;AA1CW,QAAA,kBAAkB,sBA0C7B"}
|
|
@@ -4,13 +4,15 @@ exports.AuthRequiredComponent = void 0;
|
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const react_1 = tslib_1.__importStar(require("react"));
|
|
6
6
|
const react_router_dom_1 = require("react-router-dom");
|
|
7
|
+
const authentication_slice_1 = require("./authentication.slice");
|
|
7
8
|
const authUtils_1 = require("./authUtils");
|
|
8
9
|
const AuthRequiredComponent = ({ authenticationState, authUrlBase, redirectUrl, /*baseApiUrl,*/ children }) => {
|
|
9
10
|
const location = (0, react_router_dom_1.useLocation)();
|
|
10
11
|
const queryParams = new URLSearchParams(location.search);
|
|
11
12
|
const authUuid = queryParams.get(authUtils_1.AuthUtils.authUuidQueryStringName);
|
|
12
13
|
(0, react_1.useEffect)(() => {
|
|
13
|
-
if (!authenticationState.payload && !authenticationState.options?.loading) {
|
|
14
|
+
// if (!authenticationState.payload && !authenticationState.options?.loading) {
|
|
15
|
+
if (!new authentication_slice_1.AuthenticationStateHelper({ state: authenticationState, }).authenticated && !authenticationState.options?.loading) {
|
|
14
16
|
if (!authUuid) {
|
|
15
17
|
if (!authUrlBase.includes('/login'))
|
|
16
18
|
authUrlBase = `${authUrlBase}/login`;
|
|
@@ -18,7 +20,8 @@ const AuthRequiredComponent = ({ authenticationState, authUrlBase, redirectUrl,
|
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
22
|
}, [authenticationState]);
|
|
21
|
-
if (!authenticationState.payload) {
|
|
23
|
+
// if (!authenticationState.payload) {
|
|
24
|
+
if (!new authentication_slice_1.AuthenticationStateHelper({ state: authenticationState, }).authenticated) {
|
|
22
25
|
return null;
|
|
23
26
|
}
|
|
24
27
|
return react_1.default.createElement(react_1.default.Fragment, null, children);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authRequired.js","sourceRoot":"","sources":["../../../src/auth/authRequired.tsx"],"names":[],"mappings":";;;;AACA,uDAAoD;AACpD,uDAA+C;
|
|
1
|
+
{"version":3,"file":"authRequired.js","sourceRoot":"","sources":["../../../src/auth/authRequired.tsx"],"names":[],"mappings":";;;;AACA,uDAAoD;AACpD,uDAA+C;AAC/C,iEAAyF;AACzF,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,+EAA+E;QAC/E,IAAI,CAAC,IAAI,gDAAyB,CAAC,EAAE,KAAK,EAAE,mBAAmB,GAAG,CAAC,CAAC,aAAa,IAAI,CAAC,mBAAmB,CAAC,OAAO,EAAE,OAAO,EAAE,CAAC;YAC3H,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,sCAAsC;IACtC,IAAI,CAAC,IAAI,gDAAyB,CAAC,EAAE,KAAK,EAAE,mBAAmB,GAAG,CAAC,CAAC,aAAa,EAAE,CAAC;QAClF,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,8DAAG,QAAQ,CAAI,CAAA;AACxB,CAAC,CAAC;AAtBW,QAAA,qBAAqB,yBAsBhC"}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var _a;
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.authenticationStateSet = exports.authenticationStateLogout = exports.authenticationSlice = exports.authenticationStateLoadFromApi = void 0;
|
|
4
|
+
exports.AuthenticationStateHelper = exports.authenticationStateSet = exports.authenticationStateLogout = exports.authenticationSlice = exports.authenticationStateLoadFromApi = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
5
6
|
const toolkit_1 = require("@reduxjs/toolkit");
|
|
6
7
|
const datacenter_lib_common_ts_1 = require("datacenter-lib-common-ts");
|
|
7
8
|
const fwork_jsts_common_1 = require("fwork-jsts-common");
|
|
8
9
|
const jwt_decode_1 = require("jwt-decode"); // dont use jsonwebtokens package here, its only for node projects
|
|
10
|
+
const moment_1 = tslib_1.__importDefault(require("moment"));
|
|
9
11
|
const cookie = fwork_jsts_common_1.WebUtils.getCookie('@authenticationState');
|
|
10
12
|
const cookieObj = cookie ? JSON.parse(cookie) : null;
|
|
11
13
|
const initState = cookieObj || {};
|
|
@@ -69,4 +71,22 @@ exports.authenticationSlice = (0, toolkit_1.createSlice)({
|
|
|
69
71
|
});
|
|
70
72
|
_a = exports.authenticationSlice.actions, exports.authenticationStateLogout = _a.logout, exports.authenticationStateSet = _a.set;
|
|
71
73
|
exports.default = exports.authenticationSlice.reducer;
|
|
74
|
+
class AuthenticationStateHelper {
|
|
75
|
+
constructor(args) {
|
|
76
|
+
this.state = args.state;
|
|
77
|
+
}
|
|
78
|
+
get authenticated() {
|
|
79
|
+
let result = false;
|
|
80
|
+
if (this.state.payload?.token)
|
|
81
|
+
try {
|
|
82
|
+
var decToken = (0, jwt_decode_1.jwtDecode)(this.state.payload?.token);
|
|
83
|
+
result = (decToken.exp || 0) >= (0, moment_1.default)().unix();
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
result = false;
|
|
87
|
+
}
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
exports.AuthenticationStateHelper = AuthenticationStateHelper;
|
|
72
92
|
//# sourceMappingURL=authentication.slice.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authentication.slice.js","sourceRoot":"","sources":["../../../src/auth/authentication.slice.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"authentication.slice.js","sourceRoot":"","sources":["../../../src/auth/authentication.slice.tsx"],"names":[],"mappings":";;;;;AAAA,8CAAiE;AACjE,uEAAwO;AACxO,yDAA6C;AAC7C,2CAAuC,CAAC,kEAAkE;AAC1G,4DAA4B;AAa5B,MAAM,MAAM,GAAG,4BAAQ,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,C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CAAC,CAAA;AAEW,KAAqE,2BAAmB,CAAC,OAAO,EAAtF,iCAAyB,cAAO,8BAAsB,UAAgC;AAE7G,kBAAe,2BAAmB,CAAC,OAAO,CAAA;AAE1C,MAAa,yBAAyB;IAGpC,YAAY,IAEX;QACC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAA;IACzB,CAAC;IAED,IAAI,aAAa;QACf,IAAI,MAAM,GAAG,KAAK,CAAA;QAElB,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK;YAC3B,IAAI,CAAC;gBACH,IAAI,QAAQ,GAA6B,IAAA,sBAAS,EAAC,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAA;gBAC7E,MAAM,GAAG,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC,CAAC,IAAI,IAAA,gBAAM,GAAE,CAAC,IAAI,EAAE,CAAA;YACjD,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,MAAM,GAAG,KAAK,CAAA;YAChB,CAAC;QAEH,OAAO,MAAM,CAAA;IACf,CAAC;CACF;AAtBD,8DAsBC"}
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
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;
|
|
3
|
+
exports.UserOptionsComponent = exports.AuthUtils = exports.AuthRequiredComponent = exports.authenticationStateSet = exports.authenticationSlice = exports.authenticationStateLogout = exports.AuthenticationStateHelper = exports.authenticationStateLoadFromApi = exports.authenticationReducer = exports.AuthCheckComponent = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const authCheck_1 = require("./auth/authCheck");
|
|
6
6
|
Object.defineProperty(exports, "AuthCheckComponent", { enumerable: true, get: function () { return authCheck_1.AuthCheckComponent; } });
|
|
7
7
|
const authentication_slice_1 = tslib_1.__importStar(require("./auth/authentication.slice"));
|
|
8
8
|
exports.authenticationReducer = authentication_slice_1.default;
|
|
9
9
|
Object.defineProperty(exports, "authenticationStateLoadFromApi", { enumerable: true, get: function () { return authentication_slice_1.authenticationStateLoadFromApi; } });
|
|
10
|
+
Object.defineProperty(exports, "AuthenticationStateHelper", { enumerable: true, get: function () { return authentication_slice_1.AuthenticationStateHelper; } });
|
|
10
11
|
Object.defineProperty(exports, "authenticationStateLogout", { enumerable: true, get: function () { return authentication_slice_1.authenticationStateLogout; } });
|
|
11
12
|
Object.defineProperty(exports, "authenticationSlice", { enumerable: true, get: function () { return authentication_slice_1.authenticationSlice; } });
|
|
12
13
|
Object.defineProperty(exports, "authenticationStateSet", { enumerable: true, get: function () { return authentication_slice_1.authenticationStateSet; } });
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":";;;;AAAA,gDAAgF;AAO9E,mGAPO,8BAAkB,OAOP;AANpB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":";;;;AAAA,gDAAgF;AAO9E,mGAPO,8BAAkB,OAOP;AANpB,4FAAiP;AAO/O,gCAPK,8BAAqB,CAOL;AAAE,+GAPO,qDAA8B,OAOP;AAAE,0GAPO,gDAAyB,OAOP;AAA4C,0GAPO,gDAAyB,OAOP;AAAE,oGAPO,0CAAmB,OAOP;AAAE,uGAPO,6CAAsB,OAOP;AANpM,sDAAyF;AAOvF,sGAPO,oCAAqB,OAOP;AANvB,gDAA6C;AAO3C,0FAPO,qBAAS,OAOP;AANX,oDAAsF;AAOpF,qGAPO,kCAAoB,OAOP"}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
+
import { AuthenticationType } from "datacenter-lib-common-ts";
|
|
1
2
|
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
3
|
import { useDispatch } from "react-redux";
|
|
7
|
-
import {
|
|
8
|
-
|
|
4
|
+
import { useLocation, useNavigate } from "react-router-dom";
|
|
5
|
+
import { AuthenticationStateHelper, authenticationStateLoadFromApi } from "./authentication.slice";
|
|
6
|
+
import { AuthUtils } from "./authUtils";
|
|
7
|
+
export const AuthCheckComponent = ({ authenticationState, baseApiUrl, children }) => {
|
|
9
8
|
const navigate = useNavigate();
|
|
10
9
|
const location = useLocation();
|
|
11
10
|
const dispatch = useDispatch();
|
|
@@ -18,7 +17,8 @@ export const AuthCheckComponent = ({ authenticationState, /*authUrlBase, redirec
|
|
|
18
17
|
navigate(newPath, { replace: true });
|
|
19
18
|
};
|
|
20
19
|
useEffect(() => {
|
|
21
|
-
if (!authenticationState.payload && !authenticationState.options?.loading) {
|
|
20
|
+
// if (!authenticationState.payload && !authenticationState.options?.loading) {
|
|
21
|
+
if (!new AuthenticationStateHelper({ state: authenticationState }).authenticated && !authenticationState.options?.loading) {
|
|
22
22
|
if (authUuid) {
|
|
23
23
|
dispatch(authenticationStateLoadFromApi({
|
|
24
24
|
baseApiUrl,
|
|
@@ -31,7 +31,8 @@ export const AuthCheckComponent = ({ authenticationState, /*authUrlBase, redirec
|
|
|
31
31
|
}));
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
-
else if (authenticationState.payload && authUuid) {
|
|
34
|
+
// else if (authenticationState.payload && authUuid) {
|
|
35
|
+
else if (new AuthenticationStateHelper({ state: authenticationState }).authenticated && authUuid) {
|
|
35
36
|
// caso o usuario ja tenha logado e tente acessar a pagina de autenticacao, o redirect manda o authUuid
|
|
36
37
|
// nesse cenario, deve-se eliminar o authUuid da url
|
|
37
38
|
// isso vale para autenticacao em um dominio diferente do dominio do autenticador
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authCheck.js","sourceRoot":"","sources":["../../../src/auth/authCheck.tsx"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"authCheck.js","sourceRoot":"","sources":["../../../src/auth/authCheck.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAsC,MAAM,0BAA0B,CAAC;AAClG,OAAO,KAAK,EAAE,EAAa,SAAS,EAAE,MAAM,OAAO,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,EAAE,yBAAyB,EAAE,8BAA8B,EAAwB,MAAM,wBAAwB,CAAC;AACzH,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAQxC,MAAM,CAAC,MAAM,kBAAkB,GAAuC,CAAC,EAAE,mBAAmB,EAAE,UAAU,EAAE,QAAQ,EAAE,EAAE,EAAE;IACtH,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,eAAe,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAA;IACxD,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,SAAS,CAAC,uBAAuB,CAAC,CAAA;IAEnE,MAAM,eAAe,GAAG,GAAG,EAAE;QAC3B,WAAW,CAAC,MAAM,CAAC,SAAS,CAAC,uBAAuB,CAAC,CAAC;QACtD,MAAM,SAAS,GAAG,WAAW,CAAC,QAAQ,EAAE,CAAC;QACzC,MAAM,OAAO,GAAG,GAAG,QAAQ,CAAC,QAAQ,GAAG,SAAS,CAAC,CAAC,CAAC,IAAI,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;QAC1E,QAAQ,CAAC,OAAO,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;IACvC,CAAC,CAAA;IAED,SAAS,CAAC,GAAG,EAAE;QACb,+EAA+E;QAC/E,IAAI,CAAC,IAAI,yBAAyB,CAAC,EAAE,KAAK,EAAE,mBAAmB,EAAE,CAAC,CAAC,aAAa,IAAI,CAAC,mBAAmB,CAAC,OAAO,EAAE,OAAO,EAAE,CAAC;YAC1H,IAAI,QAAQ,EAAE,CAAC;gBACb,QAAQ,CAAC,8BAA8B,CAAC;oBACtC,UAAU;oBACV,cAAc,EAAE;wBACd,IAAI,EAAE,kBAAkB,CAAC,IAAI;wBAC7B,IAAI,EAAE,QAAQ;qBACuB;oBACvC,SAAS,EAAE,eAAe;oBAC1B,OAAO,EAAE,eAAe;iBACzB,CAAC,CAAC,CAAA;YACL,CAAC;QACH,CAAC;QACD,sDAAsD;aACjD,IAAI,IAAI,yBAAyB,CAAC,EAAE,KAAK,EAAE,mBAAmB,EAAE,CAAC,CAAC,aAAa,IAAI,QAAQ,EAAE,CAAC;YACjG,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,0CAAG,QAAQ,CAAI,CAAA;AACxB,CAAC,CAAC"}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import React, { useEffect } from "react";
|
|
2
2
|
import { useLocation } from "react-router-dom";
|
|
3
|
+
import { AuthenticationStateHelper } from "./authentication.slice";
|
|
3
4
|
import { AuthUtils } from "./authUtils";
|
|
4
5
|
export const AuthRequiredComponent = ({ authenticationState, authUrlBase, redirectUrl, /*baseApiUrl,*/ children }) => {
|
|
5
6
|
const location = useLocation();
|
|
6
7
|
const queryParams = new URLSearchParams(location.search);
|
|
7
8
|
const authUuid = queryParams.get(AuthUtils.authUuidQueryStringName);
|
|
8
9
|
useEffect(() => {
|
|
9
|
-
if (!authenticationState.payload && !authenticationState.options?.loading) {
|
|
10
|
+
// if (!authenticationState.payload && !authenticationState.options?.loading) {
|
|
11
|
+
if (!new AuthenticationStateHelper({ state: authenticationState, }).authenticated && !authenticationState.options?.loading) {
|
|
10
12
|
if (!authUuid) {
|
|
11
13
|
if (!authUrlBase.includes('/login'))
|
|
12
14
|
authUrlBase = `${authUrlBase}/login`;
|
|
@@ -14,7 +16,8 @@ export const AuthRequiredComponent = ({ authenticationState, authUrlBase, redire
|
|
|
14
16
|
}
|
|
15
17
|
}
|
|
16
18
|
}, [authenticationState]);
|
|
17
|
-
if (!authenticationState.payload) {
|
|
19
|
+
// if (!authenticationState.payload) {
|
|
20
|
+
if (!new AuthenticationStateHelper({ state: authenticationState, }).authenticated) {
|
|
18
21
|
return null;
|
|
19
22
|
}
|
|
20
23
|
return React.createElement(React.Fragment, null, children);
|
|
@@ -1 +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;
|
|
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;AAC/C,OAAO,EAAE,yBAAyB,EAAwB,MAAM,wBAAwB,CAAC;AACzF,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,+EAA+E;QAC/E,IAAI,CAAC,IAAI,yBAAyB,CAAC,EAAE,KAAK,EAAE,mBAAmB,GAAG,CAAC,CAAC,aAAa,IAAI,CAAC,mBAAmB,CAAC,OAAO,EAAE,OAAO,EAAE,CAAC;YAC3H,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,sCAAsC;IACtC,IAAI,CAAC,IAAI,yBAAyB,CAAC,EAAE,KAAK,EAAE,mBAAmB,GAAG,CAAC,CAAC,aAAa,EAAE,CAAC;QAClF,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,0CAAG,QAAQ,CAAI,CAAA;AACxB,CAAC,CAAC"}
|
|
@@ -2,6 +2,7 @@ import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
|
|
|
2
2
|
import { AuthenticationsApiClient } from 'datacenter-lib-common-ts';
|
|
3
3
|
import { WebUtils } from 'fwork-jsts-common';
|
|
4
4
|
import { jwtDecode } from 'jwt-decode'; // dont use jsonwebtokens package here, its only for node projects
|
|
5
|
+
import moment from 'moment';
|
|
5
6
|
const cookie = WebUtils.getCookie('@authenticationState');
|
|
6
7
|
const cookieObj = cookie ? JSON.parse(cookie) : null;
|
|
7
8
|
const initState = cookieObj || {};
|
|
@@ -65,4 +66,21 @@ export const authenticationSlice = createSlice({
|
|
|
65
66
|
});
|
|
66
67
|
export const { logout: authenticationStateLogout, set: authenticationStateSet } = authenticationSlice.actions;
|
|
67
68
|
export default authenticationSlice.reducer;
|
|
69
|
+
export class AuthenticationStateHelper {
|
|
70
|
+
constructor(args) {
|
|
71
|
+
this.state = args.state;
|
|
72
|
+
}
|
|
73
|
+
get authenticated() {
|
|
74
|
+
let result = false;
|
|
75
|
+
if (this.state.payload?.token)
|
|
76
|
+
try {
|
|
77
|
+
var decToken = jwtDecode(this.state.payload?.token);
|
|
78
|
+
result = (decToken.exp || 0) >= moment().unix();
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
result = false;
|
|
82
|
+
}
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
68
86
|
//# sourceMappingURL=authentication.slice.js.map
|
|
@@ -1 +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,OAAO,EAAE,wBAAwB,EAAsK,MAAM,0BAA0B,CAAC;AACxO,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC,CAAC,kEAAkE;
|
|
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,OAAO,EAAE,wBAAwB,EAAsK,MAAM,0BAA0B,CAAC;AACxO,OAAO,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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;AAE1C,MAAM,OAAO,yBAAyB;IAGpC,YAAY,IAEX;QACC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAA;IACzB,CAAC;IAED,IAAI,aAAa;QACf,IAAI,MAAM,GAAG,KAAK,CAAA;QAElB,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK;YAC3B,IAAI,CAAC;gBACH,IAAI,QAAQ,GAA6B,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAA;gBAC7E,MAAM,GAAG,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC,CAAC,IAAI,MAAM,EAAE,CAAC,IAAI,EAAE,CAAA;YACjD,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,MAAM,GAAG,KAAK,CAAA;YAChB,CAAC;QAEH,OAAO,MAAM,CAAA;IACf,CAAC;CACF"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AuthCheckComponent, } from './auth/authCheck';
|
|
2
|
-
import authenticationReducer, { authenticationStateLoadFromApi, authenticationStateLogout, authenticationSlice, authenticationStateSet, } from './auth/authentication.slice';
|
|
2
|
+
import authenticationReducer, { authenticationStateLoadFromApi, AuthenticationStateHelper, authenticationStateLogout, authenticationSlice, authenticationStateSet, } from './auth/authentication.slice';
|
|
3
3
|
import { AuthRequiredComponent, } from './auth/authRequired';
|
|
4
4
|
import { AuthUtils, } from './auth/authUtils';
|
|
5
5
|
import { UserOptionsComponent, } from './auth/userOptions';
|
|
6
|
-
export { AuthCheckComponent, authenticationReducer, authenticationStateLoadFromApi, authenticationStateLogout, authenticationSlice, authenticationStateSet, AuthRequiredComponent, AuthUtils, UserOptionsComponent, };
|
|
6
|
+
export { AuthCheckComponent, authenticationReducer, authenticationStateLoadFromApi, AuthenticationStateHelper, authenticationStateLogout, authenticationSlice, authenticationStateSet, AuthRequiredComponent, AuthUtils, UserOptionsComponent, };
|
|
7
7
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +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;
|
|
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,EAAE,yBAAyB,EAA4C,yBAAyB,EAAE,mBAAmB,EAAE,sBAAsB,GAAG,MAAM,6BAA6B,CAAA;AACjP,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,EAAE,yBAAyB,EAA4C,yBAAyB,EAAE,mBAAmB,EAAE,sBAAsB,EAClM,qBAAqB,EACrB,SAAS,EACT,oBAAoB,GACrB,CAAA"}
|
|
@@ -33,3 +33,10 @@ export declare const authenticationSlice: import("@reduxjs/toolkit").Slice<IAuth
|
|
|
33
33
|
export declare const authenticationStateLogout: import("@reduxjs/toolkit").ActionCreatorWithoutPayload<"authenticationSlice/logout">, authenticationStateSet: import("@reduxjs/toolkit").ActionCreatorWithPayload<any, "authenticationSlice/set">;
|
|
34
34
|
declare const _default: import("redux").Reducer<IAuthenticationState>;
|
|
35
35
|
export default _default;
|
|
36
|
+
export declare class AuthenticationStateHelper {
|
|
37
|
+
state: IAuthenticationState;
|
|
38
|
+
constructor(args: {
|
|
39
|
+
state: IAuthenticationState;
|
|
40
|
+
});
|
|
41
|
+
get authenticated(): boolean;
|
|
42
|
+
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AuthCheckComponent, IAuthCheckComponentProps } from './auth/authCheck';
|
|
2
|
-
import authenticationReducer, { authenticationStateLoadFromApi, IAuthenticationState, IAuthenticationExt, authenticationStateLogout, authenticationSlice, authenticationStateSet } from './auth/authentication.slice';
|
|
2
|
+
import authenticationReducer, { authenticationStateLoadFromApi, AuthenticationStateHelper, IAuthenticationState, IAuthenticationExt, authenticationStateLogout, authenticationSlice, authenticationStateSet } from './auth/authentication.slice';
|
|
3
3
|
import { AuthRequiredComponent, IAuthRequiredComponentProps } from './auth/authRequired';
|
|
4
4
|
import { AuthUtils } from './auth/authUtils';
|
|
5
5
|
import { UserOptionsComponent, IUserOptionsComponentProps } from './auth/userOptions';
|
|
6
|
-
export { AuthCheckComponent, IAuthCheckComponentProps, authenticationReducer, authenticationStateLoadFromApi,
|
|
6
|
+
export { AuthCheckComponent, IAuthCheckComponentProps, authenticationReducer, authenticationStateLoadFromApi, AuthenticationStateHelper, IAuthenticationExt, IAuthenticationState, authenticationStateLogout, authenticationSlice, authenticationStateSet, AuthRequiredComponent, IAuthRequiredComponentProps, AuthUtils, UserOptionsComponent, IUserOptionsComponentProps, };
|
package/package.json
CHANGED
|
@@ -61,7 +61,7 @@
|
|
|
61
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
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
63
|
},
|
|
64
|
-
"version": "1.0.
|
|
64
|
+
"version": "1.0.5",
|
|
65
65
|
"exports": {
|
|
66
66
|
".": {
|
|
67
67
|
"require": "./dist/cjs/index.js",
|
package/src/auth/authCheck.tsx
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
1
|
|
|
2
|
+
import { AuthenticationType, IAuthenticationRequestBodyFromUuid } from "datacenter-lib-common-ts";
|
|
2
3
|
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
4
|
import { useDispatch } from "react-redux";
|
|
8
|
-
import {
|
|
5
|
+
import { useLocation, useNavigate } from "react-router-dom";
|
|
6
|
+
import { AuthenticationStateHelper, authenticationStateLoadFromApi, IAuthenticationState } from "./authentication.slice";
|
|
7
|
+
import { AuthUtils } from "./authUtils";
|
|
9
8
|
|
|
10
9
|
export interface IAuthCheckComponentProps {
|
|
11
10
|
authenticationState: IAuthenticationState,
|
|
12
11
|
baseApiUrl: string,
|
|
13
|
-
// authUrlBase: string,
|
|
14
|
-
// redirectUrl: string,
|
|
15
12
|
children: ReactNode;
|
|
16
13
|
}
|
|
17
14
|
|
|
18
|
-
export const AuthCheckComponent: React.FC<IAuthCheckComponentProps> = ({ authenticationState,
|
|
15
|
+
export const AuthCheckComponent: React.FC<IAuthCheckComponentProps> = ({ authenticationState, baseApiUrl, children }) => {
|
|
19
16
|
const navigate = useNavigate()
|
|
20
17
|
const location = useLocation()
|
|
21
18
|
const dispatch = useDispatch<any>()
|
|
@@ -30,7 +27,8 @@ export const AuthCheckComponent: React.FC<IAuthCheckComponentProps> = ({ authent
|
|
|
30
27
|
}
|
|
31
28
|
|
|
32
29
|
useEffect(() => {
|
|
33
|
-
if (!authenticationState.payload && !authenticationState.options?.loading) {
|
|
30
|
+
// if (!authenticationState.payload && !authenticationState.options?.loading) {
|
|
31
|
+
if (!new AuthenticationStateHelper({ state: authenticationState }).authenticated && !authenticationState.options?.loading) {
|
|
34
32
|
if (authUuid) {
|
|
35
33
|
dispatch(authenticationStateLoadFromApi({
|
|
36
34
|
baseApiUrl,
|
|
@@ -43,7 +41,8 @@ export const AuthCheckComponent: React.FC<IAuthCheckComponentProps> = ({ authent
|
|
|
43
41
|
}))
|
|
44
42
|
}
|
|
45
43
|
}
|
|
46
|
-
else if (authenticationState.payload && authUuid) {
|
|
44
|
+
// else if (authenticationState.payload && authUuid) {
|
|
45
|
+
else if (new AuthenticationStateHelper({ state: authenticationState }).authenticated && authUuid) {
|
|
47
46
|
// caso o usuario ja tenha logado e tente acessar a pagina de autenticacao, o redirect manda o authUuid
|
|
48
47
|
// nesse cenario, deve-se eliminar o authUuid da url
|
|
49
48
|
// isso vale para autenticacao em um dominio diferente do dominio do autenticador
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
import React, { ReactNode, useEffect } from "react";
|
|
3
3
|
import { useLocation } from "react-router-dom";
|
|
4
|
-
import { IAuthenticationState } from "./authentication.slice";
|
|
4
|
+
import { AuthenticationStateHelper, IAuthenticationState } from "./authentication.slice";
|
|
5
5
|
import { AuthUtils } from "./authUtils";
|
|
6
6
|
|
|
7
7
|
export interface IAuthRequiredComponentProps {
|
|
@@ -18,7 +18,8 @@ export const AuthRequiredComponent: React.FC<IAuthRequiredComponentProps> = ({ a
|
|
|
18
18
|
const authUuid = queryParams.get(AuthUtils.authUuidQueryStringName)
|
|
19
19
|
|
|
20
20
|
useEffect(() => {
|
|
21
|
-
if (!authenticationState.payload && !authenticationState.options?.loading) {
|
|
21
|
+
// if (!authenticationState.payload && !authenticationState.options?.loading) {
|
|
22
|
+
if (!new AuthenticationStateHelper({ state: authenticationState, }).authenticated && !authenticationState.options?.loading) {
|
|
22
23
|
if (!authUuid) {
|
|
23
24
|
if (!authUrlBase.includes('/login'))
|
|
24
25
|
authUrlBase = `${authUrlBase}/login`
|
|
@@ -27,7 +28,8 @@ export const AuthRequiredComponent: React.FC<IAuthRequiredComponentProps> = ({ a
|
|
|
27
28
|
}
|
|
28
29
|
}, [authenticationState]);
|
|
29
30
|
|
|
30
|
-
if (!authenticationState.payload) {
|
|
31
|
+
// if (!authenticationState.payload) {
|
|
32
|
+
if (!new AuthenticationStateHelper({ state: authenticationState, }).authenticated) {
|
|
31
33
|
return null;
|
|
32
34
|
}
|
|
33
35
|
|
|
@@ -2,6 +2,7 @@ import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
|
|
|
2
2
|
import { AuthenticationsApiClient, IAuthentication, IAuthenticationRequestBodyDefault, IAuthenticationRequestBodyFromGoogleToken, IAuthenticationRequestBodyFromUuid, IAuthenticationTokenData, IUser } from 'datacenter-lib-common-ts';
|
|
3
3
|
import { WebUtils } from 'fwork-jsts-common';
|
|
4
4
|
import { jwtDecode } from 'jwt-decode'; // dont use jsonwebtokens package here, its only for node projects
|
|
5
|
+
import moment from 'moment';
|
|
5
6
|
|
|
6
7
|
export interface IAuthenticationExt extends IAuthentication {
|
|
7
8
|
user: IUser
|
|
@@ -11,7 +12,7 @@ export interface IAuthenticationState {
|
|
|
11
12
|
options?: {
|
|
12
13
|
loading?: boolean,
|
|
13
14
|
},
|
|
14
|
-
payload?: IAuthenticationExt
|
|
15
|
+
payload?: IAuthenticationExt,
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
const cookie = WebUtils.getCookie('@authenticationState')
|
|
@@ -27,7 +28,7 @@ export const authenticationStateLoadFromApi = createAsyncThunk(
|
|
|
27
28
|
|
|
28
29
|
onError?: (msg?: string) => void,
|
|
29
30
|
onSuccess?: (authentication?: IAuthentication) => void,
|
|
30
|
-
|
|
31
|
+
// }, { getState }) => {
|
|
31
32
|
}) => {
|
|
32
33
|
try {
|
|
33
34
|
let result: IAuthenticationState = {}
|
|
@@ -53,7 +54,7 @@ export const authenticationStateLoadFromApi = createAsyncThunk(
|
|
|
53
54
|
if (arg.onError)
|
|
54
55
|
arg.onError('Erro ao tentar autenticar')
|
|
55
56
|
|
|
56
|
-
return
|
|
57
|
+
return
|
|
57
58
|
}
|
|
58
59
|
}
|
|
59
60
|
)
|
|
@@ -94,4 +95,28 @@ export const authenticationSlice = createSlice({
|
|
|
94
95
|
|
|
95
96
|
export const { logout: authenticationStateLogout, set: authenticationStateSet } = authenticationSlice.actions
|
|
96
97
|
|
|
97
|
-
export default authenticationSlice.reducer
|
|
98
|
+
export default authenticationSlice.reducer
|
|
99
|
+
|
|
100
|
+
export class AuthenticationStateHelper {
|
|
101
|
+
state: IAuthenticationState
|
|
102
|
+
|
|
103
|
+
constructor(args: {
|
|
104
|
+
state: IAuthenticationState
|
|
105
|
+
}) {
|
|
106
|
+
this.state = args.state
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
get authenticated(): boolean {
|
|
110
|
+
let result = false
|
|
111
|
+
|
|
112
|
+
if (this.state.payload?.token)
|
|
113
|
+
try {
|
|
114
|
+
var decToken: IAuthenticationTokenData = jwtDecode(this.state.payload?.token)
|
|
115
|
+
result = (decToken.exp || 0) >= moment().unix()
|
|
116
|
+
} catch (error) {
|
|
117
|
+
result = false
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return result
|
|
121
|
+
}
|
|
122
|
+
}
|
package/src/index.tsx
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { AuthCheckComponent, IAuthCheckComponentProps, } from './auth/authCheck'
|
|
2
|
-
import authenticationReducer, { authenticationStateLoadFromApi, IAuthenticationState, IAuthenticationExt, authenticationStateLogout, authenticationSlice, authenticationStateSet, } from './auth/authentication.slice'
|
|
2
|
+
import authenticationReducer, { authenticationStateLoadFromApi, AuthenticationStateHelper, IAuthenticationState, IAuthenticationExt, authenticationStateLogout, authenticationSlice, authenticationStateSet, } from './auth/authentication.slice'
|
|
3
3
|
import { AuthRequiredComponent, IAuthRequiredComponentProps, } from './auth/authRequired'
|
|
4
4
|
import { AuthUtils, } from './auth/authUtils'
|
|
5
5
|
import { UserOptionsComponent, IUserOptionsComponentProps, } from './auth/userOptions'
|
|
6
6
|
|
|
7
7
|
export {
|
|
8
8
|
AuthCheckComponent, IAuthCheckComponentProps,
|
|
9
|
-
authenticationReducer, authenticationStateLoadFromApi,
|
|
9
|
+
authenticationReducer, authenticationStateLoadFromApi, AuthenticationStateHelper, IAuthenticationExt, IAuthenticationState, authenticationStateLogout, authenticationSlice, authenticationStateSet,
|
|
10
10
|
AuthRequiredComponent, IAuthRequiredComponentProps,
|
|
11
11
|
AuthUtils,
|
|
12
12
|
UserOptionsComponent, IUserOptionsComponentProps,
|