-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
56 lines (49 loc) · 1.31 KB
/
middleware.ts
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { withClerkMiddleware, getAuth } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
// Set the paths that don't require the user to be signed in
const publicPaths = [
"/",
"/login*",
"/register*",
"/about",
"/terms",
"/privacy",
"/paste/public",
"/paste/**",
"/link/**",
"/new/link",
"/new/paste",
"/api/post/link",
"/api/post/paste",
"/api/available/slug",
// Static assets
"/sitemap.xml",
"/robots.txt",
"/images/**",
"/fonts/**",
"/logo.svg",
"/misc/**",
];
const isPublic = (path: string) => {
return publicPaths.find((x) =>
path.match(new RegExp(`^${x}$`.replace("*$", "($|/)")))
);
};
export default withClerkMiddleware((request: NextRequest) => {
if (isPublic(request.nextUrl.pathname)) {
return NextResponse.next();
}
// if the user is not signed in redirect them to the sign in page.
const { userId } = getAuth(request);
if (!userId) {
// redirect the users to /pages/sign-in/[[...index]].ts
const signInUrl = new URL("/login", request.url);
signInUrl.searchParams.set("redirect_url", request.url);
return NextResponse.redirect(signInUrl);
}
return NextResponse.next();
});
export const config = {
matcher: "/((?!_next/image|_next/static|favicon.ico).*)",
};