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

Error using federated sign-in #1323

Closed maksimsemenov closed 5 years ago

maksimsemenov commented 6 years ago

Do you want to request a feature or report a bug? Report a bug

What is the current behavior? I'm trying to use a federated sign-in, but getting an error: TypeError: Cannot read property 'identityPoolId' of undefined

at Credentials._setCredentialsFromFederation (react-dom.development.js:17306)
at Credentials.set (react-dom.development.js:17306)
at react-dom.development.js:17306
at new Promise (<anonymous>)
at AuthClass.federatedSignIn (react-dom.development.js:17306)
at ProxyComponent._callee$ (_overRest.js:37)
at tryCatch (_iter-define.js:70)
at Generator.invoke [as _invoke] (es6.array.iterator.js:35)
at Generator.prototype.(:1234/anonymous function) [as next] (http://localhost:1234/src.539ec86d.js:4889:21)
at step (_overRest.js:3)

My files: index.tsx

...
import Amplify from 'aws-amplify'

Amplify.configure({
  Auth: {
    identityPoolId: process.env.AWS_IDENTITY_POOL_ID,
    mandatorySignIn: true,
    region: process.env.AWS_REGION,
    userPoolId: process.env.AWS_USER_POOL_ID,
    userPoolWebClientId: process.env.AWS_USER_POOL_CLIENT_ID,

    cookieStorage: {
      domain: process.env.AWS_COOKIE_DOMAIN
    }
  }
})

...

GoogleSignIn.tsx

async federatedSignIn(googleUser: gapi.auth2.GoogleUser) {
    const { id_token, expires_at } = googleUser.getAuthResponse()
    const profile = googleUser.getBasicProfile()
    let user = {
      email: profile.getEmail(),
      name: profile.getName()
    }

    if (
      !Auth ||
      typeof Auth.federatedSignIn !== 'function' ||
      typeof Auth.currentAuthenticatedUser !== 'function'
    ) {
      throw new Error('No Auth module found, please ensure @aws-amplify/auth is imported')
    }

    await Auth.federatedSignIn('google', { token: id_token, expires_at }, user) <- error is happening here

    user = await Auth.currentAuthenticatedUser()
  }

What is the expected behavior? It should sign user in using google token

Which versions of Amplify, and which browser / OS are affected by this issue? Did this work in previous versions? "aws-amplify": "^1.0.2" MacOS: 10.13.6 Google Chrome: 67.0.3396.99

You can turn on the debug mode to provide more info for us by setting window.LOG_LEVEL = 'DEBUG'; in your app.

taschik commented 6 years ago

We are running into the same issue in our react-native mobile client. The only difference is that we are using Facebook as federated authority. It works with aws-amplify 1.0.0 but not with any higher version.

kasperbe commented 6 years ago

I sniffed around the code a little for federatedSignIn:

    AuthClass.prototype.federatedSignIn = function (provider, response, user) {
        var token = response.token, identity_id = response.identity_id, expires_at = response.expires_at;
        var that = this;
        return new Promise(function (res, rej) {
            core_1.Credentials.set({ provider: provider, token: token, identity_id: identity_id, user: user, expires_at: expires_at }, 'federation').then(function (cred) {
                dispatchAuthEvent('signIn', that.user);
                logger.debug('federated sign in credentials', cred);
                res(cred);
                return;
            }).catch(function (e) {
                rej(e);
                return;
            });
        });
    };

It seems as though the second parameters, response, is expected to carry an identity_id, however this param is used to pass in the response from the Auth provider, in this case facebook.

Auth.federatedSignIn('facebook', { token, expires_at: expires}, { name: 'USER_NAME' })

Here { token, expires_at: expires} is passed, which obviously does not contain an identity_id.

kasperbe commented 6 years ago

Actually, this seems to be a feature to let the user define the identity id for the identity pool. Super nice guys. I was working with example code from: https://aws-amplify.github.io/amplify-js/media/authentication_guide and had completely forgotten that I need to import the aws-exports. This seems to be working just fine in newest version of aws-amplify. Nice. :)

taschik commented 6 years ago

I can't confirm that this is not working for 1.0.8 for us. Our code looks like this:

const identityPoolId = CognitoFederatedIdentityPoolId || process.env.COGNITO_FEDERATED_IDENTITY_POOL_ID
const region = CognitoRegion || process.env.COGNITO_REGION
const userPoolId = CognitoUserPoolId || process.env.COGNITO_USER_POOL_ID
const userPoolWebClientId = CognitoUserPoolClientId || process.env.COGNITO_USER_POOL_CLIENT_ID
const cognitoAuthProvider = CognitoAuthProviderId || process.env.COGNITO_AUTH_PROVIDER_ID

const auth = {
  Auth: {
    identityPoolId,
    region,
    userPoolId,
    userPoolWebClientId,
    mandatorySignIn: false,
  },
}
Logger.debug(auth)
Amplify.configure(auth)

The values for auth are all set regarding to the Logger output. When we then call Auth to get the federatedCredentials with this function here:

export const getFederatedCredentials = async () => {
  let federatedCredentials
  try {
    federatedCredentials = await Auth.currentCredentials()
  } catch (err) {
    Logger.debug('Error getFederatedCredentials: ', err)
  }
  return federatedCredentials
}

We see the following error:

Error getFederatedCredentials:  TypeError: Cannot read property 'identityPoolId' of undefined
    at Credentials.<anonymous> (Credentials.js:177)
    at step (Credentials.js:40)
    at Object.next (Credentials.js:21)
    at Credentials.js:15
    at tryCallTwo (core.js:45)
    at doResolve (core.js:200)
    at new Promise (core.js:66)
    at __awaiter (Credentials.js:11)
    at Credentials._setCredentialsForGuest (Credentials.js:171)
    at Credentials.set (Credentials.js:339)

If we debug and take a look at the Amplify.Auth object, it seems to be empty:

{
currentUserCredentials: function ()
user: null
userPool: null
_cognitoAuthClient: null
_gettingCredPromise:null
__proto__: Object
}

It all works with 1.0.0 but not with any version higher.

taschik commented 6 years ago

Is there any update on this issue? I am still blocked by it and cannot upgrade aws-amplify.

powerful23 commented 6 years ago

@maksimsemenov @taschik I tested Auth Sign In with aws-amplify@1.1.3 and aws-amplify-react-native@2.0.3 with no such error.

If the Auth module is not configured, it might be caused by duplicate packages under your node_modules. Can you update your aws-amplify and aws-amplify-react-native(if exists) to the latest and make sure there is no node_modules directory under node_modules/aws-amplify/? If you could paste your dependencies that would be better.

strykerCrew commented 6 years ago

@powerful23 does this work when you use your own components or are your restricted to using the aws-amplify-react-native Authenticator? Have you tried without aws-amplify-react-native?

rafaelgrilli92 commented 5 years ago

For those who still with the problem, I just had to call the configure method from the Amplify lib before using the Auth methods. Like that:

import Amplify from 'aws-amplify';
import awsConfig from './src/aws-exports';
Amplify.configure(awsConfig);
powerful23 commented 5 years ago

Guys, the issue described in this thread cannot be reproduced in the latest version. Please provide more details if you still have this issue.

powerful23 commented 5 years ago

Closing the issue due to no further response. Please feel free to reopen if you still have this issue.

higherstar commented 5 years ago

@powerful23

I am getting this issue.

import API from '@aws-amplify/api'; import config from '../../src/aws-exports'

API.configure(config); ...

And I get this error.

API - ensure credentials error TypeError: Cannot read property 'identityPoolId' of undefined

vubui commented 4 years ago

@powerful23

I am getting this issue.

import API from '@aws-amplify/api'; import config from '../../src/aws-exports'

API.configure(config); ...

And I get this error.

API - ensure credentials error TypeError: Cannot read property 'identityPoolId' of undefined

I am getting the same issue. I tried what rafaelgrilli92@ suggested. It didn't work. 🤔

debajit commented 4 years ago

I was getting the same error, but I was able to fix the issue by changing API.configure to Amplify.configure

lianguage commented 4 years ago

Hi Guys.

I was having this problem just now too. It seems when you run:

Amplify.configure(awsconf);

your conf needs to have the oauth section populated, eg:

oauth: {
            domain: 'auth.domain.com',
            scope: ['phone', 'email', 'profile', 'openid', 'aws.cognito.signin.user.admin'],
            redirectSignIn: 'https://domain.com/',
            redirectSignOut: 'https://domain.com/',
            responseType: 'code' 
        }

I was missing this and my page no longer crashed after populating it.

fireflysemantics commented 4 years ago

I'm also having this issue with Angular. I have not run the Amplify CLI in a while though, so I'm going to try upgrading and running it from scratch.

Perhaps there's been a field configuration name change. This is the error I'm getting when running in production mode:

[ERROR] 17:40.644 AuthError - 
            Error: Amplify has not been configured correctly.
            This error is typically caused by one of the following scenarios:

            1. Make sure you're passing the awsconfig object to Amplify.configure() in your app's entry point
                See https://aws-amplify.github.io/docs/js/authentication#configure-your-app for more information

            2. There might be multiple conflicting versions of aws-amplify or amplify packages in your node_modules.
                Try deleting your node_modules folder and reinstalling the dependencies with `yarn install`

e._log @ main.2b2c20736396d77cab9a.js:1
main.2b2c20736396d77cab9a.js:1 [ERROR] 17:40.655 AuthError - 
            Error: Amplify has not been configured correctly.
            This error is typically caused by one of the following scenarios:

            1. Make sure you're passing the awsconfig object to Amplify.configure() in your app's entry point
                See https://aws-amplify.github.io/docs/js/authentication#configure-your-app for more information

            2. There might be multiple conflicting versions of aws-amplify or amplify packages in your node_modules.
                Try deleting your node_modules folder and reinstalling the dependencies with `yarn install`

e._log @ main.2b2c20736396d77cab9a.js:1
main.2b2c20736396d77cab9a.js:1 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'identityPoolId' of undefined
TypeError: Cannot read property 'identityPoolId' of undefined
    at e.<anonymous> (main.2b2c20736396d77cab9a.js:1)
    at main.2b2c20736396d77cab9a.js:1
    at Object.next (main.2b2c20736396d77cab9a.js:1)
    at main.2b2c20736396d77cab9a.js:1
    at new D (polyfills.0bf894b4fb950da0c828.js:1)
    at ce (main.2b2c20736396d77cab9a.js:1)
    at e.federatedSignIn (main.2b2c20736396d77cab9a.js:1)
    at e.socialSignIn (main.2b2c20736396d77cab9a.js:1)
    at e.signInWithGoogle (main.2b2c20736396d77cab9a.js:1)
    at e.login (main.2b2c20736396d77cab9a.js:1)
    at T (polyfills.0bf894b4fb950da0c828.js:1)
    at new D (polyfills.0bf894b4fb950da0c828.js:1)
    at ce (main.2b2c20736396d77cab9a.js:1)
    at e.federatedSignIn (main.2b2c20736396d77cab9a.js:1)
    at e.socialSignIn (main.2b2c20736396d77cab9a.js:1)
    at e.signInWithGoogle (main.2b2c20736396d77cab9a.js:1)
    at e.login (main.2b2c20736396d77cab9a.js:1)
    at main.2b2c20736396d77cab9a.js:1
    at Is (main.2b2c20736396d77cab9a.js:1)
    at i (main.2b2c20736396d77cab9a.js:1)

I'm using Amplify.configure(...) and I do have the oauth section.

The Amplify CLI version I used to generate my configuration was 4.18.1.

And the installed version of amplify was "aws-amplify": "^3.1.1"

I tried installing aws-amplify again and the fresh install points to 3.2.0 so perhaps there was a problem with 3.1.1.

I'm still getting the error. This is my aws-exports configuration (I've added additional random characters. The app is deployed here and if you open the console you can see the errors:

https://csv.fireflysemantics.com/welcome

/* eslint-disable */
// WARNING: DO NOT EDIT. This file is automatically generated by AWS Amplify. It will be overwritten.

const awsmobile = {
    "aws_project_region": "us-east-1",
    "aws_cognito_identity_pool_id": "us-east-1:9eb0ffb5-8050-4d4a-9858-bbsfsadfsdafsa",
    "aws_cognito_region": "us-east-1",
    "aws_user_pools_id": "us-east-1_vxmgPo5dfsafdassdafsaDZ",
    "aws_user_pools_web_client_id": "7r1fj3v6uj25safsadfao0tusbhp9hhcnf",
    "oauth": {
        "domain": "csve6db5sdfasdfsc6d-e6dbdsfsadfa5c6d-production.auth.us-east-1.amazoncognito.com",
        "scope": [
            "phone",
            "email",
            "openid",
            "profile",
            "aws.cognito.signin.user.admin"
        ],
        "redirectSignIn": "https://csv.fireflysemantics.com/",
        "redirectSignOut": "https://csv.fireflysemantics.com/",
        "responseType": "code"
    },
    "federationTarget": "COGNITO_USER_POOLS"
};

export default awsmobile;

Also after upgrading the dependency I'm not only getting this error:

main.f4c94e7a45a85489ed95.js:1 [ERROR] 49:06.772 AuthError - 
            Error: Amplify has not been configured correctly.
            This error is typically caused by one of the following scenarios:

            1. Make sure you're passing the awsconfig object to Amplify.configure() in your app's entry point
                See https://aws-amplify.github.io/docs/js/authentication#configure-your-app for more information

            2. There might be multiple conflicting versions of aws-amplify or amplify packages in your node_modules.
                Try deleting your node_modules folder and reinstalling the dependencies with `yarn install`

e._log @ main.f4c94e7a45a85489ed95.js:1
Promise.then (async)
k @ polyfills.0bf894b4fb950da0c828.js:1
scheduleTask @ polyfills.0bf894b4fb950da0c828.js:1
scheduleTask @ polyfills.0bf894b4fb950da0c828.js:1
scheduleMicroTask @ polyfills.0bf894b4fb950da0c828.js:1
Z @ polyfills.0bf894b4fb950da0c828.js:1
then @ polyfills.0bf894b4fb950da0c828.js:1
bootstrapModule @ main.f4c94e7a45a85489ed95.js:1
zUnb @ main.f4c94e7a45a85489ed95.js:1
l @ runtime.e227d1a0e31cbccbf8ec.js:1
0 @ main.f4c94e7a45a85489ed95.js:1
l @ runtime.e227d1a0e31cbccbf8ec.js:1
t @ runtime.e227d1a0e31cbccbf8ec.js:1
r @ runtime.e227d1a0e31cbccbf8ec.js:1
(anonymous) @ main.f4c94e7a45a85489ed95.js:1
main.f4c94e7a45a85489ed95.js:1 [ERROR] 49:06.780 AuthError - 
            Error: Amplify has not been configured correctly.
            This error is typically caused by one of the following scenarios:

            1. Make sure you're passing the awsconfig object to Amplify.configure() in your app's entry point
                See https://aws-amplify.github.io/docs/js/authentication#configure-your-app for more information

            2. There might be multiple conflicting versions of aws-amplify or amplify packages in your node_modules.
                Try deleting your node_modules folder and reinstalling the dependencies with `yarn install`

e._log @ main.f4c94e7a45a85489ed95.js:1
Promise.then (async)
k @ polyfills.0bf894b4fb950da0c828.js:1
scheduleTask @ polyfills.0bf894b4fb950da0c828.js:1
scheduleTask @ polyfills.0bf894b4fb950da0c828.js:1
scheduleMicroTask @ polyfills.0bf894b4fb950da0c828.js:1
Z @ polyfills.0bf894b4fb950da0c828.js:1
then @ polyfills.0bf894b4fb950da0c828.js:1
bootstrapModule @ main.f4c94e7a45a85489ed95.js:1
zUnb @ main.f4c94e7a45a85489ed95.js:1
l @ runtime.e227d1a0e31cbccbf8ec.js:1
0 @ main.f4c94e7a45a85489ed95.js:1
l @ runtime.e227d1a0e31cbccbf8ec.js:1
t @ runtime.e227d1a0e31cbccbf8ec.js:1
r @ runtime.e227d1a0e31cbccbf8ec.js:1
(anonymous) @ main.f4c94e7a45a85489ed95.js:1
welcome:1 Failed to load resource: the server responded with a status of 404 ()
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.