You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue only occurs if middleware.ts in the src directory is calling runWithAmplifyServerContext() in an async context. In the middleware function, as long as runWithAmplifyServerContext() is not executed, this error will not happen.
Expected behavior
NextJS app compiles frontend without displaying error
Reproduction steps
Git clone this repository: https://github.com/alexrusin/nextjs-cognito-auth (used this repository as a reference while implementing cognito authentication and authorization, same issue will occur. Same issue happens on the repository I'm working on)
Update NextJS to latest version 15.1.7: npm i next@latest
Update aws-amplify and @aws-amplify/adapter-nextjs to latest (optional)
Run npm run dev
Code Snippet
In middleware.ts:
import{typeNextRequest,NextResponse}from"next/server";import{authenticatedUser}from"./utils/amplify-server-utils";exportasyncfunctionmiddleware(request: NextRequest){constresponse=NextResponse.next();// Commenting out this line and all references to "user" makes the error go awayconstuser=awaitauthenticatedUser({ request, response });constisOnDashboard=request.nextUrl.pathname.startsWith("/dashboard");constisOnAdminArea=request.nextUrl.pathname.startsWith("/dashboard/admins");if(isOnDashboard){if(!user)returnNextResponse.redirect(newURL("/auth/login",request.nextUrl));if(isOnAdminArea&&!user.isAdmin)returnNextResponse.redirect(newURL("/dashboard",request.nextUrl));returnresponse;}elseif(user){returnNextResponse.redirect(newURL("/dashboard",request.nextUrl));}}exportconstconfig={/* * Match all request paths except for the ones starting with */matcher: ["/((?!api|_next/static|_next/image|.*\\.png$).*)"],};
In amplify-server-utils
import { authConfig } from "@/app/amplify-cognito-config";
import { NextServer, createServerRunner } from "@aws-amplify/adapter-nextjs";
import { fetchAuthSession, getCurrentUser } from "aws-amplify/auth/server";
export const { runWithAmplifyServerContext } = createServerRunner({
config: {
Auth: authConfig,
},
});
export async function authenticatedUser(context: NextServer.Context) {
// runWithAmplifyServerContext seems to be the culprit
return await runWithAmplifyServerContext({
nextServerContext: context,
operation: async (contextSpec) => {
try {
const session = await fetchAuthSession(contextSpec);
if (!session.tokens) {
return;
}
const user = {
...(await getCurrentUser(contextSpec)),
isAdmin: false,
};
const groups = session.tokens.accessToken.payload["cognito:groups"];
// @ts-ignore
user.isAdmin = Boolean(groups && groups.includes("Admins"));
return user;
} catch (error) {
console.log(error);
}
},
});
}
Hi @CarlWangMK thanks for reporting this issue. Looking at the screenshot shown error log and stack trace, it look like an internal function for parsing the Amplify configuration cannot terminate the recursive process.
Could you provide the authConfig object structure imported from @/app/amplify-cognito-config?
Before opening, please confirm:
JavaScript Framework
Next.js
Amplify APIs
Authentication
Amplify Version
v6
Amplify Categories
auth
Backend
None
Environment information
Describe the bug
This issue only occurs if
middleware.ts
in thesrc
directory is callingrunWithAmplifyServerContext()
in an async context. In the middleware function, as long asrunWithAmplifyServerContext()
is not executed, this error will not happen.Expected behavior
NextJS app compiles frontend without displaying error
Reproduction steps
npm i next@latest
aws-amplify
and@aws-amplify/adapter-nextjs
to latest (optional)npm run dev
Code Snippet
In
middleware.ts
:In
amplify-server-utils
Log output
aws-exports.js
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
I found that downgrading NextJS version 15 makes this issue goes away, to something like version 14, but I would prefer to not do this.
The text was updated successfully, but these errors were encountered: