@onecx/react-auth 8.0.0-rc.10

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/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './lib/axios';
2
+ export * from './lib/auth-proxy-service';
package/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("axios"),c=require("@onecx/accelerator"),d=c.createLoggerFactory("@onecx/react-auth"),n="No authServiceWrapper provided. Please update to the latest shell version to use the new auth mechanism.";class l{logger=d("AuthServiceProxy");getHeaderValues(){return c.ensureProperty(globalThis,["onecxAuth","authServiceProxy","v1","getHeaderValues"],()=>({})).onecxAuth.authServiceProxy.v1.getHeaderValues()}async updateTokenIfNeeded(){return c.ensureProperty(globalThis,["onecxAuth","authServiceProxy","v1","updateTokenIfNeeded"],()=>Promise.reject(new Error(n))).onecxAuth.authServiceProxy.v1.updateTokenIfNeeded().catch(t=>{throw this.logger.error("Error updating token:",t),t})}}const u=new l,x=["/assets/"],y=a=>{const o=a?s.create({baseURL:a}):s.create(),t=Object.assign(o,{tokens:{sessionExpired:!1}});return t.interceptors.request.use(async e=>{if(x.some(r=>e.url?.includes(r)))return e;try{await u.updateTokenIfNeeded()}catch(r){if(r.message!==n)throw r}const i=u.getHeaderValues();return i&&(e.headers||(e.headers=new s.AxiosHeaders),Object.entries(i).forEach(([r,h])=>{h&&e.headers.set(r,h)})),e},e=>Promise.reject(e)),t};exports.AuthServiceProxy=l;exports.MISSING_PROXY_ERROR=n;exports.authServiceProxy=u;exports.axiosFactory=y;
package/index.mjs ADDED
@@ -0,0 +1,59 @@
1
+ import n, { AxiosHeaders as d } from "axios";
2
+ import { createLoggerFactory as l, ensureProperty as u } from "@onecx/accelerator";
3
+ const x = l("@onecx/react-auth"), h = "No authServiceWrapper provided. Please update to the latest shell version to use the new auth mechanism.";
4
+ class g {
5
+ logger = x("AuthServiceProxy");
6
+ /**
7
+ * Get current auth header values from the proxy.
8
+ * @returns map of header names to values.
9
+ */
10
+ getHeaderValues() {
11
+ return u(
12
+ globalThis,
13
+ ["onecxAuth", "authServiceProxy", "v1", "getHeaderValues"],
14
+ () => ({})
15
+ ).onecxAuth.authServiceProxy.v1.getHeaderValues();
16
+ }
17
+ /**
18
+ * Ensure auth token is up to date.
19
+ * @returns true when token is valid or refreshed.
20
+ * @throws Error when proxy is missing or token refresh fails.
21
+ */
22
+ async updateTokenIfNeeded() {
23
+ return u(
24
+ globalThis,
25
+ ["onecxAuth", "authServiceProxy", "v1", "updateTokenIfNeeded"],
26
+ () => Promise.reject(new Error(h))
27
+ ).onecxAuth.authServiceProxy.v1.updateTokenIfNeeded().catch((t) => {
28
+ throw this.logger.error("Error updating token:", t), t;
29
+ });
30
+ }
31
+ }
32
+ const i = new g(), p = ["/assets/"], P = (a) => {
33
+ const o = a ? n.create({ baseURL: a }) : n.create(), t = Object.assign(o, {
34
+ tokens: { sessionExpired: !1 }
35
+ });
36
+ return t.interceptors.request.use(
37
+ async (e) => {
38
+ if (p.some((r) => e.url?.includes(r)))
39
+ return e;
40
+ try {
41
+ await i.updateTokenIfNeeded();
42
+ } catch (r) {
43
+ if (r.message !== h)
44
+ throw r;
45
+ }
46
+ const s = i.getHeaderValues();
47
+ return s && (e.headers || (e.headers = new d()), Object.entries(s).forEach(([r, c]) => {
48
+ c && e.headers.set(r, c);
49
+ })), e;
50
+ },
51
+ (e) => Promise.reject(e)
52
+ ), t;
53
+ };
54
+ export {
55
+ g as AuthServiceProxy,
56
+ h as MISSING_PROXY_ERROR,
57
+ i as authServiceProxy,
58
+ P as axiosFactory
59
+ };
@@ -0,0 +1,21 @@
1
+ /** Error message when auth service proxy is not available. */
2
+ export declare const MISSING_PROXY_ERROR = "No authServiceWrapper provided. Please update to the latest shell version to use the new auth mechanism.";
3
+ /**
4
+ * Proxy wrapper for auth service exposed on the global namespace.
5
+ */
6
+ export declare class AuthServiceProxy {
7
+ private readonly logger;
8
+ /**
9
+ * Get current auth header values from the proxy.
10
+ * @returns map of header names to values.
11
+ */
12
+ getHeaderValues(): Record<string, string>;
13
+ /**
14
+ * Ensure auth token is up to date.
15
+ * @returns true when token is valid or refreshed.
16
+ * @throws Error when proxy is missing or token refresh fails.
17
+ */
18
+ updateTokenIfNeeded(): Promise<boolean>;
19
+ }
20
+ /** Singleton auth service proxy instance. */
21
+ export declare const authServiceProxy: AuthServiceProxy;
package/lib/axios.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ import { AxiosInstance } from 'axios';
2
+ /**
3
+ * Axios instance extended with token metadata.
4
+ */
5
+ export interface AuthenticatedAxiosInstance extends AxiosInstance {
6
+ tokens: {
7
+ [key: string]: boolean | string;
8
+ };
9
+ }
10
+ /**
11
+ * Create an axios instance for client-side requests to the BFF.
12
+ * @param baseURL - optional base URL for the axios instance.
13
+ * @returns authenticated axios instance with interceptors.
14
+ */
15
+ export declare const axiosFactory: (baseURL?: string) => AuthenticatedAxiosInstance;
@@ -0,0 +1,13 @@
1
+ declare global {
2
+ interface Window {
3
+ onecxAuth?: {
4
+ authServiceProxy?: {
5
+ v1?: {
6
+ getHeaderValues: () => Record<string, string>;
7
+ updateTokenIfNeeded: () => Promise<boolean>;
8
+ };
9
+ };
10
+ };
11
+ }
12
+ }
13
+ export default globalThis;
@@ -0,0 +1 @@
1
+ export declare const createLogger: import('../../../../accelerator/src/index.ts').CreateLogger;
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@onecx/react-auth",
3
+ "version": "8.0.0-rc.10",
4
+ "license": "Apache-2.0",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/onecx/onecx-portal-ui-libs"
8
+ },
9
+ "main": "./index.js",
10
+ "types": "./index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./index.mjs",
14
+ "require": "./index.js"
15
+ }
16
+ },
17
+ "peerDependencies": {
18
+ "@onecx/accelerator": "^8.0.0-rc.10",
19
+ "react": "^19.2.4",
20
+ "react-dom": "^19.2.4",
21
+ "axios": "^1.13.2",
22
+ "keycloak-js": "^26.2.2"
23
+ },
24
+ "dependencies": {},
25
+ "publishConfig": {
26
+ "access": "public"
27
+ }
28
+ }