mirror of
https://github.com/nicotsx/zerobyte.git
synced 2026-04-18 05:47:31 -04:00
30 lines
868 B
TypeScript
30 lines
868 B
TypeScript
import { createServerFn } from "@tanstack/react-start";
|
|
import { auth } from "../auth";
|
|
import { getRequest } from "@tanstack/react-start/server";
|
|
|
|
export const getOrganizationContext = createServerFn({ method: "GET" }).handler(async () => {
|
|
const request = getRequest();
|
|
|
|
const data = await auth.api.listOrganizations({
|
|
headers: request.headers,
|
|
});
|
|
const session = await auth.api.getSession({ headers: request.headers });
|
|
|
|
const activeOrganizationId = session?.session?.activeOrganizationId;
|
|
const activeOrganization = data.find((org) => org.id === activeOrganizationId);
|
|
|
|
if (data.length === 0) {
|
|
throw new Error("No organizations found for user");
|
|
}
|
|
|
|
const member = await auth.api.getActiveMember({
|
|
headers: request.headers,
|
|
});
|
|
|
|
return {
|
|
organizations: data,
|
|
activeOrganization: activeOrganization || data[0],
|
|
activeMember: member,
|
|
};
|
|
});
|