K
Kinde•7mo ago
Chetan Verma

NextJS App Router Error out in middleware

Hello, I've been working on implementing Kinde Auth within the Next.js app router using middleware. However, I encountered an error after logging in. Code -
import { withAuth } from "@kinde-oss/kinde-auth-nextjs/middleware";
import { NextApiRequest } from "next";

interface KindeRequest extends NextApiRequest {
kindeAuth: any; // Replace 'any' with the actual type of kindeAuth if known
}

export default function middleware(req: KindeRequest) {
return withAuth(req);
}
export const config = {
matcher: ["/dashboard"],
};
import { withAuth } from "@kinde-oss/kinde-auth-nextjs/middleware";
import { NextApiRequest } from "next";

interface KindeRequest extends NextApiRequest {
kindeAuth: any; // Replace 'any' with the actual type of kindeAuth if known
}

export default function middleware(req: KindeRequest) {
return withAuth(req);
}
export const config = {
matcher: ["/dashboard"],
};
Additionally, I have a backend server where I aim to authenticate requests originating from the client-side Next.js application. Do I need to register a new application on the dashboard? Also, is there a method to authenticate these requests on the server? (The server is written in Nest.js.)
No description
8 Replies
onderay
onderay•7mo ago
Thanks for providing all the details @Chetan Verma I will flick this over to our NextJS expert to help troubleshoot this. Are you using Kinde NextJS SDK v2.0.10?
Peter Hu
Peter Hu•7mo ago
I got the same error, and yes, I am using the Kinde NextJS SDK v2.0.10.
Oli - Kinde
Oli - Kinde•7mo ago
Hi @Chetan Verma, @Peter Hu and @Ruyy, We have identified an issue with the middleware in NextJS App Router v2. You should be able to use the middleware in NextJS App Router v1: https://kinde.com/docs/developer-tools/nextjs-sdkv1/#protecting-pages We will patch the middleware in NextJS App Router v2, but you should be able to continue using the middleware in NextJS App Router v1 before and after the patch to achieve what you are after. Please let me know if you have any other questions. I am here to help.
Kinde Docs
NextJS App Router SDK v1 - Developer tools - Help center
Our developer tools provide everything you need to get started with Kinde.
Peter Hu
Peter Hu•7mo ago
Thank you, Oli. I have a question unrelated to this middleware error (and still is a middleware question). What if I have a couple of public pages for the user? Do I need to modify the middleware to ignore Kinde authMiddleware and use the NextResponse.next()? Currently, it always redirects me back to the home page.
peteswah
peteswah•7mo ago
The middleware should only run when visiting a page/route included in the matcher. So if you don't want the middleware to run on a page, then you should make sure it's not covered by the matcher 🙂 With this error we are running into - when are we seeing it? the TypeError: Cannot read properties of undefined (reading value) I'm tryng to see if I can recreate the issue so I might be able to fix it
BigRIP
BigRIP•6mo ago
Currently dealing with the same issue, the error begins when a user attempts to login/SignUp, they are redirected to the kinde auth page and after signing in they are sent back to the site This is where the error begins It appears the user cookie is created and saved but when trying to access any private routes configured in middleware.ts the user is shown the error Also forgot to mention when trying to access private routes the user is returned unauthorised, even tho they are and allowed to view it, the user is then redirected to /auth-callback and then the error is shown on the page
Oli - Kinde
Oli - Kinde•6mo ago
Hey @Chetan Verma, @Peter Hu, @Ruyy and @BigRIP, There is a new version of the NextJS SDK v2.0.11 that fixed this middleware issue. Please let us know if you are still experiencing any issues.