Update carousels, card hover, and IranBay branding.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const MESHKEE_API_URL =
|
||||
process.env.MESHKEE_API_URL?.replace(/\/$/, "") ||
|
||||
process.env.NEXT_PUBLIC_MESHKEE_API_URL?.replace(/\/$/, "") ||
|
||||
"https://api.meshkee.com/api/v1";
|
||||
|
||||
type CachedToken = { token: string; expiresAt: number };
|
||||
|
||||
let cachedToken: CachedToken | null = null;
|
||||
|
||||
function tokenExpiry(token: string) {
|
||||
try {
|
||||
const payload = JSON.parse(
|
||||
Buffer.from(token.split(".")[1] ?? "", "base64url").toString(),
|
||||
) as { exp?: number };
|
||||
if (typeof payload.exp === "number") return payload.exp * 1000;
|
||||
} catch {
|
||||
/* ignore malformed tokens */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function usableStaticToken() {
|
||||
const token = process.env.MESHKEE_API_TOKEN?.trim();
|
||||
if (!token) return null;
|
||||
const expiresAt = tokenExpiry(token);
|
||||
if (expiresAt && expiresAt < Date.now() + 15_000) return null;
|
||||
return token;
|
||||
}
|
||||
|
||||
async function serviceToken(): Promise<string | null> {
|
||||
const now = Date.now();
|
||||
if (cachedToken && cachedToken.expiresAt > now + 15_000) {
|
||||
return cachedToken.token;
|
||||
}
|
||||
|
||||
const staticToken = usableStaticToken();
|
||||
if (staticToken) return staticToken;
|
||||
|
||||
const cellNumber = process.env.MESHKEE_SUPERADMIN_CELL?.trim();
|
||||
const password = process.env.MESHKEE_SUPERADMIN_PASSWORD?.trim();
|
||||
if (!cellNumber || !password) return null;
|
||||
|
||||
const response = await fetch(`${MESHKEE_API_URL}/auth/login`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ cellNumber, password }),
|
||||
cache: "no-store",
|
||||
});
|
||||
if (!response.ok) return null;
|
||||
|
||||
const payload = (await response.json().catch(() => null)) as {
|
||||
accessToken?: string;
|
||||
} | null;
|
||||
const token = payload?.accessToken?.trim();
|
||||
if (!token) return null;
|
||||
|
||||
cachedToken = {
|
||||
token,
|
||||
expiresAt: tokenExpiry(token) || now + 10 * 60 * 1000,
|
||||
};
|
||||
return token;
|
||||
}
|
||||
|
||||
async function authHeader(request: NextRequest): Promise<string | null> {
|
||||
const incoming = request.headers.get("authorization")?.trim();
|
||||
if (incoming) return incoming;
|
||||
|
||||
const token = await serviceToken();
|
||||
return token ? `Bearer ${token}` : null;
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const auth = await authHeader(request);
|
||||
if (!auth) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
message:
|
||||
"Meshkee categories need auth. Set MESHKEE_API_TOKEN or sign in on the dashboard first.",
|
||||
},
|
||||
{ status: 503 },
|
||||
);
|
||||
}
|
||||
|
||||
const q = request.nextUrl.searchParams.get("q")?.trim() ?? "";
|
||||
const params = new URLSearchParams();
|
||||
if (q) params.set("q", q);
|
||||
|
||||
const response = await fetch(
|
||||
`${MESHKEE_API_URL}/business-categories?${params}`,
|
||||
{
|
||||
headers: { Authorization: auth },
|
||||
cache: "no-store",
|
||||
},
|
||||
);
|
||||
|
||||
const payload = await response.json().catch(() => null);
|
||||
if (!response.ok) {
|
||||
return NextResponse.json(
|
||||
payload ?? { message: `Meshkee error (${response.status})` },
|
||||
{ status: response.status },
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(payload);
|
||||
}
|
||||
Reference in New Issue
Block a user