Next.js Discord

Discord Forum

Upstash ratelimit & next auth in middleware

Unanswered
Collin posted this in #help-forum
Open in Discord
Hi i'm trying to get upstash ratelimit and nextauth in 1 middleware.ts file but cant get it to work, can someone help me?

9 Replies

I'm trying to combine:
import { NextFetchEvent, NextRequest, NextResponse } from "next/server";
 
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
 
const redis = new Redis({
    url: "",
    token: ""
  });
 
const ratelimit = new Ratelimit({
  redis: redis,
  limiter: Ratelimit.slidingWindow(5, "10 s"),
});
 
export default async function middleware(
  request: NextRequest,
  event: NextFetchEvent,
): Promise<Response | undefined> {
  const ip = request.ip ?? "127.0.0.1";
  const { success, pending, limit, reset, remaining } =
    await ratelimit.limit(ip);
  return success
    ? NextResponse.next()
    : NextResponse.redirect(new URL("/blocked", request.url));
}
 
export const config = {
  matcher: "/",
};

With
export { default } from "next-auth/middleware";
export const config = { matcher: ["/dashboard/:path*"] };
American Chinchilla
What error do you get
@American Chinchilla
American Chinchilla
Maybe because you cant have two default
When you hover over
The red error lines
What message does it say?
That you cant have 2 defaults but i dont know how to merge them