-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
64 lines (55 loc) · 1.9 KB
/
Copy pathproxy.ts
File metadata and controls
64 lines (55 loc) · 1.9 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { clerkMiddleware } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
import {
acceptsJsonOverHtml,
isPublicPath,
sanitizeRedirectUrl,
} from "@/lib/auth-routing";
const DEVELOPMENT_CLOCK_SKEW_IN_MS = 60_000;
const proxy = clerkMiddleware(
async (auth, request) => {
// Public landing, auth, and shared-map routes bypass Clerk protection.
if (isPublicPath(request.nextUrl.pathname)) {
return;
}
const { userId } = await auth();
if (userId) {
return;
}
if (
request.nextUrl.pathname.startsWith("/api") ||
acceptsJsonOverHtml(request.headers.get("accept"))
) {
return NextResponse.json({ error: "Unauthenticated" }, { status: 401 });
}
const destination = `${request.nextUrl.pathname}${request.nextUrl.search}`;
const unauthenticatedUrl = new URL("/sign-in", request.url);
unauthenticatedUrl.searchParams.set(
"redirect_url",
sanitizeRedirectUrl(destination)
);
return NextResponse.redirect(unauthenticatedUrl);
},
{
// This workstation's clock is currently about 49 seconds slow. Keep the
// temporary tolerance local-only; production retains Clerk's strict default.
clockSkewInMs:
process.env.NODE_ENV === "development"
? DEVELOPMENT_CLOCK_SKEW_IN_MS
: undefined,
}
);
// Next parses `config` statically at compile time, so it must be exported inline —
// a re-export from a trailing `export { ... }` statement fails the build.
export const config = {
matcher: [
/*
* Match all request paths except:
* - Next.js internals
* - common static files, unless they are explicitly requested via search params
*/
"/((?!_next|robots\\.txt|sitemap\\.xml|favicon\\.ico|opengraph-image.*|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)",
"/(api|trpc)(.*)",
],
};
export { proxy };