-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
36 lines (29 loc) · 1.04 KB
/
middleware.ts
File metadata and controls
36 lines (29 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// programmer: rethabile eric siase
// github.com/rethabile2004
// middleware :)
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server';
import { NextResponse, type NextRequest } from 'next/server';
const isPublicRoute = createRouteMatcher([
'/',
'/products(.*)',
'/about',
]);// defines all the routes a user who has not logged in can access
const isAdminRoute = createRouteMatcher([
'/admin(.*)'
]); // defines the admin routes
export default clerkMiddleware(async (auth, req: NextRequest) => {
const uid = (await auth()).userId
if (isAdminRoute(req) && !(uid === process.env.ADMIN_USERID)) {
return NextResponse.redirect(new URL('/', req.url)) //redirect the user to the home page if the user
// is not an admin
}
if (!isPublicRoute(req)) {
await auth.protect(); // redirect the user to a login screen
}
});
export const config = {
matcher: [
'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
'/(api|trpc)(.*)',
],
};