hamid1882
hamid18823mo ago

Middleware Issue on our Nextjs project

We are facing an issue on our existing middleware setup on our nextjs project, we use to check if a user is authenticated before using this code:
await convexAuth.isAuthenticated()
await convexAuth.isAuthenticated()
but now after upgrading convex auth from 0.0.74 to 0.0.80 we are getting this error: Middleware.ts file code
export default convexAuthNextjsMiddleware(
async (request, { convexAuth }) => {
const url = new URL(request.url);

if (url.pathname === "/login" && (await convexAuth.isAuthenticated())) {
const urlSearchParams = new URL(request.url).searchParams
.get("redirect")
?.replaceAll("/", "");

if (urlSearchParams) {
return NextResponse.redirect(
new URL(`/${urlSearchParams}`, request.url)
);
} else {
return NextResponse.redirect(new URL("/", request.url));
}
}

if (isProtectedRoute(request) && !(await convexAuth.isAuthenticated())) {
const url = new URL(request.url);
const pathAfterLocalhost = url.pathname.replace("application", "");

if (pathAfterLocalhost) {
return NextResponse.redirect(
new URL(`/login?redirect=${pathAfterLocalhost}`, request.url)
);
}

return nextjsMiddlewareRedirect(request, "/login");
}
},
{ cookieConfig: { maxAge: 60 * 60 * 24 * 30 } }
);
export default convexAuthNextjsMiddleware(
async (request, { convexAuth }) => {
const url = new URL(request.url);

if (url.pathname === "/login" && (await convexAuth.isAuthenticated())) {
const urlSearchParams = new URL(request.url).searchParams
.get("redirect")
?.replaceAll("/", "");

if (urlSearchParams) {
return NextResponse.redirect(
new URL(`/${urlSearchParams}`, request.url)
);
} else {
return NextResponse.redirect(new URL("/", request.url));
}
}

if (isProtectedRoute(request) && !(await convexAuth.isAuthenticated())) {
const url = new URL(request.url);
const pathAfterLocalhost = url.pathname.replace("application", "");

if (pathAfterLocalhost) {
return NextResponse.redirect(
new URL(`/login?redirect=${pathAfterLocalhost}`, request.url)
);
}

return nextjsMiddlewareRedirect(request, "/login");
}
},
{ cookieConfig: { maxAge: 60 * 60 * 24 * 30 } }
);
Error:
Server Error
Error: Server Error: could not find api.auth.isAuthenticated. convex-auth 0.0.76 introduced a new export in convex/auth.ts. Add `isAuthenticated` to the list of functions returned from convexAuth(). See convex-auth changelog for more https://github.com/get-convex/convex-auth/blob/main/CHANGELOG.md

This error happened while generating the page. Any console logs will be displayed in the terminal window.
Source
src\middleware.ts (29:38) @ async eval

> 29 | if (isProtectedRoute(request) && !(await convexAuth.isAuthenticated())) {}
Server Error
Error: Server Error: could not find api.auth.isAuthenticated. convex-auth 0.0.76 introduced a new export in convex/auth.ts. Add `isAuthenticated` to the list of functions returned from convexAuth(). See convex-auth changelog for more https://github.com/get-convex/convex-auth/blob/main/CHANGELOG.md

This error happened while generating the page. Any console logs will be displayed in the terminal window.
Source
src\middleware.ts (29:38) @ async eval

> 29 | if (isProtectedRoute(request) && !(await convexAuth.isAuthenticated())) {}
can anyone help me fix this issue.
2 Replies
Convex Bot
Convex Bot3mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
sshader
sshader3mo ago
There are too many things called convexAuth -- this is saying that your convex/auth.ts file needs to be changed to look like export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth(... Because Next.js middleware now expects to call an isAuthenticated query exported from convex/auth.ts

Did you find this page helpful?