@nibssplc/cams-sdk-react 0.0.1-beta.49 → 0.0.1-beta.50
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/README.md +294 -57
- package/dist/components/CAMSProvider.d.ts +6 -11
- package/dist/hooks/useCAMSAuth.d.ts +4 -1
- package/dist/hooks/useCAMSMSALAuth.d.ts +1 -0
- package/dist/index.cjs.js +107 -34
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +107 -34
- package/dist/index.esm.js.map +1 -1
- package/package.json +2 -2
package/dist/index.esm.js
CHANGED
|
@@ -88,19 +88,22 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
88
88
|
|
|
89
89
|
function useCAMSAuth(options) {
|
|
90
90
|
var _this = this;
|
|
91
|
-
if (options === void 0) { options = {
|
|
91
|
+
if (options === void 0) { options = {
|
|
92
|
+
appCode: "",
|
|
93
|
+
storageKey: "CAMS-AUTH-SDK",
|
|
94
|
+
}; }
|
|
92
95
|
var _a = useState(false), isAuthenticated = _a[0], setIsAuthenticated = _a[1];
|
|
93
96
|
var _b = useState(false), isLoading = _b[0], setIsLoading = _b[1];
|
|
94
97
|
var _c = useState(null), error = _c[0], setError = _c[1];
|
|
95
|
-
var _d = useState(
|
|
98
|
+
var _d = useState(""), token = _d[0], setToken = _d[1];
|
|
96
99
|
var _e = useState(null), profile = _e[0], setProfile = _e[1];
|
|
97
100
|
var sessionManagerRef = useRef(null);
|
|
98
101
|
useEffect(function () {
|
|
99
|
-
var _a;
|
|
102
|
+
var _a, _b;
|
|
100
103
|
// Only initialize on client side
|
|
101
|
-
if (typeof window ===
|
|
104
|
+
if (typeof window === "undefined")
|
|
102
105
|
return;
|
|
103
|
-
(_a = sessionManagerRef.current) !== null && _a !== void 0 ? _a : (sessionManagerRef.current = new CAMSSessionManager(localStorage, options.storageKey ||
|
|
106
|
+
(_a = sessionManagerRef.current) !== null && _a !== void 0 ? _a : (sessionManagerRef.current = new CAMSSessionManager(localStorage, options.storageKey || "CAMS-SDK", {
|
|
104
107
|
onAuthSuccess: function (res) {
|
|
105
108
|
var _a;
|
|
106
109
|
setToken(res.userProfile.tokens.Bearer);
|
|
@@ -115,16 +118,16 @@ function useCAMSAuth(options) {
|
|
|
115
118
|
onTokenExpired: function () {
|
|
116
119
|
var _a;
|
|
117
120
|
setIsAuthenticated(false);
|
|
118
|
-
setToken(
|
|
121
|
+
setToken("");
|
|
119
122
|
setProfile(null);
|
|
120
123
|
(_a = options.onTokenExpired) === null || _a === void 0 ? void 0 : _a.call(options);
|
|
121
|
-
}
|
|
124
|
+
},
|
|
122
125
|
}));
|
|
123
126
|
// Check initial auth state
|
|
124
127
|
var initialAuth = sessionManagerRef.current.isAuthenticated();
|
|
125
128
|
setIsAuthenticated(initialAuth);
|
|
126
129
|
if (initialAuth) {
|
|
127
|
-
setToken(sessionManagerRef.current.getAccessToken());
|
|
130
|
+
setToken((_b = sessionManagerRef.current.getAccessToken()) !== null && _b !== void 0 ? _b : "");
|
|
128
131
|
sessionManagerRef.current.getProfile().then(setProfile);
|
|
129
132
|
}
|
|
130
133
|
}, [options.storageKey]);
|
|
@@ -170,7 +173,7 @@ function useCAMSAuth(options) {
|
|
|
170
173
|
case 1:
|
|
171
174
|
_a.sent();
|
|
172
175
|
setIsAuthenticated(false);
|
|
173
|
-
setToken(
|
|
176
|
+
setToken("");
|
|
174
177
|
setProfile(null);
|
|
175
178
|
setError(null);
|
|
176
179
|
return [2 /*return*/];
|
|
@@ -184,7 +187,9 @@ function useCAMSAuth(options) {
|
|
|
184
187
|
isLoading: isLoading,
|
|
185
188
|
error: error,
|
|
186
189
|
token: token,
|
|
187
|
-
profile: profile
|
|
190
|
+
profile: profile,
|
|
191
|
+
appCode: options.appCode,
|
|
192
|
+
storageKey: options.storageKey || "CAMS-SDK",
|
|
188
193
|
};
|
|
189
194
|
}
|
|
190
195
|
|
|
@@ -16692,17 +16697,16 @@ function useCAMSMSALAuth(options) {
|
|
|
16692
16697
|
var _this = this;
|
|
16693
16698
|
if (options === void 0) { options = {
|
|
16694
16699
|
mfaUrl: "/auth/multi-factor",
|
|
16700
|
+
storageKey: "CAMS-MSAL-AUTH-SDK",
|
|
16695
16701
|
}; }
|
|
16696
16702
|
var _a = useMsal(), instance = _a.instance, inProgress = _a.inProgress, accounts = _a.accounts;
|
|
16697
16703
|
var account = useAccount(accounts[0] || {});
|
|
16698
|
-
var storageKey = "CAMS-MSAL-AUTH-SDK";
|
|
16699
16704
|
var _b = useState(null), error = _b[0], setError = _b[1];
|
|
16700
|
-
var _c = useState(""),
|
|
16701
|
-
var _d = useState(""),
|
|
16702
|
-
var _e = useState(""),
|
|
16703
|
-
var _f = useState(""), accessToken = _f[0], setAccessToken = _f[1];
|
|
16705
|
+
var _c = useState(""), idToken = _c[0], setIdToken = _c[1];
|
|
16706
|
+
var _d = useState(""), appCode = _d[0], setAppCode = _d[1];
|
|
16707
|
+
var _e = useState(""), accessToken = _e[0], setAccessToken = _e[1];
|
|
16704
16708
|
var isLoading = inProgress !== InteractionStatus.None;
|
|
16705
|
-
var isAuthenticated = !!account && !!
|
|
16709
|
+
var isAuthenticated = !!account && !!accessToken;
|
|
16706
16710
|
var scopes = options.scopes || ["openid", "profile", "email"];
|
|
16707
16711
|
var isTokenValid = function (token) {
|
|
16708
16712
|
try {
|
|
@@ -16714,25 +16718,24 @@ function useCAMSMSALAuth(options) {
|
|
|
16714
16718
|
}
|
|
16715
16719
|
};
|
|
16716
16720
|
useEffect(function () {
|
|
16717
|
-
if (typeof window !== "undefined" && !
|
|
16718
|
-
var stored = localStorage.getItem(storageKey);
|
|
16721
|
+
if (typeof window !== "undefined" && !accessToken) {
|
|
16722
|
+
var stored = localStorage.getItem(options.storageKey);
|
|
16719
16723
|
if (stored) {
|
|
16720
16724
|
try {
|
|
16721
16725
|
var _a = JSON.parse(stored), accessToken_1 = _a.accessToken, idToken_1 = _a.idToken, appCode_1 = _a.appCode;
|
|
16722
16726
|
if (accessToken_1 && isTokenValid(accessToken_1)) {
|
|
16723
|
-
setToken(accessToken_1);
|
|
16724
16727
|
setAccessToken(accessToken_1);
|
|
16725
16728
|
setIdToken(idToken_1);
|
|
16726
16729
|
setAppCode(appCode_1);
|
|
16727
16730
|
}
|
|
16728
16731
|
else {
|
|
16729
|
-
localStorage.removeItem(storageKey);
|
|
16732
|
+
localStorage.removeItem(options.storageKey);
|
|
16730
16733
|
}
|
|
16731
16734
|
}
|
|
16732
16735
|
catch (_b) { }
|
|
16733
16736
|
}
|
|
16734
16737
|
}
|
|
16735
|
-
}, [
|
|
16738
|
+
}, [accessToken]);
|
|
16736
16739
|
// useEffect(() => {
|
|
16737
16740
|
// const handleRedirect = async () => {
|
|
16738
16741
|
// try {
|
|
@@ -16786,12 +16789,11 @@ function useCAMSMSALAuth(options) {
|
|
|
16786
16789
|
accessToken: accessToken,
|
|
16787
16790
|
idToken: idToken,
|
|
16788
16791
|
});
|
|
16789
|
-
setToken(response.accessToken);
|
|
16790
16792
|
setAccessToken(response.accessToken);
|
|
16791
16793
|
setIdToken(response.idToken);
|
|
16792
16794
|
// Persist tokens to localStorage
|
|
16793
16795
|
if (typeof window !== "undefined") {
|
|
16794
|
-
localStorage.setItem(storageKey, JSON.stringify({
|
|
16796
|
+
localStorage.setItem(options.storageKey, JSON.stringify({
|
|
16795
16797
|
isAuthenticated: true,
|
|
16796
16798
|
accessToken: response.accessToken,
|
|
16797
16799
|
idToken: response.idToken,
|
|
@@ -16838,14 +16840,16 @@ function useCAMSMSALAuth(options) {
|
|
|
16838
16840
|
var listener_1 = function (event) {
|
|
16839
16841
|
if (event.source !== authWindow_1)
|
|
16840
16842
|
return;
|
|
16841
|
-
var allowedOrigins = options.allowedOrigins ||
|
|
16842
|
-
|
|
16843
|
-
|
|
16844
|
-
|
|
16843
|
+
var allowedOrigins = options.allowedOrigins ||
|
|
16844
|
+
[
|
|
16845
|
+
options.messageOrigin || new URL(options.mfaUrl || "").origin,
|
|
16846
|
+
].filter(Boolean);
|
|
16847
|
+
if (allowedOrigins.length > 0 &&
|
|
16848
|
+
!allowedOrigins.includes(event.origin))
|
|
16845
16849
|
return;
|
|
16846
16850
|
var tokenMsg = ProfileSchema.safeParse(event.data);
|
|
16847
16851
|
if (tokenMsg.success) {
|
|
16848
|
-
localStorage.setItem(storageKey, JSON.stringify({
|
|
16852
|
+
localStorage.setItem(options.storageKey, JSON.stringify({
|
|
16849
16853
|
isAuthenticated: true,
|
|
16850
16854
|
accessToken: response.accessToken,
|
|
16851
16855
|
idToken: response.idToken,
|
|
@@ -16907,13 +16911,12 @@ function useCAMSMSALAuth(options) {
|
|
|
16907
16911
|
return [4 /*yield*/, instance.logoutRedirect()];
|
|
16908
16912
|
case 1:
|
|
16909
16913
|
_a.sent();
|
|
16910
|
-
setToken("");
|
|
16911
16914
|
setAccessToken("");
|
|
16912
16915
|
setIdToken("");
|
|
16913
16916
|
setAppCode("");
|
|
16914
16917
|
setError(null);
|
|
16915
16918
|
if (typeof window !== "undefined") {
|
|
16916
|
-
localStorage.removeItem(storageKey);
|
|
16919
|
+
localStorage.removeItem(options.storageKey);
|
|
16917
16920
|
}
|
|
16918
16921
|
return [3 /*break*/, 3];
|
|
16919
16922
|
case 2:
|
|
@@ -16928,7 +16931,7 @@ function useCAMSMSALAuth(options) {
|
|
|
16928
16931
|
return {
|
|
16929
16932
|
login: login,
|
|
16930
16933
|
logout: logout,
|
|
16931
|
-
storageKey: storageKey,
|
|
16934
|
+
storageKey: options.storageKey,
|
|
16932
16935
|
isAuthenticated: isAuthenticated,
|
|
16933
16936
|
isLoading: isLoading,
|
|
16934
16937
|
error: error,
|
|
@@ -17358,11 +17361,81 @@ if (process.env.NODE_ENV === 'production') {
|
|
|
17358
17361
|
var jsxRuntimeExports = jsxRuntime.exports;
|
|
17359
17362
|
|
|
17360
17363
|
var CAMSContext = createContext(null);
|
|
17364
|
+
var setCookie$1 = function (name, value, days) {
|
|
17365
|
+
var expires = new Date(Date.now() + days * 864e5).toUTCString();
|
|
17366
|
+
document.cookie = "".concat(name, "=").concat(encodeURIComponent(value), "; expires=").concat(expires, "; path=/; samesite=Lax");
|
|
17367
|
+
};
|
|
17368
|
+
var getCookie$1 = function (name) {
|
|
17369
|
+
var _a;
|
|
17370
|
+
return ((_a = document.cookie
|
|
17371
|
+
.split("; ")
|
|
17372
|
+
.find(function (row) { return row.startsWith(name + "="); })) === null || _a === void 0 ? void 0 : _a.split("=")[1])
|
|
17373
|
+
? decodeURIComponent(document.cookie
|
|
17374
|
+
.split("; ")
|
|
17375
|
+
.find(function (row) { return row.startsWith(name + "="); })
|
|
17376
|
+
.split("=")[1])
|
|
17377
|
+
: null;
|
|
17378
|
+
};
|
|
17379
|
+
var deleteCookie$1 = function (name) {
|
|
17380
|
+
document.cookie = name + "=; Max-Age=-99999999; path=/";
|
|
17381
|
+
};
|
|
17361
17382
|
function CAMSProvider(_a) {
|
|
17362
|
-
var
|
|
17383
|
+
var _this = this;
|
|
17384
|
+
var children = _a.children, authOptions = __rest(_a, ["children"]);
|
|
17363
17385
|
var auth = useCAMSAuth(authOptions);
|
|
17364
|
-
var
|
|
17365
|
-
var
|
|
17386
|
+
var profileStorageKey = "".concat(auth.storageKey, "-PROFILE");
|
|
17387
|
+
var getInitialProfile = function () {
|
|
17388
|
+
if (typeof window === "undefined") {
|
|
17389
|
+
return null;
|
|
17390
|
+
}
|
|
17391
|
+
try {
|
|
17392
|
+
var storedProfile = getCookie$1(profileStorageKey);
|
|
17393
|
+
return storedProfile ? JSON.parse(storedProfile) : null;
|
|
17394
|
+
}
|
|
17395
|
+
catch (_a) {
|
|
17396
|
+
return null;
|
|
17397
|
+
}
|
|
17398
|
+
};
|
|
17399
|
+
var _b = useState(getInitialProfile), userProfile = _b[0], setUserProfile = _b[1];
|
|
17400
|
+
// Load profile from storage on mount
|
|
17401
|
+
useEffect(function () {
|
|
17402
|
+
if (typeof window !== "undefined") {
|
|
17403
|
+
var storedProfile = getCookie$1(profileStorageKey);
|
|
17404
|
+
if (storedProfile) {
|
|
17405
|
+
try {
|
|
17406
|
+
setUserProfile(JSON.parse(storedProfile));
|
|
17407
|
+
}
|
|
17408
|
+
catch (_a) { }
|
|
17409
|
+
}
|
|
17410
|
+
}
|
|
17411
|
+
}, [profileStorageKey]);
|
|
17412
|
+
// Persist profile separately
|
|
17413
|
+
useEffect(function () {
|
|
17414
|
+
if (typeof window !== "undefined") {
|
|
17415
|
+
if (userProfile) {
|
|
17416
|
+
setCookie$1(profileStorageKey, JSON.stringify(userProfile), 1);
|
|
17417
|
+
}
|
|
17418
|
+
else {
|
|
17419
|
+
deleteCookie$1(profileStorageKey);
|
|
17420
|
+
}
|
|
17421
|
+
}
|
|
17422
|
+
}, [userProfile, profileStorageKey]);
|
|
17423
|
+
// Enhanced logout that also clears profile
|
|
17424
|
+
var enhancedLogout = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
17425
|
+
return __generator(this, function (_a) {
|
|
17426
|
+
switch (_a.label) {
|
|
17427
|
+
case 0: return [4 /*yield*/, auth.logout()];
|
|
17428
|
+
case 1:
|
|
17429
|
+
_a.sent();
|
|
17430
|
+
setUserProfile(null);
|
|
17431
|
+
if (typeof window !== "undefined") {
|
|
17432
|
+
deleteCookie$1(profileStorageKey);
|
|
17433
|
+
}
|
|
17434
|
+
return [2 /*return*/];
|
|
17435
|
+
}
|
|
17436
|
+
});
|
|
17437
|
+
}); };
|
|
17438
|
+
var value = useMemo(function () { return (__assign(__assign({}, auth), { logout: enhancedLogout, userProfile: userProfile, setUserProfile: setUserProfile })); }, [auth, userProfile]);
|
|
17366
17439
|
return (jsxRuntimeExports.jsx(CAMSContext.Provider, { value: value, children: children }));
|
|
17367
17440
|
}
|
|
17368
17441
|
function useCAMSContext() {
|