Created
September 18, 2019 20:36
-
-
Save philippefutureboy/2362ab4b659ce07d1d272854c3120842 to your computer and use it in GitHub Desktop.
Node.js script using AWS-Amplify to automatically force reset password for admin-generated cognito users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Example command: | |
./node_modules/.bin/cross-env \ | |
COGNITO_REGION=ca-central \ | |
COGNITO_USERPOOL_ID=userpool_id \ | |
COGNITO_CLIENT_ID=client_id \ | |
COGNITO_USER_USERNAME=username \ | |
COGNITO_USER_INITIAL_PASSWORD=pass \ | |
COGNITO_USER_NEW_PASSWORD=newpass \ | |
COGNITO_USER_EMAIL=email \ | |
node scripts/cognitoForceResetPassword.js | |
*/ | |
global.fetch = require('node-fetch'); | |
const Amplify = require('aws-amplify'); | |
Amplify.default.configure({ | |
region: process.env.ARTHUR_COGNITO_REGION, | |
userPoolId: process.env.ARTHUR_COGNITO_USERPOOL_ID, | |
userPoolWebClientId: process.env.ARTHUR_COGNITO_CLIENT_ID, | |
}); | |
const { Auth } = Amplify; | |
Auth.signIn( | |
process.env.ARTHUR_COGNITO_USER_USERNAME, | |
process.env.ARTHUR_COGNITO_USER_INITIAL_PASSWORD | |
) | |
.then((user) => { | |
if (user.challengeName === 'NEW_PASSWORD_REQUIRED') { | |
Auth.completeNewPassword( | |
user, // the Cognito User Object | |
process.env.ARTHUR_COGNITO_USER_NEW_PASSWORD, | |
// OPTIONAL, the required attributes | |
{ | |
email: process.env.ARTHUR_COGNITO_USER_EMAIL, | |
} | |
) | |
.then((authenticatedUser) => { | |
console.log(authenticatedUser); | |
}) | |
.catch((e) => { | |
console.log(e); | |
}); | |
} | |
}) | |
.catch((e) => { | |
console.log(e); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment