-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
27 lines (23 loc) · 791 Bytes
/
Copy pathmiddleware.ts
File metadata and controls
27 lines (23 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
24
25
26
27
import { authMiddleware, redirectToSignIn } from "@clerk/nextjs";
import { NextResponse } from "next/server";
export default authMiddleware({
afterAuth(auth, req, evt) {
if (!auth.userId && !auth.isPublicRoute) {
return redirectToSignIn({ returnBackUrl: req.url });
}
if (
auth.userId &&
req.nextUrl.pathname !== "/dashboard" &&
(req.nextUrl.pathname === "/auth/login" ||
req.nextUrl.pathname === "/auth/register" ||
req.nextUrl.pathname === "/auth/forgot-password")
) {
return NextResponse.redirect(new URL("/dashboard", req.url));
}
return NextResponse.next();
},
publicRoutes: ["/", "/auth/forgot-password"],
});
export const config = {
matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};