mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-30 11:08:12 +00:00
fix um
This commit is contained in:
@@ -18,6 +18,6 @@ export const verifyMfaRequest = async (payload: {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const getMeRequest = async () => {
|
export const getMeRequest = async () => {
|
||||||
const response = await api.get<AuthUser>("/auth/me");
|
const response = await api.get<AuthUser>("/me");
|
||||||
return response.data;
|
return response.data;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,23 +1,34 @@
|
|||||||
import { useEffect, useRef } from "react";
|
import { useEffect, useRef } from 'react';
|
||||||
import { createRoot, type Root } from "react-dom/client";
|
import { createRoot, type Root } from 'react-dom/client';
|
||||||
import {
|
import {
|
||||||
UserManagementApp,
|
UserManagementApp,
|
||||||
type UserManagementRuntimeOptions,
|
type UserManagementRuntimeOptions,
|
||||||
type UserManagementSessionSeed,
|
type UserManagementSessionSeed,
|
||||||
} from "@tria-plc/iamui";
|
} from '@tria-plc/iamui';
|
||||||
|
import { iamConfig } from './iamConfig';
|
||||||
|
|
||||||
import { getCookie } from "@/auth/cookies";
|
function readCookieValue(name: string): string | null {
|
||||||
|
const escaped = name.replace(/([.$?*|{}()[\]\\/+^])/g, '\\$1');
|
||||||
|
const match = document.cookie.match(
|
||||||
|
new RegExp(`(?:^|; )${escaped}=([^;]*)`),
|
||||||
|
);
|
||||||
|
|
||||||
import { iamConfig } from "./iamConfig";
|
return match ? decodeURIComponent(match[1]) : null;
|
||||||
|
}
|
||||||
|
|
||||||
function readInitialSession(): UserManagementSessionSeed | null {
|
function readInitialSession(): UserManagementSessionSeed | null {
|
||||||
const token = getCookie("auth-token");
|
const token =
|
||||||
|
localStorage.getItem('fhc-backoffice-auth-token') ??
|
||||||
|
readCookieValue('auth-token');
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const refreshToken = getCookie("refresh-token") ?? undefined;
|
const refreshToken =
|
||||||
|
localStorage.getItem('fhc-backoffice-auth-refresh-token') ??
|
||||||
|
readCookieValue('refresh-token') ??
|
||||||
|
undefined;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
token,
|
token,
|
||||||
@@ -47,15 +58,14 @@ export default function UserManagementHostPage() {
|
|||||||
rootRef.current = createRoot(mountNode);
|
rootRef.current = createRoot(mountNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
const apiBaseUrl = import.meta.env.VITE_BASE_API_URL.replace(/\/+$/, "");
|
const apiBaseUrl = import.meta.env.VITE_BASE_API_URL.replace(/\/+$/, '');
|
||||||
const iamApiUrl = "/um-api";
|
|
||||||
const runtime: UserManagementRuntimeOptions = {
|
const runtime: UserManagementRuntimeOptions = {
|
||||||
basename: "/um",
|
basename: '/um',
|
||||||
apiBaseUrl,
|
apiBaseUrl,
|
||||||
apiUrl: iamApiUrl,
|
apiUrl: `${apiBaseUrl}/api`,
|
||||||
recordApiUrl: iamApiUrl,
|
recordApiUrl: `${apiBaseUrl}/api`,
|
||||||
chronicleUrl: iamApiUrl,
|
chronicleUrl: `${apiBaseUrl}/api`,
|
||||||
auditApiUrl: iamApiUrl,
|
auditApiUrl: `${apiBaseUrl}/api`,
|
||||||
};
|
};
|
||||||
|
|
||||||
rootRef.current.render(
|
rootRef.current.render(
|
||||||
@@ -78,5 +88,5 @@ export default function UserManagementHostPage() {
|
|||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return <div ref={mountRef} style={{ position: "fixed", inset: 0 }} />;
|
return <div ref={mountRef} style={{ position: 'fixed', inset: 0 }} />;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,97 +11,9 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|||||||
const require = createRequire(import.meta.url);
|
const require = createRequire(import.meta.url);
|
||||||
const streamBrowserifyPath = require.resolve("stream-browserify");
|
const streamBrowserifyPath = require.resolve("stream-browserify");
|
||||||
|
|
||||||
function createIamApiAdapter(apiBaseUrl: string): Plugin {
|
|
||||||
const upstreamBaseUrl = `${apiBaseUrl.replace(/\/+$/, "")}/api`;
|
|
||||||
|
|
||||||
return {
|
|
||||||
name: "iam-api-adapter",
|
|
||||||
configureServer(server) {
|
|
||||||
server.middlewares.use("/um-api", async (req, res) => {
|
|
||||||
const requestPath = req.url ?? "/";
|
|
||||||
const normalizedPath = requestPath.replace(/^\/+/, "");
|
|
||||||
const targetUrl = new URL(normalizedPath, `${upstreamBaseUrl}/`);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const headers = new Headers();
|
|
||||||
for (const [key, value] of Object.entries(req.headers)) {
|
|
||||||
if (!value || key.toLowerCase() === "host") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Array.isArray(value)) {
|
|
||||||
for (const item of value) {
|
|
||||||
headers.append(key, item);
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
headers.set(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
const body =
|
|
||||||
req.method === "GET" || req.method === "HEAD"
|
|
||||||
? undefined
|
|
||||||
: await new Promise<Buffer>((resolve, reject) => {
|
|
||||||
const chunks: Buffer[] = [];
|
|
||||||
req.on("data", (chunk) =>
|
|
||||||
chunks.push(
|
|
||||||
Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
req.on("end", () => resolve(Buffer.concat(chunks)));
|
|
||||||
req.on("error", reject);
|
|
||||||
});
|
|
||||||
|
|
||||||
const upstreamResponse = await fetch(targetUrl, {
|
|
||||||
method: req.method,
|
|
||||||
headers,
|
|
||||||
body,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (targetUrl.pathname.endsWith("/auth/me")) {
|
|
||||||
const payload = await upstreamResponse.json();
|
|
||||||
const unwrappedPayload =
|
|
||||||
payload &&
|
|
||||||
typeof payload === "object" &&
|
|
||||||
"success" in payload &&
|
|
||||||
"data" in payload
|
|
||||||
? payload.data
|
|
||||||
: payload;
|
|
||||||
|
|
||||||
res.statusCode = upstreamResponse.status;
|
|
||||||
res.setHeader("content-type", "application/json; charset=utf-8");
|
|
||||||
res.end(JSON.stringify(unwrappedPayload));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
res.statusCode = upstreamResponse.status;
|
|
||||||
upstreamResponse.headers.forEach((value, key) => {
|
|
||||||
res.setHeader(key, value);
|
|
||||||
});
|
|
||||||
res.end(Buffer.from(await upstreamResponse.arrayBuffer()));
|
|
||||||
} catch (error) {
|
|
||||||
server.ssrFixStacktrace(error as Error);
|
|
||||||
res.statusCode = 502;
|
|
||||||
res.setHeader("content-type", "application/json; charset=utf-8");
|
|
||||||
res.end(
|
|
||||||
JSON.stringify({
|
|
||||||
message: "Failed to forward IAM request",
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export default defineConfig(({ mode }) => {
|
export default defineConfig(({ mode }) => {
|
||||||
const env = loadEnv(mode, __dirname, "");
|
|
||||||
const apiBaseUrl =
|
|
||||||
env.VITE_BASE_API_URL?.trim() || "http://localhost:3000";
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
plugins: [react(), tailwindcss(), createIamApiAdapter(apiBaseUrl)],
|
plugins: [react(), tailwindcss()],
|
||||||
resolve: {
|
resolve: {
|
||||||
alias: {
|
alias: {
|
||||||
"@": path.resolve(__dirname, "./src"),
|
"@": path.resolve(__dirname, "./src"),
|
||||||
|
|||||||
Reference in New Issue
Block a user