@enterprisestandard/react 0.0.3-beta.2 → 0.0.3-beta.20251013.2
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/index.js +45 -35
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -34,8 +34,8 @@ var jwksCache = new Map;
|
|
|
34
34
|
function sso(config) {
|
|
35
35
|
const configWithDefaults = {
|
|
36
36
|
...config,
|
|
37
|
-
secure: config.secure !== undefined ? config.secure :
|
|
38
|
-
sameSite: config.sameSite !== undefined ? config.sameSite : "
|
|
37
|
+
secure: config.secure !== undefined ? config.secure : true,
|
|
38
|
+
sameSite: config.sameSite !== undefined ? config.sameSite : "Strict",
|
|
39
39
|
cookiePrefix: config.cookiePrefix ?? `es.sso.${config.client_id}`,
|
|
40
40
|
cookiePath: config.cookiePath ?? "/"
|
|
41
41
|
};
|
|
@@ -45,10 +45,10 @@ function sso(config) {
|
|
|
45
45
|
return;
|
|
46
46
|
}
|
|
47
47
|
try {
|
|
48
|
-
const
|
|
49
|
-
if (!
|
|
48
|
+
const { tokens } = await getTokenFromCookies(request);
|
|
49
|
+
if (!tokens)
|
|
50
50
|
return;
|
|
51
|
-
return await parseUser(
|
|
51
|
+
return await parseUser(tokens);
|
|
52
52
|
} catch (error) {
|
|
53
53
|
console.error("Error parsing user from cookies:", error);
|
|
54
54
|
return;
|
|
@@ -334,7 +334,7 @@ function sso(config) {
|
|
|
334
334
|
const refresh_token = getCookie("refresh", req);
|
|
335
335
|
const control = getCookie("control", req, true);
|
|
336
336
|
if (!access_token || !id_token || !refresh_token || !control) {
|
|
337
|
-
return;
|
|
337
|
+
return { tokens: undefined, refreshHeaders: [] };
|
|
338
338
|
}
|
|
339
339
|
let tokenResponse = {
|
|
340
340
|
access_token,
|
|
@@ -344,14 +344,17 @@ function sso(config) {
|
|
|
344
344
|
};
|
|
345
345
|
if (control.expires && refresh_token && Date.now() > new Date(control.expires).getTime()) {
|
|
346
346
|
tokenResponse = await refreshToken(refresh_token);
|
|
347
|
+
const user = await parseUser(tokenResponse);
|
|
348
|
+
const refreshHeaders = createJwtCookies(tokenResponse, user.sso.expires);
|
|
349
|
+
return { tokens: tokenResponse, refreshHeaders };
|
|
347
350
|
}
|
|
348
|
-
return tokenResponse;
|
|
351
|
+
return { tokens: tokenResponse, refreshHeaders: [] };
|
|
349
352
|
}
|
|
350
353
|
async function getJwt(request) {
|
|
351
|
-
const
|
|
352
|
-
if (!
|
|
354
|
+
const { tokens } = await getTokenFromCookies(request);
|
|
355
|
+
if (!tokens)
|
|
353
356
|
return;
|
|
354
|
-
return
|
|
357
|
+
return tokens.access_token;
|
|
355
358
|
}
|
|
356
359
|
function createCookie(name, value, expires) {
|
|
357
360
|
name = `${configWithDefaults.cookiePrefix}.${name}`;
|
|
@@ -396,32 +399,39 @@ function sso(config) {
|
|
|
396
399
|
return callbackHandler(request);
|
|
397
400
|
}
|
|
398
401
|
if (userUrl === path) {
|
|
399
|
-
const
|
|
400
|
-
if (!
|
|
402
|
+
const { tokens, refreshHeaders } = await getTokenFromCookies(request);
|
|
403
|
+
if (!tokens) {
|
|
401
404
|
return new Response("User not logged in", { status: 401 });
|
|
402
405
|
}
|
|
406
|
+
const user = await parseUser(tokens);
|
|
403
407
|
return new Response(JSON.stringify(user), {
|
|
404
|
-
headers: [["Content-Type", "application/json"]]
|
|
408
|
+
headers: [["Content-Type", "application/json"], ...refreshHeaders]
|
|
405
409
|
});
|
|
406
410
|
}
|
|
407
411
|
if (tokenUrl === path) {
|
|
408
|
-
const
|
|
409
|
-
if (!
|
|
412
|
+
const { tokens, refreshHeaders } = await getTokenFromCookies(request);
|
|
413
|
+
if (!tokens) {
|
|
410
414
|
return new Response("User not logged in", { status: 401 });
|
|
411
415
|
}
|
|
412
416
|
return new Response(JSON.stringify({
|
|
413
|
-
token:
|
|
414
|
-
expires:
|
|
417
|
+
token: tokens.access_token,
|
|
418
|
+
expires: tokens.expires
|
|
415
419
|
}), {
|
|
416
|
-
headers: [["Content-Type", "application/json"]]
|
|
420
|
+
headers: [["Content-Type", "application/json"], ...refreshHeaders]
|
|
417
421
|
});
|
|
418
422
|
}
|
|
419
423
|
if (refreshUrl === path) {
|
|
420
|
-
const
|
|
421
|
-
if (!
|
|
424
|
+
const refresh_token = getCookie("refresh", request);
|
|
425
|
+
if (!refresh_token) {
|
|
422
426
|
return new Response("User not logged in", { status: 401 });
|
|
423
427
|
}
|
|
424
|
-
|
|
428
|
+
const newTokenResponse = await refreshToken(refresh_token);
|
|
429
|
+
const user = await parseUser(newTokenResponse);
|
|
430
|
+
const refreshHeaders = createJwtCookies(newTokenResponse, user.sso.expires);
|
|
431
|
+
return new Response("Refresh Complete", {
|
|
432
|
+
status: 200,
|
|
433
|
+
headers: refreshHeaders
|
|
434
|
+
});
|
|
425
435
|
}
|
|
426
436
|
if (loginUrl === "*" || loginUrl === path) {
|
|
427
437
|
return initiateLogin({
|
|
@@ -610,38 +620,38 @@ async function handler(request, config) {
|
|
|
610
620
|
return sso2.handler(request, config);
|
|
611
621
|
}
|
|
612
622
|
// src/ui/sign-in-loading.tsx
|
|
613
|
-
import {
|
|
623
|
+
import { jsx, Fragment } from "react/jsx-runtime";
|
|
614
624
|
function SignInLoading({ complete = false, children }) {
|
|
615
625
|
const { isLoading } = useUser();
|
|
616
626
|
if (isLoading && !complete)
|
|
617
|
-
return /* @__PURE__ */
|
|
627
|
+
return /* @__PURE__ */ jsx(Fragment, {
|
|
618
628
|
children
|
|
619
|
-
}
|
|
629
|
+
});
|
|
620
630
|
return null;
|
|
621
631
|
}
|
|
622
632
|
// src/ui/signed-in.tsx
|
|
623
|
-
import {
|
|
633
|
+
import { jsx as jsx2, Fragment as Fragment2 } from "react/jsx-runtime";
|
|
624
634
|
function SignedIn({ children }) {
|
|
625
635
|
const { user } = useUser();
|
|
626
636
|
if (user)
|
|
627
|
-
return /* @__PURE__ */
|
|
637
|
+
return /* @__PURE__ */ jsx2(Fragment2, {
|
|
628
638
|
children
|
|
629
|
-
}
|
|
639
|
+
});
|
|
630
640
|
return null;
|
|
631
641
|
}
|
|
632
642
|
// src/ui/signed-out.tsx
|
|
633
|
-
import {
|
|
643
|
+
import { jsx as jsx3, Fragment as Fragment3 } from "react/jsx-runtime";
|
|
634
644
|
function SignedOut({ children }) {
|
|
635
|
-
const { user } = useUser();
|
|
636
|
-
if (user)
|
|
645
|
+
const { user, isLoading } = useUser();
|
|
646
|
+
if (user || isLoading)
|
|
637
647
|
return null;
|
|
638
|
-
return /* @__PURE__ */
|
|
648
|
+
return /* @__PURE__ */ jsx3(Fragment3, {
|
|
639
649
|
children
|
|
640
|
-
}
|
|
650
|
+
});
|
|
641
651
|
}
|
|
642
652
|
// src/ui/sso-provider.tsx
|
|
643
653
|
import { createContext, useCallback, useContext, useEffect, useState } from "react";
|
|
644
|
-
import {
|
|
654
|
+
import { jsx as jsx4 } from "react/jsx-runtime";
|
|
645
655
|
var CTX = createContext(undefined);
|
|
646
656
|
var generateStorageKey = (tenantId) => {
|
|
647
657
|
return `es-sso-user-${tenantId.replace(/[^a-zA-Z0-9]/g, "-").replace(/-+/g, "-").replace(/^-|-$/g, "")}`;
|
|
@@ -776,10 +786,10 @@ function SSOProvider({
|
|
|
776
786
|
tokenUrl,
|
|
777
787
|
refreshUrl
|
|
778
788
|
};
|
|
779
|
-
return /* @__PURE__ */
|
|
789
|
+
return /* @__PURE__ */ jsx4(CTX.Provider, {
|
|
780
790
|
value: contextValue,
|
|
781
791
|
children
|
|
782
|
-
}
|
|
792
|
+
});
|
|
783
793
|
}
|
|
784
794
|
function useUser() {
|
|
785
795
|
const context = useContext(CTX);
|
package/package.json
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@enterprisestandard/react",
|
|
3
|
-
"version": "0.0.3-beta.2",
|
|
3
|
+
"version": "0.0.3-beta.20251013.2",
|
|
4
4
|
"description": "Enterprise Standard React Components",
|
|
5
5
|
"private": false,
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"build": "bun run build.ts"
|
|
9
|
-
"prepublishOnly": "bun run build"
|
|
8
|
+
"build": "bun run build.ts"
|
|
10
9
|
},
|
|
11
10
|
"types": "./dist/index.d.ts",
|
|
12
11
|
"exports": {
|
|
@@ -30,6 +29,7 @@
|
|
|
30
29
|
"access": "public"
|
|
31
30
|
},
|
|
32
31
|
"devDependencies": {
|
|
32
|
+
"@types/react": "^18.0.0",
|
|
33
33
|
"typescript": "^5.0.0"
|
|
34
34
|
},
|
|
35
35
|
"author": "enterprisestandard",
|