aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.42k stars 2.12k forks source link

React Native email/password login broken in monorepo #13953

Open mikeislearning opened 2 days ago

mikeislearning commented 2 days ago

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

Authentication

Amplify Version

v6

Amplify Categories

auth

Backend

None

Environment information

``` # Put output below this line System: OS: macOS 15.0.1 CPU: (10) arm64 Apple M1 Max Memory: 61.47 MB / 32.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 20.14.0 - ~/.local/share/mise/installs/node/20.14.0/bin/node Yarn: 4.5.0 - ~/.local/share/mise/installs/node/20.14.0/bin/yarn npm: 10.7.0 - ~/.local/share/mise/installs/node/20.14.0/bin/npm Watchman: 2024.09.09.00 - /opt/homebrew/bin/watchman Browsers: Chrome: 130.0.6723.59 Edge: 130.0.2849.52 Safari: 18.0.1 npmGlobalPackages: corepack: 0.28.1 firebase-tools: 13.22.1 npm: 10.7.0 ```

Describe the bug

Our team is using a turbo-based monorepo with both a react web app and a react native mobile app, in addition to our node.js server.

When we trying logging in using email+password on the react web app, everything works as expected. When we try the same login with the react-native mobile app, it is unable to find the configuration, with the error message:

[AuthUserPoolException: Auth UserPool not configured.]

Hope to hear from you!

Expected behavior

We would expected this to allow a user to login via amplify with an email + password using the react-native app

Reproduction steps

It seems like it'll be tough to reproduce without someone setting up a full turbo monorepo, so we do already have a fix for it with a patch we created using patch-package located at <root>/patches/@aws-amplify+auth+6.5.3.patch. Here's the code:

diff --git a/node_modules/@aws-amplify/auth/dist/cjs/providers/cognito/tokenProvider/TokenStore.js b/node_modules/@aws-amplify/auth/dist/cjs/providers/cognito/tokenProvider/TokenStore.js
index a0b7c92..1406e3b 100644
--- a/node_modules/@aws-amplify/auth/dist/cjs/providers/cognito/tokenProvider/TokenStore.js
+++ b/node_modules/@aws-amplify/auth/dist/cjs/providers/cognito/tokenProvider/TokenStore.js
@@ -2,6 +2,7 @@

 Object.defineProperty(exports, "__esModule", { value: true });
 exports.getAuthStorageKeys = exports.createKeysForAuthStorage = exports.DefaultTokenStore = void 0;
+const core_1 = require("@aws-amplify/core");
 const utils_1 = require("@aws-amplify/core/internals/utils");
 const AuthError_1 = require("../../../errors/AuthError");
 const types_1 = require("./types");
@@ -124,9 +125,11 @@ class DefaultTokenStore {
         ]);
     }
     async getAuthKeys(username) {
-        (0, utils_1.assertTokenProviderConfig)(this.authConfig?.Cognito);
+        const authConfig = core_1.Amplify.getConfig().Auth;
+        (0, utils_1.assertTokenProviderConfig)(authConfig.Cognito);
+        this.setAuthConfig(authConfig);
         const lastAuthUser = username ?? (await this.getLastAuthUser());
-        return (0, exports.createKeysForAuthStorage)(this.name, `${this.authConfig.Cognito.userPoolClientId}.${lastAuthUser}`);
+        return (0, exports.createKeysForAuthStorage)(this.name, `${authConfig.Cognito.userPoolClientId}.${lastAuthUser}`);
     }
     getLastAuthUserKey() {
         (0, utils_1.assertTokenProviderConfig)(this.authConfig?.Cognito);

### Code Snippet

```javascript
// Put your code below this line.

Log output

``` // Put your logs below this line [AuthUserPoolException: Auth UserPool not configured.] ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

iOS and Android

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

HuiSF commented 2 days ago

Hi @mikeislearning thanks for opening this issue. It sounds like that your project ended up have multiple versions / instances of the @aws-amplify/core package. Could you confirm that in your monorepo set up, the dependencies tree is consistent, and no duplicate dependencies were produced?