I was Trying with NestJS and Cognito and Getting the below Error -
"message": "Username and Pool information are required."
The Controller I have Built is -
@Patch('changePassword')
async changePassword(@Body() user: ChangePasswordDto) {
try {
return await this.authService.changePassword(user);
} catch (e) {
throw new HttpException(e.message, e.status);
}
}
The service for the change Password is -
changePassword(user: ChangePasswordDto) {
const { username, oldPassword, newPassword } = user;
const userData = {
Username: username,
Pool: this.userPool,
};
const cognitoUser = new CognitoUser(userData);
cognitoUser.getSession((err: any, result: any) => {
if (result) {
cognitoUser.changePassword(
oldPassword,
newPassword,
(err: any, result: any) => {
if (result) {
return result;
} else {
throw new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR);
}
},
);
} else {
throw new HttpException(err, HttpStatus.UNAUTHORIZED);
}
});
}
The User pool has been initialized at constructor like -
this.userPool = new CognitoUserPool({
UserPoolId: this.authConfig.userPoolId,
ClientId: this.authConfig.clientId,
});
I am able to successfully login and getting some Response like this -
{
"idToken": {
"jwtToken": "eyJraWQiOiJKNkNGlheUBnbWFpbC5jb20ifQ.rhgZAC8SGTVVsM21V6ktWwf-2dMDksU9r8NOuWrPO7HwaQuVDmyXU2_mJOI53D3zP3CrpGXQWadQnv8gVmYtxTnLExEc4z1X2s9M9_XkSAlCEeD1uKNMfjvq7M6JPnxZqb1X6YoLFBmCUOVK_uR8VPpqfF45g39yLblHWxK83ouOtZaPeTmD1p_AEhTLahJF3aIOQ",
"payload": {
"sub": "2aed3574932f",
"email_verified": true,
"iss": "https://cognito-idp.us-east-1.amazonaws.com/xxxx",
"cognito:username": "xxxxxxxxrrrrrrr",
"origin_jti": "bc0b-8dbe8330d357",
"aud": "e3r13314134",
"event_id": "adcd-d97bbc385350",
"token_use": "id",
"auth_time": 1670939555,
"exp": 1670943155,
"iat": 1670939555,
"jti": "r3434414-a25a071ef805",
"email": "[email protected]"
}
},
"refreshToken": {
"token": "hzQQ3k6rQpOG4uHcQ9oplMiW3VeyiIHJzOelzN-PBclYpdS3QeaJ7jBJwcYMMJEHFMUHL30F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574932f",
"iss": "https://cognito-idp.us-east-1.amazonaws.com/xxxxxx",
"client_id": "qeqweqwe4123412422ldt0a1jl3d2k",
"origin_jti": "bc0b-8dbe8330d357",
"event_id": "adcd-d97bbc385350",
"token_use": "access",
"scope": "aws.cognito.signin.user.admin",
"auth_time": 1670939555,
"exp": 1670943155,
"iat": 1670939555,
"jti": "810f-0f8a2ce85d05",
"username": "xxxxxx"
}
},
"clockDrift": 0
}
Solved this By the Following Snippets -
Controller
@Post('changePassword')
@UsePipes(ValidationPipe)
async changePassword(
@Body() authChangePasswordUserDto: AuthChangePasswordUserDto,
) {
const result = await this.awsCognitoService.changeUserPassword(
authChangePasswordUserDto,
);
if (result == 'SUCCESS') {
return { status: 'Password Changed Successfully' };
}
}
Service
async changeUserPassword(
authChangePasswordUserDto: AuthChangePasswordUserDto,
) {
const { email, currentPassword, newPassword } = authChangePasswordUserDto;
const userData = {
Username: email,
Pool: this.userPool,
};
const authenticationDetails = new AuthenticationDetails({
Username: email,
Password: currentPassword,
});
const userCognito = new CognitoUser(userData);
return new Promise((resolve, reject) => {
userCognito.authenticateUser(authenticationDetails, {
onSuccess: () => {
userCognito.changePassword(
currentPassword,
newPassword,
(err, result) => {
if (err) {
reject(err);
return;
}
resolve(result);
},
);
},
onFailure: (err) => {
reject(err);
},
});
});
}
Also, Please make sure to make the changes in your User Pool in AWS to login with your email as username.