added /admin for admin panel - limited to users in the admin role (also updated local .env to match prod keys for clerk)
This commit is contained in:
@@ -1,17 +1,45 @@
|
|||||||
// src/middleware.ts
|
import { clerkMiddleware, createRouteMatcher, clerkClient } from '@clerk/astro/server';
|
||||||
import { clerkMiddleware, createRouteMatcher } from '@clerk/astro/server';
|
|
||||||
import type { AstroMiddlewareRequest, AstroMiddlewareResponse } from 'astro';
|
|
||||||
|
|
||||||
const isProtectedRoute = createRouteMatcher([
|
const isProtectedRoute = createRouteMatcher(['/pokemon']);
|
||||||
'/pokemon',
|
const isAdminRoute = createRouteMatcher(['/admin']);
|
||||||
]);
|
|
||||||
|
|
||||||
export const onRequest = clerkMiddleware((auth, context) => {
|
const TARGET_ORG_ID = "org_3Baav9czkRLLlC7g89oJWqRRulK";
|
||||||
const { isAuthenticated, redirectToSignIn } = auth()
|
|
||||||
|
export const onRequest = clerkMiddleware(async (auth, context) => {
|
||||||
|
const { isAuthenticated, userId, redirectToSignIn } = auth();
|
||||||
|
|
||||||
if (!isAuthenticated && isProtectedRoute(context.request)) {
|
if (!isAuthenticated && isProtectedRoute(context.request)) {
|
||||||
// Add custom logic to run before redirecting
|
return redirectToSignIn();
|
||||||
|
|
||||||
return redirectToSignIn()
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
if (isAdminRoute(context.request)) {
|
||||||
|
if (!isAuthenticated || !userId) {
|
||||||
|
return redirectToSignIn();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const client = await clerkClient(context); // pass context here
|
||||||
|
const memberships = await client.organizations.getOrganizationMembershipList({
|
||||||
|
organizationId: TARGET_ORG_ID,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log("Total memberships found:", memberships.data.length);
|
||||||
|
console.log("Current userId:", userId);
|
||||||
|
console.log("Memberships:", JSON.stringify(memberships.data.map(m => ({
|
||||||
|
userId: m.publicUserData?.userId,
|
||||||
|
role: m.role,
|
||||||
|
})), null, 2));
|
||||||
|
|
||||||
|
const userMembership = memberships.data.find(
|
||||||
|
(m) => m.publicUserData?.userId === userId
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!userMembership || userMembership.role !== "org:admin") {
|
||||||
|
return context.redirect("/");
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Clerk membership check failed:", e);
|
||||||
|
return context.redirect("/");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
18
src/pages/admin.astro
Normal file
18
src/pages/admin.astro
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
---
|
||||||
|
export const prerender = false;
|
||||||
|
import Layout from '../layouts/Main.astro';
|
||||||
|
import NavItems from '../components/NavItems.astro';
|
||||||
|
import NavBar from '../components/NavBar.astro';
|
||||||
|
import Footer from '../components/Footer.astro';
|
||||||
|
---
|
||||||
|
<Layout title="Admin Panel">
|
||||||
|
<NavBar slot="navbar">
|
||||||
|
<NavItems slot="navItems" />
|
||||||
|
</NavBar>
|
||||||
|
<div class="row mb-4" slot="page">
|
||||||
|
<div class="col-12">
|
||||||
|
<h1>Admin Panel</h1>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<Footer slot="footer" />
|
||||||
|
</Layout>
|
||||||
Reference in New Issue
Block a user