Hello from MCP server
// import router from "@/router";
import { useRouter } from "vue-router";
import PocketBase from "pocketbase";
import { TypedPocketBase, UsersRecord } from "@/pocketbase-types";
const DEFAULT_API_HOST = import.meta.env.VITE_API_HOST;
const PROD_API_HOST = "https://pricebookplatform.com";
// Allow runtime override of API host (for secret mode on localhost)
let apiHostOverride: string | null = null;
let pb: TypedPocketBase | null = null;
export function setApiHostOverride(host: string | null) {
// Only recreate PocketBase if the host actually changed
const newHost = host || DEFAULT_API_HOST;
const currentHost = apiHostOverride || DEFAULT_API_HOST;
if (newHost !== currentHost) {
apiHostOverride = host;
pb = null; // Force recreation with new host
}
}
export function getApiHostOverride(): string | null {
return apiHostOverride;
}
export function getCurrentApiHost(): string {
return apiHostOverride || DEFAULT_API_HOST;
}
export function getProdApiHost(): string {
return PROD_API_HOST;
}
export async function getApi(authorize: boolean = true) {
const currentHost = apiHostOverride || DEFAULT_API_HOST;
if (!pb) {
pb = new PocketBase(currentHost) as TypedPocketBase;
}
if (authorize) {
if (pb.authStore.isValid) {
try {
await pb
.collection("users")
.authRefresh({ expand: "activeOrg, profiles" });
} catch {
window.localStorage.clear();
}
} else {
const router = useRouter();
if (router) {
router.push("/auth/login");
}
}
}
return { pb };
}
//TODO: something like this might be better
// export function getApiInstance() {
// if (!pb) pb = new PocketBase(API_HOST) as TypedPocketBase;
// return pb;
// }
//
// export async function ensureAuth() {
// const pb = getApiInstance();
// if (pb.authStore.isValid) {
// await pb.collection("users").authRefresh({ expand: "activeOrg" });
// } else {
// console.log("token expired");
// router.push("/login");
// }
// }