mirror of
https://github.com/nicotsx/zerobyte.git
synced 2026-07-31 18:16:03 -04:00
* refactor(auth): mark desktop sessions with auth source Makes it easier to filter out on session type in backend paths that behave differently depending on the context * chore: fix un-used import * fix(auth): align desktop session guards * refactor(auth): gate desktop sessions by runtime features
37 lines
1.3 KiB
TypeScript
37 lines
1.3 KiB
TypeScript
import { createServerFn } from "@tanstack/react-start";
|
|
import { getRequestHeaders } from "@tanstack/react-start/server";
|
|
import type { Permission, RuntimeFeature } from "~/lib/permission-policy";
|
|
import { resolvePermissions } from "~/server/core/request-context";
|
|
import { auth } from "~/server/lib/auth";
|
|
import { getSessionAuthSource } from "~/server/modules/auth/helpers";
|
|
|
|
export const currentPermissionsQueryKey = ["current-permissions"] as const;
|
|
|
|
export type CurrentPermissions = {
|
|
permissions: Record<Permission, boolean>;
|
|
features: Record<RuntimeFeature, boolean>;
|
|
};
|
|
|
|
export function getCurrentPermissionsOptions(
|
|
queryFn: () => Promise<CurrentPermissions> = () => getCurrentPermissions(),
|
|
) {
|
|
return { queryKey: currentPermissionsQueryKey, queryFn };
|
|
}
|
|
|
|
export const getCurrentPermissions = createServerFn({ method: "GET" }).handler(
|
|
async (): Promise<CurrentPermissions> => {
|
|
const headers = getRequestHeaders();
|
|
const [session, activeMember] = await Promise.all([
|
|
auth.api.getSession({ headers }),
|
|
auth.api.getActiveMember({ headers }),
|
|
]);
|
|
const { permissions, features } = resolvePermissions({
|
|
instanceRole: session?.user?.role,
|
|
orgRole: activeMember?.role,
|
|
authSource: session?.user ? getSessionAuthSource(session.session.authSource) : null,
|
|
});
|
|
|
|
return { permissions, features };
|
|
},
|
|
);
|