Changed middleware
This commit is contained in:
parent
bb86095313
commit
863392994a
1 changed files with 19 additions and 60 deletions
|
@ -15,74 +15,33 @@ const rateLimit = new Ratelimit({
|
||||||
});
|
});
|
||||||
|
|
||||||
export default authMiddleware({
|
export default authMiddleware({
|
||||||
|
ignoredRoutes: ["/"],
|
||||||
publicRoutes: [
|
publicRoutes: [
|
||||||
"/",
|
|
||||||
"/api/external/public/(.*)",
|
"/api/external/public/(.*)",
|
||||||
"/api/webhooks",
|
"/api/webhooks",
|
||||||
"/api/webhooks/(.*)",
|
"/api/webhooks/(.*)",
|
||||||
],
|
],
|
||||||
afterAuth: async (auth, req) => {
|
apiRoutes: ["/api/external/private/(.*)", "/api/internal/(.*)"],
|
||||||
if (!auth.userId && auth.isPublicRoute) {
|
beforeAuth: async (req) => {
|
||||||
const { success } = await rateLimit.limit(req.ip || "");
|
const { success } = await rateLimit.limit(req.ip || "");
|
||||||
if (success) {
|
if (success) {
|
||||||
return NextResponse.next();
|
if (req.nextUrl.pathname.includes("/api/external/private")) {
|
||||||
|
const isValid = await validateRequest(req);
|
||||||
|
|
||||||
|
if (!isValid) {
|
||||||
|
return new NextResponse("UNAUTHORIZED", {
|
||||||
|
status: 403,
|
||||||
|
statusText: "Unauthorized!",
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return new NextResponse("TOO MANY REQUESTS", {
|
return NextResponse.next();
|
||||||
status: 429,
|
|
||||||
statusText: "Too many requests!",
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.nextUrl.pathname.includes("/api/internal")) {
|
return new NextResponse("TOO MANY REQUESTS", {
|
||||||
const { success } = await rateLimit.limit(req.ip || "");
|
status: 429,
|
||||||
|
statusText: "Too many requests!",
|
||||||
if (!success) {
|
});
|
||||||
return new NextResponse("TOO MANY REQUESTS", {
|
|
||||||
status: 429,
|
|
||||||
statusText: "Too many requests!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (auth.userId) {
|
|
||||||
return NextResponse.next();
|
|
||||||
} else {
|
|
||||||
return new NextResponse("UNAUTHORIZED", {
|
|
||||||
status: 403,
|
|
||||||
statusText: "Unauthorized!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (req.nextUrl.pathname.includes("/api/external/private")) {
|
|
||||||
const { success } = await rateLimit.limit(req.ip || "");
|
|
||||||
|
|
||||||
if (!success) {
|
|
||||||
return new NextResponse("TOO MANY REQUESTS", {
|
|
||||||
status: 429,
|
|
||||||
statusText: "Too many requests!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const isValid = await validateRequest(req);
|
|
||||||
|
|
||||||
if (isValid) {
|
|
||||||
return NextResponse.next();
|
|
||||||
} else {
|
|
||||||
return new NextResponse("UNAUTHORIZED", {
|
|
||||||
status: 403,
|
|
||||||
statusText: "Unauthorized!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!auth.userId && !auth.isPublicRoute) {
|
|
||||||
if (req.nextUrl.pathname.includes("/api")) {
|
|
||||||
return NextResponse.next();
|
|
||||||
}
|
|
||||||
// This is annoying...
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-explicit-any
|
|
||||||
return redirectToSignIn({ returnBackUrl: req.url });
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue