amplify-js: signWithRedirect() cannot complete with production build of a Vite React app

Before opening, please confirm:

JavaScript Framework

React

Amplify APIs

Authentication

Amplify Version

v6

Amplify Categories

auth

Backend

Other

Environment information

# Put output below this line

  System:
    OS: macOS 15.0
    CPU: (10) arm64 Apple M1 Pro
    Memory: 62.72 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.15.0 - ~/.nvm/versions/node/v20.15.0/bin/node
    npm: 10.7.0 - ~/.nvm/versions/node/v20.15.0/bin/npm
  Browsers:
    Chrome: 127.0.6533.99
    Safari: 18.0
  npmPackages:
    @bugsnag/js: ^7.22.4 => 7.25.0
    @bugsnag/plugin-react: ^7.19.0 => 7.25.0
    @bugsnag/source-maps: ^2.3.3 => 2.3.3
    @datadog/browser-rum: ^5.14.0 => 5.23.3
    @heroicons/react: ^2.0.18 => 2.1.5
    @react-spring/web: ^9.7.3 => 9.7.4
    @testing-library/jest-dom: ^6.4.8 => 6.4.8
    @testing-library/react: ^14.2.2 => 14.3.1
    @testing-library/user-event: ^14.4.3 => 14.5.2
    @types/canvas-confetti: ^1.6.0 => 1.6.4
    @types/d3: ^7.4.3 => 7.4.3
    @types/d3-array: ^3.2.1 => 3.2.1 (3.0.3)
    @types/jest: ^29.5.6 => 29.5.12
    @types/lodash: ^4.17.0 => 4.17.7
    @types/mixpanel-browser: ^2.48.1 => 2.49.1
    @types/react: ^18.2.73 => 18.3.3
    @types/react-beforeunload: ^2.1.1 => 2.1.5
    @types/react-color: ^3.0.12 => 3.0.12
    @types/react-csv: ^1.1.8 => 1.1.10
    @types/react-datepicker: ^4.15.0 => 4.19.6
    @types/react-dom: ^18.2.18 => 18.3.0
    @types/react-gtm-module: ^2.0.1 => 2.0.3
    @types/react-helmet: ^6.1.6 => 6.1.11
    @types/react-toggle: ^4.0.3 => 4.0.5
    @types/sanitize-html: ^2.11.0 => 2.11.0
    @types/string-similarity: ^4.0.2 => 4.0.2
    @types/styled-components: ^5.1.26 => 5.1.34
    @types/uuid: ^9.0.2 => 9.0.8
    @typescript-eslint/eslint-plugin: ^8.0.0 => 8.0.0
    @typescript-eslint/parser: ^8.0.0 => 8.0.0
    @visx/axis: ^3.10.1 => 3.10.1
    @visx/event: ^3.3.0 => 3.3.0
    @visx/gradient: ^3.3.0 => 3.3.0
    @visx/grid: ^3.3.0 => 3.5.0
    @visx/group: ^3.3.0 => 3.3.0
    @visx/legend: ^3.3.0 => 3.10.3
    @visx/responsive: ^3.10.2 => 3.10.2
    @visx/scale: ^3.5.0 => 3.5.0
    @visx/shape: ^3.5.0 => 3.5.0
    @visx/text: ^3.3.0 => 3.3.0
    @visx/tooltip: ^3.3.0 => 3.3.0
    @visx/voronoi: ^3.3.0 => 3.3.0
    @vitejs/plugin-basic-ssl: ^1.1.0 => 1.1.0
    @vitejs/plugin-react-swc: ^3.6.0 => 3.7.0
    aws-amplify: ^6.3.8 => 6.4.3
    aws-amplify/adapter-core:  undefined ()
    aws-amplify/analytics:  undefined ()
    aws-amplify/analytics/kinesis:  undefined ()
    aws-amplify/analytics/kinesis-firehose:  undefined ()
    aws-amplify/analytics/personalize:  undefined ()
    aws-amplify/analytics/pinpoint:  undefined ()
    aws-amplify/api:  undefined ()
    aws-amplify/api/server:  undefined ()
    aws-amplify/auth:  undefined ()
    aws-amplify/auth/cognito:  undefined ()
    aws-amplify/auth/cognito/server:  undefined ()
    aws-amplify/auth/enable-oauth-listener:  undefined ()
    aws-amplify/auth/server:  undefined ()
    aws-amplify/data:  undefined ()
    aws-amplify/data/server:  undefined ()
    aws-amplify/datastore:  undefined ()
    aws-amplify/in-app-messaging:  undefined ()
    aws-amplify/in-app-messaging/pinpoint:  undefined ()
    aws-amplify/push-notifications:  undefined ()
    aws-amplify/push-notifications/pinpoint:  undefined ()
    aws-amplify/storage:  undefined ()
    aws-amplify/storage/s3:  undefined ()
    aws-amplify/storage/s3/server:  undefined ()
    aws-amplify/storage/server:  undefined ()
    aws-amplify/utils:  undefined ()
    axios: 1 => 1.7.3
    babel-plugin-macros: ^3.1.0 => 3.1.0
    bin:  1.0.0
    canvas-confetti: ^1.6.0 => 1.9.3
    core-js-pure: ^3.35.1 => 3.38.0
    d3: ^7.8.5 => 7.9.0
    d3-array: ^3.2.4 => 3.2.4 (3.2.1)
    dist:  1.0.0
    eslint: ^8.57.0 => 8.57.0
    eslint-config-prettier: ^9.1.0 => 9.1.0
    eslint-plugin-import: ^2.29.1 => 2.29.1
    eslint-plugin-jsx-a11y: ^6.8.0 => 6.9.0
    eslint-plugin-prettier: ^5.2.1 => 5.2.1
    eslint-plugin-react: ^7.34.1 => 7.35.0
    eslint-plugin-react-hooks: ^4.6.0 => 4.6.2
    eslint-plugin-sonarjs: ^0.25.0 => 0.25.1
    fastest-levenshtein: ^1.0.16 => 1.0.16
    genversion: ^3.1.1 => 3.2.0
    gsap: ^3.12.2 => 3.12.5
    jest: ^29.7.0 => 29.7.0
    jest-environment-jsdom: ^29.7.0 => 29.7.0
    jest-sonar-reporter: ^2.0.0 => 2.0.0
    lodash: ^4.17.21 => 4.17.21
    mixpanel-browser: ^2.48.1 => 2.55.0
    moment: ^2.29.4 => 2.30.1
    number-to-excel-header: ^1.0.1 => 1.0.1
    openapi-typescript: ^6.7.5 => 6.7.6
    polished: ^4.3.0 => 4.3.1
    prettier: ^3.3.3 => 3.3.3
    react: ^18.2.0 => 18.3.1
    react-accessible-accordion: ^5.0.0 => 5.0.0
    react-auth-code-input: ^3.2.1 => 3.2.1
    react-avatar: ^5.0.3 => 5.0.3
    react-beforeunload: ^2.5.3 => 2.6.0
    react-color: ^2.19.3 => 2.19.3
    react-content-loader: ^7.0.2 => 7.0.2
    react-content-loader/native:  undefined ()
    react-countup: ^6.4.2 => 6.5.3
    react-csv: ^2.2.2 => 2.2.2
    react-datepicker: ^6.0.0 => 6.9.0
    react-dnd: ^16.0.1 => 16.0.1
    react-dnd-html5-backend: ^16.0.1 => 16.0.1
    react-dom: ^18.2.0 => 18.3.1
    react-dropzone: ^14.2.3 => 14.2.3
    react-easy-crop: ^5.0.4 => 5.0.8
    react-helmet: ^6.1.0 => 6.1.0
    react-innertext: ^1.1.5 => 1.1.5
    react-paginate: ^8.1.4 => 8.2.0
    react-password-checklist: ^1.4.3 => 1.8.0
    react-phone-number-input: ^3.3.10 => 3.4.5
    react-phone-number-input/commonjs:  undefined ()
    react-phone-number-input/core:  undefined ()
    react-phone-number-input/flags:  undefined ()
    react-phone-number-input/input-core:  undefined ()
    react-phone-number-input/input-max:  undefined ()
    react-phone-number-input/input-min:  undefined ()
    react-phone-number-input/input-mobile:  undefined ()
    react-phone-number-input/max:  undefined ()
    react-phone-number-input/min:  undefined ()
    react-phone-number-input/mobile:  undefined ()
    react-phone-number-input/react-hook-form:  undefined ()
    react-phone-number-input/react-hook-form-core:  undefined ()
    react-phone-number-input/react-hook-form-input:  undefined ()
    react-phone-number-input/react-hook-form-input-core:  undefined ()
    react-phone-number-input/react-native-input:  undefined ()
    react-phone-number-input/react-styleguidist:  undefined ()
    react-qr-code: ^2.0.15 => 2.0.15
    react-query: ^3.39.3 => 3.39.3
    react-router-dom: ^6.24.0 => 6.26.0
    react-spinners: ^0.13.8 => 0.13.8
    react-toastify: ^9.1.3 => 9.1.3
    react-toggle: ^4.1.3 => 4.1.3
    react-tooltip: ^5.26.4 => 5.28.0
    run-script-os: ^1.1.6 => 1.1.6
    sanitize-html: ^2.13.0 => 2.13.0
    source-map-explorer: ^2.5.3 => 2.5.3
    string-similarity: ^4.0.4 => 4.0.4
    styled-components: ^5.3.10 => 5.3.11
    styled-components/macro:  undefined ()
    styled-components/native:  undefined ()
    styled-components/primitives:  undefined ()
    stylelint: ^16.3.1 => 16.8.1
    terser: ^5.31.3 => 5.31.3
    ts-jest: ^29.1.1 => 29.2.4
    ts-jest-mock-import-meta: ^1.2.0 => 1.2.0
    ts-key-enum: ^2.0.12 => 2.0.12
    typescript: ^5.4.5 => 5.5.4
    uuid: ^9.0.0 => 9.0.1
    vite: ^5 => 5.3.5
    vite-plugin-babel-macros: ^1.0.6 => 1.0.6
    vite-plugin-checker: ^0.6.4 => 0.6.4
    vite-tsconfig-paths: ^4.3.2 => 4.3.2
  npmGlobalPackages:
    corepack: 0.28.1
    npm: 10.7.0

Describe the bug

Upon deploying an application to production that uses SSO / SAML / SignInWithRedirect via Cognito, stored in CookieStorage, it is observed that once the SAML idP redirects the user back to the application with the ?code=xxxx&state=xxxx in the URL, Cognito is storing the oauthState, inflightOAuth, and oauthPKCE key/values in localstorage instead of cookiestorage.

As a result, the application hangs at a white screen as Amplify does not seem to know how to process the incoming oAuth code.

The cookiestorage settings are properly in place and work successfully for username/password login, as the various Cognito keys are seen in cookiestorage correctly.

This is only noticed when the environment is using a valid SSL certificate / production.

Expected behavior

oAuth related key/values are stored in CookieStorage for Amplify/auth to utilize them

Reproduction steps

  1. Implement Amplify/auth with Cookie Storage following https://docs.amplify.aws/gen1/react/build-a-backend/auth/manage-user-session/#update-your-token-saving-mechanism
  2. Implement a SignInWithRedirect / SAML auth flow
  3. Test in an environment with a valid SSL certificate with secure true on the Cookie storage parameters
  4. Note the SAML redirect stores oauthState, inflightOAuth, and oauthPKCE key/values in localstorage and Amplify/auth is unable to proceed.

Code Snippet

// Put your code below this line.

Amplify.configure(
{
	Auth: {
		Cognito: {
			userPoolId: import.meta.env.VITE_COGNITO_USER_POOL_ID,
			userPoolClientId: import.meta.env.VITE_COGNITO_USER_POOL_WEB_CLIENT_ID,
			loginWith: {
				// SSO configuration
				oauth: {
					domain: import.meta.env.VITE_COGNITO_DOMAIN,
					scopes: ["phone", "email", "openid", "aws.cognito.signin.user.admin", "profile"],
					redirectSignIn: [import.meta.env.VITE_COGNITO_REDIRECT_SIGN_IN],
					redirectSignOut: [import.meta.env.VITE_COGNITO_REDIRECT_SIGN_OUT],
					responseType: "code",
				},
			},
		},
	},
},
)

cognitoUserPoolsTokenProvider.setKeyValueStorage(
new CookieStorage({
	domain: app.example.com,
	expires: 365,
	sameSite: "strict",
	secure: true,
}),
)

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

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (10 by maintainers)

Most upvoted comments

Hi @sators, much appreciated providing the sample code!

I can reproduce this issue, but this is not an issue from Amplify. I checked the production build of the vitecreate app; it creates separate JS bundles per use case in your app even though it is a SPA. E.g., there will be a Login.js bundle created for your Login.tsx, where the signInWithRedirect() API gets called. When importing this API, it will add an OAuth listener to capture the code parameter and complete the sign-in.

The app renders the login form, so the Login.js bundle gets lazily loaded. However, when it redirects back from your SSO provider, at this point, your app doesn’t render anything while waiting for getCurrentUser() to resolve. So the Login.js bundle will not get loaded. Hence, the OAuth listener never gets added. As the OAuth never completes, the call of getCurrentUser() never resolves, and you get stuck.

To resolve this, you can add the following to the index.tsx:

import "aws-amplify/auth/enable-oauth-listener";

This ensures the OAuth listener is added to the “root” bundle of your app, so it’s always available when needed.

Thank you so much for the help here. I confirm adding import "aws-amplify/auth/enable-oauth-listener"; in the root files of the project solved this!

I didn’t encounter these issues with v5 so it appeared to me like a v6 issue

We improved the tree shakability in v6 the listener gets bundled only when it’s actually used. 😃

@sators, thank you for providing the sample repo. We’ll work on digging into this on our side to reproduce and follow up with further questions.

Strangely enough, I haven’t been able to reproduce the issue on localhost, only production with Chrome. Safari has not been tested. I will add the additional logging per @HuiSF and follow up.