"Incorrect username or password" when calling Auth.signIn from jest automated test. Auth.signIn succeeds when called from UI or HTTP call
See original GitHub issueNOTE: See my comment from Aug 27th 2020 for an update. It’s been narrowed to the Auth.signIn method.
Describe the bug
Executing an automated test in jest confirming a new user via adminConfirmSignUp
and then calling Auth.signIn
fails with “Incorrect username or password” but if the same user and pass are used via HTTP authentication endpoint or application UI the signin succeeds.
To Reproduce Steps to reproduce the behavior:
- Create a user using
Auth
signUp
. An email is sent with a confirmation code. - As this is a test, call
CognitoIdentityServiceProvider
adminConfirmSignUp
andadminUpdateUserAttributes
to confirm user and email respectively. - Call
Auth``signIn
- an
Incorrect username or password.
error message is received.
Expected behavior
I expect the user to be correctly signed in as the user, password, userpoolId, region, etc are correct. This is verified because the user is correctly added to Cognito users pool, Account status
is CONFIRMED
and the Email verified
is also correctly set to true
.
Code Snippet
try {
const userIdFragment = shortid.generate();
const username = `<real_emailuser>+${userIdFragment}@anemail.com`;
const password = "password20";
const signedUpUser = await Auth.signUp({
username,
password,
attributes: {
email: username,
name: "Test",
family_name: "Test",
},
});
const credentials = new SharedIniFileCredentials({
profile: "<my-amplify-profile>",
});
const cognitoIdentityServiceProvider = new CognitoIdentityServiceProvider({
accessKeyId: credentials.accessKeyId,
secretAccessKey: credentials.secretAccessKey,
region: config.aws_cognito_region,
});
const params = {
UserPoolId: config.aws_user_pools_id,
Username: username,
};
const result = await cognitoIdentityServiceProvider.adminConfirmSignUp(params).promise();
const params2 = {
UserAttributes: [
{
Name: "email_verified",
Value: "true",
},
],
UserPoolId: config.aws_user_pools_id,
Username: username,
};
const result2 = await cognitoIdentityServiceProvider
.adminUpdateUserAttributes(params2)
.promise();
lg(`Signing In ${username} wit password ${password}...`);
const user = await Auth.signIn(username, password);
expect(user).not.toBeNull();
} catch (error) {
throw error;
}
Screenshots NA
What is Configured? If applicable, please provide what is configured for Amplify CLI:
- Which steps did you follow via Amplify CLI when configuring your resources.
Resources were configured in varios steps. This is the current
amplify status
output:
| Category | Resource name | Operation | Provider plugin |
| -------- | ------------------------------ | --------- | ----------------- |
| Auth | <myapp>Auth | No Change | awscloudformation |
| Auth | userPoolGroups | No Change | awscloudformation |
| Api | <myapp>api | No Change | awscloudformation |
| Api | AdminQueries | No Change | awscloudformation |
| Function | myappAuthPostConfirmation | No Change | awscloudformation |
| Function | myappAuthPreTokenGeneration | No Change | awscloudformation |
| Function | AdminQueriesxxxxxx | No Change | awscloudformation |
| Function | myappAuthPreSignup | No Change | awscloudformation |
- Which resources do you have configured?
aws-exports
file:
const awsmobile = {
"aws_project_region": "us-west-2",
"aws_cognito_identity_pool_id": "us-west-2:xxxxxxxxxxxxx",
"aws_cognito_region": "us-west-2",
"aws_user_pools_id": "us-west-2_xxxxxxxxx",
"aws_user_pools_web_client_id": "xxxxxxxxxxx",
"oauth": {},
"aws_appsync_graphqlEndpoint": "https://xxxxxxxxxxx.appsync-api.us-west-2.amazonaws.com/graphql",
"aws_appsync_region": "us-west-2",
"aws_appsync_authenticationType": "AMAZON_COGNITO_USER_POOLS",
"aws_cloud_logic_custom": [
{
"name": "AdminQueries",
"endpoint": "https://xxxxxxxxxx.us-west-2.amazonaws.com/develop",
"region": "us-west-2"
}
]
};
export default awsmobile;
Environment
npx envinfo --system --binaries --browsers --npmPackages --npmGlobalPackages
System:
OS: macOS 10.15.6
CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
Memory: 2.03 GB / 32.00 GB
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 12.16.3 - ~/n/bin/node
Yarn: 1.21.1 - ~/.yarn/bin/yarn
npm: 6.14.7 - ~/n/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
Browsers:
Brave Browser: 80.1.5.123
Chrome: 84.0.4147.105
Firefox: 72.0.2
Safari: 13.1.2
npmPackages:
@aws-amplify/api: ^3.1.9 => 3.1.21
@aws-amplify/pubsub: ^3.0.10 => 3.0.22
@babel/core: ^7.6.4 => 7.10.5
@carimus/metro-symlinked-deps: ^1.1.0 => 1.1.0
@react-native-community/datetimepicker: 2.2.2 => 2.2.2
@react-native-community/netinfo: 5.5.1 => 5.5.1
@types/jest: ^26.0.7 => 26.0.7
@types/node-fetch: ^2.5.7 => 2.5.7
@types/object-path: ^0.11.0 => 0.11.0
@types/react: ^16.9.11 => 16.9.43
@types/react-native: ^0.60.22 => 0.60.31
@types/react-native-calendars: ^1.20.7 => 1.20.8
@types/react-test-renderer: ^16.9.2 => 16.9.2
@types/validatorjs: ^3.15.0 => 3.15.0
aws-amplify: ^3.0.10 => 3.0.22
aws-amplify-react-native: ^4.1.1 => 4.2.3
aws-sdk: ^2.730.0 => 2.730.0
babel-loader: ^8.0.6 => 8.1.0
babel-plugin-module-resolver: ^4.0.0 => 4.0.0
babel-plugin-root-import: ^6.4.1 => 6.5.0
babel-plugin-transform-remove-console: ^6.9.4 => 6.9.4
babel-preset-expo: ^8.1.0 => 8.1.0
debug: ^4.1.1 => 4.1.1
docz: ^1.3.2 => 1.3.2
docz-theme-default: ^1.2.0 => 1.2.0
expo: ^37.0.0 => 37.0.12
expo-random: ^8.1.0 => 8.2.1
jest-expo: ^37.0.0 => 37.0.0
jsonwebtoken: ^8.5.1 => 8.5.1
lodash: ^4.17.19 => 4.17.19
mobx: ^4.14.1 => 4.15.4
mobx-react-lite: ^1.5.0 => 1.5.2
mobx-state-tree: ^3.15.0 => 3.17.2
nanoid: ^3.1.10 => 3.1.11
node-fetch: ^2.6.0 => 2.6.0
object-path: ^0.11.4 => 0.11.4
react: 16.9.0 => 16.9.0
react-art: 16.8.6 => 16.8.6
react-dom: 16.9.0 => 16.9.0
react-native: https://github.com/expo/react-native/archive/sdk-37.0.0.tar.gz => 0.61.4
react-native-calendars: ^1.265.0 => 1.300.0
react-native-keyboard-aware-scroll-view: ^0.9.1 => 0.9.1
react-native-keyboard-aware-scrollview: ^2.1.0 => 2.1.0
react-native-modal-datetime-picker: ^8.5.4 => 8.7.1
react-native-paper: ^3.9.0 => 3.11.0
react-native-picker-select: ^6.6.0 => 6.6.0
react-native-svg-transformer: ^0.14.3 => 0.14.3
react-native-web: ^0.11.7 => 0.11.7
react-test-renderer: ^16.13.1 => 16.13.1
shortid: ^2.2.15 => 2.2.15
ts-node: ^8.4.1 => 8.10.2
tsconfig-paths: ^3.9.0 => 3.9.0
typescript: ^3.8.3 => 3.9.7
uuid: ^3.3.3 => 3.4.0
validatorjs: ^3.17.1 => 3.18.1
npmGlobalPackages:
@aws-amplify/cli: 4.24.3
expo-cli: 3.22.3
juicy-rn: 0.2.7-b7
lz-string: 1.4.4
npm: 6.14.7
ts-node: 8.10.1
typescript: 3.8.3
uuid: 7.0.1
Additional context
- I even added a 5 second delay after calling confirmation and before calling signIn but I’m getting the same error.
- Another thing I tried was creating a
PreSignup
Trigger where I setautoVerifyEmail
andautoConfirmUser
to true So I didn’t need to executeadminConfirmSignUp
andadminUpdateUserAttributes
. The result was the same error. - Using the same user and pass works when I login from UI and also when I get a token via
https://cognito-idp.us-west-2.amazonaws.com/
You can turn on the debug mode to provide more info for us by setting window.LOG_LEVEL = ‘DEBUG’; in your app.
console.log node_modules/@aws-amplify/core/lib/Logger/ConsoleLogger.js:109
[DEBUG] 50:58.693 AuthClass - signUp attrs: [
{ Name: 'email', Value: 'xxxxx+0PiA8tckL@anemail.com' },
{ Name: 'name', Value: 'Test' },
{ Name: 'family_name', Value: 'Test' }
]
console.log node_modules/@aws-amplify/core/lib/Logger/ConsoleLogger.js:109
[DEBUG] 50:58.714 AuthClass - signUp validation data: null
console.log node_modules/@aws-amplify/core/lib/Logger/ConsoleLogger.js:109
[DEBUG] 51:00.377 Hub - Dispatching to auth with {
event: 'signUp',
data: {
user: CognitoUser {
username: 'xxxxx+0PiA8tckL@anemail.com',
pool: [CognitoUserPool],
Session: null,
client: [Client],
signInUserSession: null,
authenticationFlowType: 'USER_SRP_AUTH',
storage: [Function],
keyPrefix: 'CognitoIdentityServiceProvider.xxxxxxxxxxx',
userDataKey: 'CognitoIdentityServiceProvider.xxxxxxxxxxx.xxxxxx+0PiA8tckL@anemail.com.userData'
},
userConfirmed: false,
userSub: 'xxxxxx-xxxxxx-xxxxx-xxxx-xxxxxxxxxx',
codeDeliveryDetails: {
AttributeName: 'email',
DeliveryMedium: 'EMAIL',
Destination: 'x***@a***.com'
}
},
message: 'xxxxxxx+0PiA8tckL@anemail.com has signed up successfully'
}
console.log node_modules/@aws-amplify/core/lib/Logger/ConsoleLogger.js:109
[DEBUG] 51:15.302 AuthClass - signIn failure {
code: 'NotAuthorizedException',
name: 'NotAuthorizedException',
message: 'Incorrect username or password.'
}
console.log node_modules/@aws-amplify/core/lib/Logger/ConsoleLogger.js:109
[DEBUG] 51:15.303 Hub - Dispatching to auth with {
event: 'signIn_failure',
data: {
code: 'NotAuthorizedException',
name: 'NotAuthorizedException',
message: 'Incorrect username or password.'
},
message: 'xxxxxx-xxxxxx-xxxxx-xxxx-xxxxxxxxxx failed to signin'
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:9
Top GitHub Comments
I was having the same problem with Amplify but was able to get past this by adding
testEnvironment: 'node'
to my jest.config.js as suggesting in a related thread.I found out the problem is similar to aws/aws-encryption-sdk-javascript#126. The problem is with this code fragment in the
lib-typedarrays.js
the condition fails presumably due the value coming in from the jest test being generated in other nodejs vm instance or something like that.
TL;DR I created a custom testEnvironment file as commented by @wi-ski in https://github.com/facebook/jest/issues/7780#issuecomment-615890410 and added
testEnvironment: ".path/to/my/customenv.js",
to thejest.config.js
file.Here is the test env file I created.
With this tweak I can finally successfully sign in with the default
USER_SRP_AUTH
authenticationFlowType.PD: @khanson98 may be this can give you a hint for your problem.
PD2: Now I’m facing another one: calling a graphql operation with authMode=AWS_IAM returns this error when executing from Jest. No problem occurs when doing it from the Web/RN App UI.
I guess I’ll dig-in again and open a new issue if needed.