feat: 🎸 implement API for change password

This commit is contained in:
Randall Morey committed 2020-09-16 14:56:52 -04:00
1 parent 33448de090
commit 41a5b7c33c
3 files changed
+72

No files matched your search

+24
View File
@@ -31,4 +31,28 @@ export default class AccountModel extends GeneratedAccountModel {
},
});
}
/**
* Update account password via the `change-password` method.
* See serializer and adapter for more information.
* @param {string} currentPassword
* @param {string} newPassword
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
changePassword(currentPassword, newPassword, options = { adapterOptions: {} }) {
const defaultAdapterOptions = {
method: 'change-password',
currentPassword,
newPassword,
};
return this.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
...options.adapterOptions,
},
});
}
}
+20
View File
@@ -20,6 +20,11 @@ export default class AccountSerializer extends ApplicationSerializer {
if (snapshot?.adapterOptions?.method === 'set-password') {
serialized = this.serializeForSetPassword(snapshot, password);
}
if (snapshot?.adapterOptions?.method === 'change-password') {
const { currentPassword, newPassword } = snapshot?.adapterOptions;
serialized =
this.serializeForChangePassword(snapshot, currentPassword, newPassword);
}
return serialized;
}
@@ -35,4 +40,19 @@ export default class AccountSerializer extends ApplicationSerializer {
password,
};
}
/**
* Returns a payload containing current and new passwords.
* @param {Snapshot} snapshot
* @param {string} current_password
* @param {string} new_password
* @return {object}
*/
serializeForChangePassword(snapshot, current_password, new_password) {
return {
version: snapshot.attr('version'),
current_password,
new_password,
};
}
}
@@ -94,4 +94,32 @@ module('Unit | Serializer | account', function (hooks) {
version: 1,
});
});
test('it serializes a custom payload when `adapterOptions.method` is set to `change-password`', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('account');
const record = store.createRecord('account', {
type: 'password',
name: 'Name',
description: 'Description',
attributes: {
login_name: 'Login Name',
},
auth_method_id: '1',
version: 1,
});
const snapshot = record._createSnapshot();
snapshot.adapterOptions = {
method: 'change-password',
currentPassword: 'Current',
newPassword: 'New',
};
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
current_password: 'Current',
new_password: 'New',
version: 1,
});
});
});