Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: delete user attributes #7342

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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