Skip to content

Commit

Permalink
feat(@aws-amplify/auth): delete user attributes (#7342)
Browse files Browse the repository at this point in the history
* feat: delete user attributes

* fix: remove un-accepted clientMetadata props

* test: add test od deleteUserAttributes function

* test: fix test code of deleteUserAttributes

* test: fix test code of deleteUserAttributes

Co-authored-by: Hidetaka Okamoto <info@wp-kyoto.net>
Co-authored-by: Sam Martinez <samlmar@amazon.com>
Co-authored-by: KJ(Kaijie) Huang <hkjpotato@gmail.com>
Co-authored-by: KJ(Kaijie) Huang <kaijih@amazon.com>
Co-authored-by: Francisco Rodriguez <frodriguez.cs@gmail.com>
  • Loading branch information
6 people authored Jul 23, 2021
1 parent b881933 commit 1b1df67
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
49 changes: 49 additions & 0 deletions packages/auth/__tests__/auth-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {};

Expand Down Expand Up @@ -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 = {};
Expand Down
27 changes: 27 additions & 0 deletions packages/auth/src/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1b1df67

Please sign in to comment.