Skip to content

fix(aws-amplify): refresh default Cognito auth config on DefaultAmplify reconfigure #14820

Description

@ShrutiPundir17

Before opening, please confirm:

JavaScript Framework

Next.js

Amplify APIs

Authentication, Storage

Amplify Version

v6

Amplify Categories

auth, storage

Backend

Amplify Gen 2

Environment information

Details
# Put output below this line

System:
  OS: Windows 11 10.0.26200
  CPU: (8) x64 Intel(R) Core(TM) Ultra 7 256V
  Memory: 1.94 GB / 15.54 GB
Binaries:
  Node: 22.17.0
  npm: 10.9.2
Browsers:
  Edge: Chromium (140.0.3485.54)
npmPackages:
  aws-amplify: 6.x (latest on npm at time of report)

Describe the bug

When using Amplify.configure from the aws-amplify package (DefaultAmplify.configure in packages/aws-amplify/src/initSingleton.ts), subsequent configure calls can behave incorrectly in two ways:

  1. Non-Auth libraryOptions are dropped on reconfigure
    @aws-amplify/core replaces libraryOptions when a new object is passed (no merge). DefaultAmplify is responsible for merging before calling core, but several reconfigure paths did not:

Passing libraryOptions.Auth alone replaced the whole options object and dropped keys such as Storage from an earlier configure.
Passing a partial object (e.g. only { ssr: true }) after Auth was already set up built { Auth: authLibraryOptions, ...libraryOptions } without ...Amplify.libraryOptions, so prior Storage (and other) options were omitted.

  1. Default Cognito token provider keeps stale Auth config
    cognitoUserPoolsTokenProvider.setAuthConfig() was only called on the first Auth setup path. Later Amplify.configure calls that changed resourcesConfig.Auth (new user pool / client id, config-only reconfigure, or libraryOptions.Auth with the default provider) did not refresh the token provider.

TokenStore builds storage keys from authConfig.Cognito.userPoolClientId, so tokens can be read/written under old keys after the app reconfigures to a new pool.

Expected behavior

  1. Each Amplify.configure from aws-amplify should merge new libraryOptions with existing Amplify.libraryOptions so category options (e.g. Storage) remain unless explicitly overridden.
  2. When the default cognitoUserPoolsTokenProvider is in use, setAuthConfig should run whenever resourcesConfig.Auth changes on reconfigure, so token storage keys match the current Cognito client id.

Reproduction steps

Scenario - partial reconfigure drops Storage

-Install aws-amplify v6 and configure with Auth + Storage options: Amplify.configure(configA, { Storage: { S3: { defaultAccessLevel: 'private' } } }).
-Call Amplify.configure(configB, { ssr: true }) (or only { ssr: false }) with Auth still present in the resource config.
-Inspect Amplify.libraryOptions (or observe Storage using wrong/missing options).
-Actual: Storage is missing from singleton libraryOptions. Expected: Storage is preserved and ssr is applied.

Code Snippet

// Put your code below this line.
import { Amplify } from 'aws-amplify';

const poolA = {
  Auth: {
    Cognito: {
      userPoolId: 'us-east-1_poolA',
      userPoolClientId: 'client-a',
    },
  },
};

const poolB = {
  Auth: {
    Cognito: {
      userPoolId: 'us-east-1_poolB',
      userPoolClientId: 'client-b',
    },
  },
};

// 1) First configure with Storage
Amplify.configure(poolA, {
  Storage: {
    S3: { defaultAccessLevel: 'private' },
  },
});

// 2) Reconfigure — partial options (Scenario A)
Amplify.configure(poolB, { ssr: true });
// Bug: Amplify.libraryOptions.Storage may be undefined

// 3) Reconfigure — Auth options only (Scenario B)
Amplify.configure(poolB, {
  Auth: {
    // default cognitoUserPoolsTokenProvider from first configure
  },
});
// Bug: Storage dropped; setAuthConfig may not run for pool B (Scenario C)

Log output

Details
// Put your logs below this line
No runtime error or stack trace — incorrect singleton state (libraryOptions / token store keys).
Enable debug if needed: window.LOG_LEVEL = 'DEBUG';

aws-exports.js

No response

Manual configuration

import { Amplify } from 'aws-amplify';

const config = {
Auth: {
Cognito: {
userPoolId: 'us-east-1_XXXXXXX',
userPoolClientId: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
},
},
};

Amplify.configure(config, {
Storage: {
S3: {
bucket: 'my-bucket',
region: 'us-east-1',
},
},
});

// Later reconfigure (hot reload, env switch, SSR toggle)
Amplify.configure(updatedConfig, { ssr: true });

Additional configuration

{
"note": "Reproducible in Node/SSR (Next.js) and browser when configure runs more than once",
"affectedFile": "packages/aws-amplify/src/initSingleton.ts",
"coreBehavior": "@aws-amplify/core Amplify.configure replaces libraryOptions when a new object is passed"
}

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

--Pull request (fix + tests + changeset): #14819
--Related issues: #14816, #13707
--Related PR: #14815 (partial libraryOptions merge only)
--Labels suggestion: bug, Auth, Storage
--Screenshots: N/A (library/singleton behavior)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions