-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
Before opening, please confirm:
- I have searched for duplicate or closed issues and discussions.
- I have read the guide for submitting bug reports.
- I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
JavaScript Framework
Next.js
Amplify APIs
Authentication
Amplify Version
v6
Amplify Categories
Not applicable
Backend
Amplify Gen 2
Environment information
# Put output below this line
Describe the bug
Hello! I am getting errors implementing a check in my middleware for a currently signed in user. It is working on the client side flawlessly (my client side implement of configure.amplify is done similar to the docs + ssr is set to true), but the server side is not picking up the signed in user. When I call sign in on client side, cognitioidentityserviceprovider cookies are being set, but when calling the below, get an error: UserUnAuthenticatedException: User needs to be authenticated to call this API.
export const { runWithAmplifyServerContext } = createServerRunner({
config: {
Auth: {
Cognito: {
userPoolId: process.env.NEXT_PUBLIC_COGNITO_USER_POOL_ID!,
userPoolClientId: process.env.NEXT_PUBLIC_COGNITO_USER_POOL_CLIENT_ID!,
identityPoolId: process.env.NEXT_PUBLIC_COGNITO_IDENTITY_POOL_ID!,
},
}
},
});
export async function AuthGetCurrentUserServer(req: NextRequest, res: NextResponse) {
try {
const currentUser = await runWithAmplifyServerContext({
nextServerContext: { request: req, response: res },
operation: (contextSpec) => fetchUserAttributes(contextSpec)
});
return currentUser;
} catch (error) {
console.log(error);
return null;
}
}
then in middleware:
export async function middleware(req: NextRequest) {
const response = NextResponse.next();
const userData = await AuthGetCurrentUserServer(req, response);
I'm pretty certain this is exactly how it's done in docs - let me know!
Expected behavior
I expected it to provide back the userattributes or fetchauthsession object
Reproduction steps
- Install aws amplify
- Configure on client side, then call it within layout.tsx in nextjs app router application
- implement the code provided above
Code Snippet
// Put your code below this line.
Log output
// Put your logs below this line
aws-exports.js
No response
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
No response