Search code examples
amazon-web-servicesvue.jsaws-amplify-cliaws-amplify

AWS Amplify 'currentUserCredentials()' returns unexpected authenticated value, bug?


When calling Auth.currentUserCredentials() after Auth.signIn() I get valid valid credentials, but for an unauthenticated user, why?

Code:

   async signIn({ dispatch, state }, { email, password }) {

  try {
    const user = await Auth.signIn(email, password);
    console.log("User state after login")
    const userCredentialsAfterLogin = await Auth.currentUserCredentials();
    console.log(userCredentialsAfterLogin)

  } catch (error) {
    console.log("error")
    console.log(error)
    return
  }
  await dispatch('fetchUser')
},

Expected behaviour:

After signing in with a valid user, Auth.currentUserCredentials() should return an authenticated set of Credentials.

Actual behaviour:

Auth.currentUserCredentials() returns an unauthenticated user with the authenticated property set to false and a 400 error, "ResourceNotFoundException: IdentityPool 'eu-west-1:62dab5ed-5a84-4064-a7a2-87d1d0df511b'

System:

  • authenticationFlowType: "USER_SRP_AUTH"
  • Versions: "aws-amplify": "^3.3.14", "aws-amplify-vue": "^2.1.4", amplify version 4.42.0

config

{
  "authSelections": "userPoolOnly",
  "resourceName": "testapp89e81d50",
  "serviceType": "imported",
  "region": "eu-west-1"
}

Solution

  • I understand where you're coming from, and honestly I can't really show you clear documentation that exactly states why this won't work. AWS documentation on Cognito and Amplify is difficult to piece together, both because the Amplify framework still uses an old library under the hood ('amazon-cognito-identity-js') and Cognito is the name for both a connect-login-with-IAM and signup/signin-as-a-service offering. Cognito is super powerful and rock solid in terms of security if done right, but the setup is a bit of a pain.

    There's a bit of documentation, e.g. the API docs of Amplify Auth here. You can see there that currentCredentials / currentUserCredentials gives you some object which contains among other things an 'identitiId'. Credentials, in the Amplify Auth framework, refer to AWS IAM credentials that refer to an Cognito Identity.

    You seem to be using Amplify to login to a Cognito User Pool, using email/password. A Cognito User Pool can be connected to a Cognito Identify pool, to 'exchange' a Cognito JWT token for some credentials that can be used to use AWS resources (IAM credentials). This is not needed to have a normal sign in / sign up flow working though.

    So, the question is: what do you want?

    1. Do you want to know about the currently logged in Cognito User Pool user, e.g. his email, JWT token and other fields that are stored in the JWT token? Use 'currentUserInfo' or 'currentUserSession'

    2. Do you actually want to have some IAM credentials to invoke AWS resources? Make sure to create and connect your Cognito User Pool with a Cognito Identity Pool and configure your Identity Pool id in your frontend settings. If you've done that, you should be able to use 'currentCredentials'.