Skip to content

Next.js Amplify v6 Server side Authorization header issue #12971

@asp3

Description

@asp3

Before opening, please confirm:

JavaScript Framework

Next.js

Amplify APIs

GraphQL API

Amplify Version

v6

Amplify Categories

auth, api

Backend

Amplify CLI

Environment information

  System:
    OS: macOS 14.0
    CPU: (12) arm64 Apple M2 Max
    Memory: 64.38 MB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.6.1 - ~/.nvm/versions/node/v20.6.1/bin/node
    Yarn: 1.22.5 - ~/.yarn/bin/yarn
    npm: 9.8.1 - ~/.nvm/versions/node/v20.6.1/bin/npm
    Watchman: 2023.10.23.00 - /usr/local/bin/watchman
  Browsers:
    Brave Browser: 114.1.52.130
    Chrome: 121.0.6167.139
    Safari: 17.0
  npmPackages:
    @knowt/eslint-config: * => 0.0.0 
    dotenv-cli: latest => 7.3.0 
    husky: ^8.0.0 => 8.0.3 
    lint-staged: ^12.4.0 => 12.5.0 
    prettier: ^2.7.1 => 2.8.8 
    turbo: ^1.10.12 => 1.10.16 
  npmGlobalPackages:
    amplify: 0.0.11
    appcenter-cli: 2.14.0
    corepack: 0.19.0
    eas-cli: 5.4.0
    eslint: 8.56.0
    expo-cli: 6.3.10
    npm-check: 6.0.1
    npm: 9.8.1

Describe the bug

On client side, I am able to make all calls as expected, by passing an id token in the header.

Amplify.configure(awsConfig, {
    ssr: true,
    API: {
        GraphQL: {
            headers: async () => ({
                Authorization: (await fetchAuthSession()).tokens?.idToken?.toString(),
            }),
        },
    },
});
export const serverClient = generateServerClientUsingCookies({
    config: awsConfig,
    cookies,
});

however, on server side, we are not allowed to override the headers from this function, so there is no way to use the id token!

I saw a few threads (#12699) but it seems unnecessarily complex where we have to wrap every call in runWithAmplifyServerContextCore, rather than just use the idToken already readily available in the cookies. Wondering if there was any fix for that!

Expected behavior

Expected server side calls are also made with the proper ID token claims. Currently, it seems to be making it with the access token, with no way to override.

Reproduction steps

  1. setup next.js app with client and server side, using id token claims override
  2. observe that client calls work properly, but server calls do not.

Code Snippet

No response

Log output

No response

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

Labels

AuthRelated to Auth components/categoryGraphQLRelated to GraphQL API issuesSSRIssues related to Server Side Renderingfeature-requestRequest a new feature

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions