// My dashboard route
export const Route = createFileRoute("/_admin/admin/_layout/dashboard")({
loader: async ({ context: { queryClient } }) => {
return queryClient.ensureQueryData(adminOverviewOptions());
},
component: DashboardPage,
});
// The query option with the server function
export const adminOverviewOptions = () => queryOptions({
queryKey: adminQueryKeys.adminOverviewKey(),
queryFn: () => getAdminOverview(),
});
// The server function with the middlewares
export const getAdminOverview = createServerFn({ method: "GET" })
.middleware([managerAuthMiddleware, adminAuthMiddleware])
.handler(async () => {
const userService = await getContainer().then((c) => c.services.user);
return userService.getAdminOverview();
});
// The admin middleware which check the token, if bad token should redirect to "/admin"
export const adminAuthMiddleware = createMiddleware({ type: "function" }).server(async ({ next }) => {
const adminToken = getCookie(ADMIN_COOKIE_NAME);
if (!adminToken || !verifyAdminToken(adminToken)) {
throw redirect({ to: "/admin" });
}
return next();
});
// My dashboard route
export const Route = createFileRoute("/_admin/admin/_layout/dashboard")({
loader: async ({ context: { queryClient } }) => {
return queryClient.ensureQueryData(adminOverviewOptions());
},
component: DashboardPage,
});
// The query option with the server function
export const adminOverviewOptions = () => queryOptions({
queryKey: adminQueryKeys.adminOverviewKey(),
queryFn: () => getAdminOverview(),
});
// The server function with the middlewares
export const getAdminOverview = createServerFn({ method: "GET" })
.middleware([managerAuthMiddleware, adminAuthMiddleware])
.handler(async () => {
const userService = await getContainer().then((c) => c.services.user);
return userService.getAdminOverview();
});
// The admin middleware which check the token, if bad token should redirect to "/admin"
export const adminAuthMiddleware = createMiddleware({ type: "function" }).server(async ({ next }) => {
const adminToken = getCookie(ADMIN_COOKIE_NAME);
if (!adminToken || !verifyAdminToken(adminToken)) {
throw redirect({ to: "/admin" });
}
return next();
});