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

Auth.currentCredentials() fails with exception "No Auth module registered in Amplify" #6750

Closed abhjaw closed 4 years ago

abhjaw commented 4 years ago

Describe the bug Auth.currentCredentials() fails with exception "No Auth module registered in Amplify". The same code was working a couple of weeks back (in fact I have it working in a Cloud9 environment). Any new installs however are not working for this code.

To Reproduce Steps to reproduce the behavior:

  1. Have a cognito identity pool and user pool configured with identity pool having "Enable access to unauthenticated identities" checked.
  2. Use the Auth.currentCredentials() call to get either unauth or auth (in case a user is logged in to the user pool) credentials.

Expected behavior Get either unauth or authenticated credentials as the case may be. Use these credentials to call STS assume role with the right permissions to access Amazon Kendra index, S3 objects etc. Code Snippet This code snippet has stopped working

async componentDidMount() {
    console.log("AWS Exports: ", aws_exports);
    Auth.configure(aws_exports);
    //console.log("Sleep for 10 seconds");
    //await this.sleep(10000);
    try {
      let credentials = await Auth.currentCredentials();
      //Use Cognito Credentials either authorized or unauthorized to get temporary credentials for Kendra
      this.setInfra(credentials.accessKeyId, credentials.secretAccessKey, credentials.sessionToken);
    } catch (e) {
      console.log("Auth exception: ", e);
    }
  }

This code snippet continues to work

 authChangeState = async (nextAuthState: AuthState) => {
    let user = undefined;
    let groups = undefined;
    try {
      user = await Auth.currentAuthenticatedUser();
      groups = user.signInUserSession.accessToken.payload['cognito:groups'];
      console.log('Signed in user, groups:', user, groups);
      let credentials = await Auth.currentCredentials();
      console.log('Got credentials:', credentials);
      this.setInfra(credentials.accessKeyId, credentials.secretAccessKey, credentials.sessionToken);
    } catch {
      console.log('currentAuthenticatedUser Exception');
    }
    if (nextAuthState === AuthState.SignedIn){
      this.setState({loginScreen:false, authUser: true, user: user ? user!.username : undefined, groups: groups ? groups : undefined});
    } else {
      this.setState({loginScreen:true, authUser: true, user: user ? user!.username : undefined, groups: groups ? groups : undefined});
    }
  }

aws_exports.js

 const awsmobile = {
    "aws_project_region": "us-east-1",
    "aws_cognito_identity_pool_id": "us-east-1:XXXXXXX",
    "aws_cognito_region": "us-east-1",
    "aws_user_pools_id": "us-east-1_XXXXXX",
    "aws_user_pools_web_client_id": "XXXXXXXX",
    "oauth": {}
};

export default awsmobile;
<details>
  <summary><strong>Environment</strong></summary>

<!-- Please run the following command inside your project and copy/paste the output into the codeblock: -->

admin:~/environment/kendra-poc (master) $ npx envinfo --system --binaries --browsers --npmPackages --npmGlobalPackages npx: installed 1 in 1.805s

System: OS: Linux 4.14 Amazon Linux AMI 2018.03 CPU: (2) x64 Intel(R) Xeon(R) CPU E5-2676 v3 @ 2.40GHz Memory: 1.66 GB / 3.85 GB Container: Yes Shell: 4.2.46 - /bin/bash Binaries: Node: 10.22.0 - ~/.nvm/versions/node/v10.22.0/bin/node npm: 6.14.6 - ~/.nvm/versions/node/v10.22.0/bin/npm npmPackages: @aws-amplify/cli: ^4.29.0 => 4.29.0 @aws-amplify/ui-react: ^0.2.18 => 0.2.18 @testing-library/jest-dom: ^4.2.4 => 4.2.4 @testing-library/react: ^9.5.0 => 9.5.0 @testing-library/user-event: ^7.2.1 => 7.2.1 @types/jest: ^24.9.1 => 24.9.1 @types/lodash: ^4.14.161 => 4.14.161 @types/node: ^12.12.55 => 12.12.55 @types/rc-slider: ^8.6.6 => 8.6.6 @types/react: ^16.9.49 => 16.9.49 @types/react-dom: ^16.9.8 => 16.9.8 aws-amplify: ^3.1.1 => 3.1.1 aws-sdk: ^2.747.0 => 2.747.0 bootstrap: ^4.5.2 => 4.5.2 moment: ^2.27.0 => 2.27.0 node-sass: ^4.14.1 => 4.14.1 rc-slider: ^9.2.4 => 9.2.4 react: ^16.13.1 => 16.13.1 react-bootstrap: ^1.3.0 => 1.3.0 react-dom: ^16.13.1 => 16.13.1 react-google-charts: ^3.0.15 => 3.0.15 react-scripts: 3.4.3 => 3.4.3 semantic-ui-react: ^1.2.1 => 1.2.1 typescript: ^3.9.7 => 3.9.7 npmGlobalPackages: @aws-amplify/cli: 4.29.0 @aws-amplify/ui-react: 0.2.15 @types/lodash: 4.14.161 @types/rc-slider: 8.6.6 aws-amplify: 3.0.24 aws-sdk: 2.739.0 bootstrap: 4.5.2 cdk: 1.61.0 coffeescript: 2.5.1 esformatter: 0.11.3 js-beautify: 1.13.0 moment: 2.27.0 node-sass: 4.14.1 npm: 6.14.6 prettier: 2.1.1 rc-slider: 9.2.4 react-bootstrap: 1.3.0 react-google-charts: 3.0.15 semantic-ui-react: 1.2.1 typescript: 4.0.2



</details>
swyxio commented 4 years ago

my sympathies for this issue.

i dont know the codebase well so i took a look. this is the only place this error occurs: https://github.com/aws-amplify/amplify-js/blob/1cb1afd1e56135908dceb2ef6403f0b3e78067fe/packages/core/src/Credentials.ts#L108-L112

therefore it probably is that Amplify.Auth is falsy. based on this comment, Auth has to be imported: https://github.com/aws-amplify/amplify-js/blob/1cb1afd1e56135908dceb2ef6403f0b3e78067fe/packages/core/src/Credentials.ts#L36

I observe that Amplify Auth relies on a curious antipattern of registering as a side effect: https://github.com/aws-amplify/amplify-js/blob/1cb1afd1e56135908dceb2ef6403f0b3e78067fe/packages/auth/src/Auth.ts#L2087

as a first check - have you cleaned up your imports somehow since you ran this code without issue (some automated tooling cleans unused imports, which is a problem if importing has side effects)? do you still import Amplify, { Auth } from 'aws-amplify';?

if you are satisfied that those things have not changed - is there caching or some other race condition going on that caused your unchanged code to now stop working - and if so, can you clear all sources of cache to verify this?

elorzafe commented 4 years ago

@abhjaw are you running Amplify on a node environment or this runs on a browser?

abhjaw commented 4 years ago

@abhjaw are you running Amplify on a node environment or this runs on a browser?

I am using Cloud9 IDE and I am using the preview browser in Cloud9.

abhjaw commented 4 years ago

my sympathies for this issue. Thank you :)

as a first check - have you cleaned up your imports somehow since you ran this code without issue (some automated tooling cleans unused imports, which is a problem if importing has side effects)? do you still import Amplify, { Auth } from 'aws-amplify';?

My imports statements are the same: import React from "react"; import Button from 'react-bootstrap/Button'; import Search from "./search/Search"; import { indexId, region } from "./services/Kendra"; import { facetConfiguration } from "./search/configuration"; import S3 from 'aws-sdk/clients/s3'; import AWS from 'aws-sdk'; import aws_exports from './aws-exports'; import Kendra from 'aws-sdk/clients/kendra'; import {Auth} from '@aws-amplify/auth'; import { AmplifyGreetings, AmplifyAuthenticator } from '@aws-amplify/ui-react'; import { AuthState } from '@aws-amplify/ui-components';

import "./App.css";

if you are satisfied that those things have not changed - is there caching or some other race condition going on that caused your unchanged code to now stop working - and if so, can you clear all sources of cache to verify this?

I cleared the browser cache but still had the same issue. Also tried in a different browser (Firefox instead of Chrome). But it is still having the same problem.

swyxio commented 4 years ago

@abhjaw you are using modular imports there: https://docs.amplify.aws/lib/auth/advanced/q/platform/js#using-modular-imports

did you run Auth.configure();?

notice the difference between @aws-amplify/auth and aws-amplify

abhjaw commented 4 years ago

@abhjaw you are using modular imports there: https://docs.amplify.aws/lib/auth/advanced/q/platform/js#using-modular-imports

did you run Auth.configure();?

notice the difference between @aws-amplify/auth and aws-amplify

Yes, I am doing Auth.configure(aws_exports);

abhjaw commented 4 years ago

I just changed Auth.currentCredentials() to Augh.currentUserCredentials() and the thing started working.

kevinsperrine commented 4 years ago

I ran into a similar issue when trying to update pinpoint endpoints, so instead of calling configure for the individual packages, called configure from the core package and it solved the problem.

import Auth from "@aws-amplify/auth";
import Storage from "@aws-amplify/storage";
import API from "@aws-amplify/api";
import Analytics from "@aws-amplify/analytics";
+ import Amplify from "@aws-amplify/core";

Amplify.Logger.LOG_LEVEL = "VERBOSE";
+ Amplify.configure(Config);
- Auth.configure(Config.Auth);
- Storage.configure(Config.Storage);
- API.configure(Config.API);
- Analytics.configure(Config.Analytics);
achaphiv commented 4 years ago

Same issue here, but I don't have unauthenticated users enabled.

In my nuxt project:

plugins/aws-amplify.client.js

import Auth from '@aws-amplify/auth'
import awsConfig from '@/config/aws-config'

export default async ({ store, $axios }) => {
  Auth.configure(awsConfig)

I had to downgrade dependencies to work again.

package.json

  "dependencies": {
    "@aws-amplify/auth": "~3.3.0",
  },
  "resolutions": {
    "@aws-amplify/core": "~3.4.0"
  }
What Bad Good
@aws-amplify/auth 3.4.1 3.3.6
@aws-amplify/core 3.5.1 3.4.7

I suspect the problem lies more in @aws-amplify/core, but I downgraded both to be safe.

sammartinez commented 4 years ago

6806

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.