middleware origin and auth bugged / not working
Unanswered
Tonkinese posted this in #help-forum
TonkineseOP
import { NextRequestWithAuth, withAuth } from "next-auth/middleware";
import { NextResponse } from "next/server";
Â
// export { default } from "next-auth/middleware";
Â
const allowedOrigins =
 process.env.NODE_ENV === "production"
   ? ["https://www.yoursite.com", "https://yoursite.com"]
   : ["http://localhost:3000"];
Â
export function middleware(request: Request) {
 const origin = request.headers.get("origin");
 if (origin && !allowedOrigins.includes(origin)) {
   return new NextResponse(null, {
     status: 400,
     statusText: "Bad Request",
     headers: {
       "Content-Type": "text/plain",
     },
   });
 }
}
Â
export default withAuth(
 function middleware(request: NextRequestWithAuth) {
   console.log(request.nextUrl.pathname);
   console.log(request.nextauth.token);
Â
   // if (request.nextUrl.pathname.endsWith("/signin")) {
   //  console.log("Fired!");
   //  return NextResponse.redirect("/");
   // }
 },
 {
   callbacks: {
     authorized: ({ token }) => !!token,
   },
 }
);
Â
export const config = { matcher: ["/profile", "/api/:path*"] };10 Replies
European sprat
try formatting your code so it's readable to people who want to help
look at code blocks (three backticks)
TonkineseOP
My bad I’m on iOS
Did that fix it
European sprat
three back ticks at the beginning and end
`
`
TonkineseOP
That’s what I did hmmm
Alrighty fixed
@European sprat three back ticks at the beginning and end
`
TonkineseOP
Any solution u got to my bug ?
@Tonkinese Any solution u got to my bug ?
You are using
withAuth and default middleware at the same time. They can block each other. Take a look at the following thread for a possible solution: #`withAuth` only runs if the user is