Add customer management
This commit is contained in:
28
internal_frontend/lib/api/callApi.ts
Normal file
28
internal_frontend/lib/api/callApi.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
// lib/api/callApi.ts
|
||||
import {serverCall} from "@/lib/api/serverCall";
|
||||
|
||||
export async function callApi<TResponse, TRequest = unknown>(
|
||||
path: string,
|
||||
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH",
|
||||
body?: TRequest
|
||||
): Promise<TResponse> {
|
||||
const res = await serverCall(path, method, body);
|
||||
|
||||
const contentType = res.headers.get("content-type") ?? "";
|
||||
const isJson = contentType.includes("application/json");
|
||||
|
||||
const rawBody = isJson ? await res.json() : await res.text();
|
||||
|
||||
console.log(`[api ${path}] Response:`, res.status, rawBody);
|
||||
|
||||
if (!res.ok) {
|
||||
const errorMessage = isJson
|
||||
? (rawBody?.message ?? rawBody?.errors?.join(", ")) ?? "Unbekannter Fehler"
|
||||
: String(rawBody);
|
||||
|
||||
console.error(`[api ${path}] Error:`, errorMessage);
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
return rawBody as TResponse;
|
||||
}
|
||||
28
internal_frontend/lib/api/serverCall.ts
Normal file
28
internal_frontend/lib/api/serverCall.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
// lib/callBackendApi.ts
|
||||
import {getServerSession} from "next-auth";
|
||||
import {authOptions} from "@/lib/auth/authOptions";
|
||||
|
||||
export async function serverCall(
|
||||
path: string,
|
||||
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH",
|
||||
body?: unknown
|
||||
): Promise<Response> {
|
||||
const url = `${process.env.INTERNAL_BACKEND_URL ?? "http://localhost:8080/api"}${path}`;
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
|
||||
if (session != null) {
|
||||
headers["Authorization"] = `Bearer ${session.accessToken}`;
|
||||
}
|
||||
|
||||
console.log("[api] Calling backend API: ", method, url, body);
|
||||
|
||||
return fetch(url, {
|
||||
method,
|
||||
headers,
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
});
|
||||
}
|
||||
@@ -15,10 +15,10 @@ const {
|
||||
NEXTAUTH_SECRET,
|
||||
} = process.env;
|
||||
|
||||
if (!KEYCLOAK_CLIENT_ID) throw new Error("Missing KEYCLOAK_CLIENT_ID");
|
||||
if (!KEYCLOAK_CLIENT_SECRET) throw new Error("Missing KEYCLOAK_CLIENT_SECRET");
|
||||
if (!KEYCLOAK_ISSUER) throw new Error("Missing KEYCLOAK_ISSUER");
|
||||
if (!NEXTAUTH_SECRET) throw new Error("Missing NEXTAUTH_SECRET");
|
||||
// if (!KEYCLOAK_CLIENT_ID) throw new Error("Missing KEYCLOAK_CLIENT_ID");
|
||||
// if (!KEYCLOAK_CLIENT_SECRET) throw new Error("Missing KEYCLOAK_CLIENT_SECRET");
|
||||
// if (!KEYCLOAK_ISSUER) throw new Error("Missing KEYCLOAK_ISSUER");
|
||||
// if (!NEXTAUTH_SECRET) throw new Error("Missing NEXTAUTH_SECRET");
|
||||
|
||||
console.log("[auth] Using Keycloak provider:");
|
||||
console.log(" - Client ID:", KEYCLOAK_CLIENT_ID);
|
||||
@@ -42,8 +42,8 @@ async function isTokenValid(token: string): Promise<boolean> {
|
||||
export const authOptions: NextAuthOptions = {
|
||||
providers: [
|
||||
KeycloakProvider({
|
||||
clientId: KEYCLOAK_CLIENT_ID,
|
||||
clientSecret: KEYCLOAK_CLIENT_SECRET,
|
||||
clientId: KEYCLOAK_CLIENT_ID as string,
|
||||
clientSecret: KEYCLOAK_CLIENT_SECRET as string,
|
||||
issuer: KEYCLOAK_ISSUER,
|
||||
}),
|
||||
],
|
||||
|
||||
@@ -4,5 +4,6 @@ export const breadcrumbMap: Record<string, string> = {
|
||||
'settings': 'Settings',
|
||||
'demo': 'Demo',
|
||||
'users': 'User Management',
|
||||
'customers': 'Kundenübersicht',
|
||||
// Add more mappings as needed
|
||||
};
|
||||
16
internal_frontend/lib/ui/showError.ts
Normal file
16
internal_frontend/lib/ui/showError.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
// lib/ui/showError.ts
|
||||
import {toast} from "sonner";
|
||||
|
||||
export function showError(error: unknown, fallback = "Ein unbekannter Fehler ist aufgetreten") {
|
||||
let message: string;
|
||||
|
||||
if (error instanceof Error) {
|
||||
message = error.message;
|
||||
} else if (typeof error === "string") {
|
||||
message = error;
|
||||
} else {
|
||||
message = fallback;
|
||||
}
|
||||
|
||||
toast.error(message);
|
||||
}
|
||||
Reference in New Issue
Block a user