-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
23 lines (21 loc) · 791 Bytes
/
middleware.ts
File metadata and controls
23 lines (21 loc) · 791 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { authMiddleware, redirectToSignIn } from '@clerk/nextjs';
import { NextResponse } from 'next/server';
export default authMiddleware({
// Routes that can be accessed while signed out
publicRoutes: ['/', '/home'],
afterAuth(auth, req, evt) {
// Handle users who aren't authenticated
if (!auth.userId && !auth.isPublicRoute) {
return redirectToSignIn({ returnBackUrl: req.url });
}
// If the user is logged in and trying to access a protected route, allow them to access route
if (auth.userId && !auth.isPublicRoute) {
return NextResponse.next();
}
// Allow users visiting public routes to access them
return NextResponse.next();
},
});
export const config = {
matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'],
};