Skip to content

Server Side fetchauthsession/fetchuserattributes calls behaving spotty, either working or userunauthenticated error #14350

@Dante-Capobianco

Description

@Dante-Capobianco

Before opening, please confirm:

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

  1. Install aws amplify
  2. Configure on client side, then call it within layout.tsx in nextjs app router application
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    AuthRelated to Auth components/categoryNext.jsquestionGeneral question

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions