diff --git a/packages/auth/__tests__/auth-unit-test.ts b/packages/auth/__tests__/auth-unit-test.ts index a5bdf1c6665..f4888d325fc 100644 --- a/packages/auth/__tests__/auth-unit-test.ts +++ b/packages/auth/__tests__/auth-unit-test.ts @@ -204,6 +204,9 @@ jest.mock('amazon-cognito-identity-js/lib/CognitoUser', () => { CognitoUser.prototype.updateAttributes = (attributeList, callback) => { callback(null, 'SUCCESS'); }; + CognitoUser.prototype.deleteAttributes = (attributeList, callback) => { + callback(null, 'SUCCESS'); + }; CognitoUser.prototype.setAuthenticationFlowType = type => {}; @@ -2686,6 +2689,52 @@ describe('auth unit test', () => { }); }); + describe('deleteUserAttributes test', () => { + test('happy case', async () => { + const auth = new Auth(authOptions); + + const user = new CognitoUser({ + Username: 'username', + Pool: userPool, + }); + + const attributeNames = [ + 'email', 'phone_number' + ]; + + const spyon = jest + .spyOn(Auth.prototype, 'userSession') + .mockImplementationOnce(() => { + return new Promise((res) => { + res(session); + }); + }); + + expect.assertions(1); + expect(await auth.deleteUserAttributes(user, attributeNames)).toBe('SUCCESS'); + + spyon.mockClear(); + }); + + test('happy case to call with expected attributes', async () => { + const spyon = jest.spyOn(CognitoUser.prototype, 'deleteAttributes'); + const auth = new Auth(authOptionsWithClientMetadata); + const user = new CognitoUser({ + Username: 'username', + Pool: userPool, + }); + + await auth.deleteUserAttributes(user, ['email', 'phone_number']); + + expect(await CognitoUser.prototype.deleteAttributes).toBeCalledWith( + ['email', 'phone_number'], + jasmine.any(Function), + ); + spyon.mockClear(); + }); + + }); + describe('federatedSignIn test', () => { test('No Identity Pool and No User Pool', async () => { const options: AuthOptions = {}; diff --git a/packages/auth/src/Auth.ts b/packages/auth/src/Auth.ts index b7922a73127..766cb9f6d7a 100644 --- a/packages/auth/src/Auth.ts +++ b/packages/auth/src/Auth.ts @@ -1111,6 +1111,33 @@ export class AuthClass { }); } + /** + * Delete an authenticated users' attributes + * @param {CognitoUser} - The currently logged in user object + * @return {Promise} + **/ + public deleteUserAttributes( + user: CognitoUser | any, + attributeNames: string[], + ) { + const that = this; + return new Promise((resolve, reject) => { + that.userSession(user).then(session => { + user.deleteAttributes( + attributeNames, + (err, result) => { + if (err) { + return reject(err); + } else { + return resolve(result); + } + } + ); + }); + }); + + } + /** * Update an authenticated users' attributes * @param {CognitoUser} - The currently logged in user object