Currently, I am working on setting up a forgot-password route for my typescript project with AWS Cognito. The code below shows my initial implementation, but I encountered an error message
Contact administrator to reset password.
Is there a way to create a route where confirmed users can directly view or reset their passwords?
import AmazonCognitoIdentity from 'amazon-cognito-identity-js';
const UserPoolId = ************;
const ClientId = ************;
const region = ************
const config = {region: region }
export const resetPassword = async (req, res) => {
try {
const { username } = req.body;
const poolData = {
UserPoolId: ************,
ClientId: ************,
};
const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
const userData = {
Username: username,
Pool: userPool,
};
const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.forgotPassword({
onSuccess: (data) => {
console.log(data)
res.status(200).json({ message: "Password Reset"});
},
onFailure: (err) => {
console.log('ERR:', err)
res.status(401).json({ message: "Password Not Reset", error: err.message});
},
})
}
catch (err) {
console.log("FAILED")
console.log("Error"+ err.message)
res.status(500).json({ message: "Password Not Reset", error: err.message});
}
};
I am exploring the possibility of allowing users to either directly reset their passwords through this route or receive a confirmation email with a code for resetting their password. Does AWS Cognito User Pools support this flow?
Your help and guidance would be greatly appreciated!