[bugfix] allow local clerk to work
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { clerkMiddleware, createRouteMatcher, clerkClient } from '@clerk/astro/server';
|
||||
import type { MiddlewareNext } from 'astro';
|
||||
import 'dotenv/config';
|
||||
|
||||
declare global {
|
||||
@@ -14,6 +13,10 @@ const isProtectedRoute = createRouteMatcher(['/pokemon']);
|
||||
const isAdminRoute = createRouteMatcher(['/admin']);
|
||||
|
||||
const TARGET_ORG_ID = "org_3Baav9czkRLLlC7g89oJWqRRulK";
|
||||
const ADMIN_ORG_IDS = new Set([
|
||||
"org_3Baav9czkRLLlC7g89oJWqRRulK",
|
||||
"org_3ABdwuK3qD7Saq590ZMQWY7AvVz",
|
||||
]);
|
||||
|
||||
export const onRequest = clerkMiddleware(async (auth, context, next) => {
|
||||
const { isAuthenticated, userId, redirectToSignIn, has } = auth();
|
||||
@@ -45,15 +48,26 @@ export const onRequest = clerkMiddleware(async (auth, context, next) => {
|
||||
|
||||
try {
|
||||
const client = await clerkClient(context);
|
||||
const memberships = await client.organizations.getOrganizationMembershipList({
|
||||
organizationId: TARGET_ORG_ID,
|
||||
});
|
||||
const userOrgIds = await getUserOrgIds(context, userId);
|
||||
const matchingOrgIds = userOrgIds.filter((id) => ADMIN_ORG_IDS.has(id));
|
||||
|
||||
const userMembership = memberships.data.find(
|
||||
(m) => m.publicUserData?.userId === userId
|
||||
if (matchingOrgIds.length === 0) {
|
||||
return new Response(null, { status: 404 });
|
||||
}
|
||||
|
||||
const membershipLists = await Promise.all(
|
||||
matchingOrgIds.map((orgId) =>
|
||||
client.organizations.getOrganizationMembershipList({ organizationId: orgId })
|
||||
)
|
||||
);
|
||||
|
||||
if (!userMembership || userMembership.role !== "org:admin") {
|
||||
const isAdmin = membershipLists.some((list) =>
|
||||
list.data.some(
|
||||
(m) => m.publicUserData?.userId === userId && m.role === "org:admin"
|
||||
)
|
||||
);
|
||||
|
||||
if (!isAdmin) {
|
||||
return new Response(null, { status: 404 });
|
||||
}
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user