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

Fix for #55 #84

Merged
merged 1 commit into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
48 changes: 28 additions & 20 deletions src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint no-param-reassign: 0 */

const errors = require('feathers-errors');
const { checkContext } = require('feathers-hooks-common');
const { checkContext, getItems, replaceItems } = require('feathers-hooks-common');
const { getLongToken, getShortToken, ensureFieldHasChanged } = require('./helpers');

module.exports.addVerification = path => hook => {
Expand Down Expand Up @@ -49,25 +49,33 @@ module.exports.isVerified = () => hook => {

module.exports.removeVerification = ifReturnTokens => hook => {
checkContext(hook, 'after');
const user = hook.result || {};

if (!('isVerified' in user) && hook.method === 'create') {
/* eslint-disable no-console */
console.warn('Property isVerified not found in user properties. (removeVerification)');
console.warn('Have you added authManagement\'s properties to your model? (Refer to README.md)');
console.warn('Have you added the addVerification hook on users::create?');
/* eslint-enable */
}
// Retrieve the items from the hook
let users = getItems(hook)
if (!users) return
const isArray = Array.isArray(users)
users = (isArray ? users : [users])

users.forEach(user => {
if (!('isVerified' in user) && hook.method === 'create') {
/* eslint-disable no-console */
console.warn('Property isVerified not found in user properties. (removeVerification)');
console.warn('Have you added authManagement\'s properties to your model? (Refer to README.md)');
console.warn('Have you added the addVerification hook on users::create?');
/* eslint-enable */
}

if (hook.params.provider && user) { // noop if initiated by server
delete user.verifyExpires;
delete user.resetExpires;
delete user.verifyChanges;
if (!ifReturnTokens) {
delete user.verifyToken;
delete user.verifyShortToken;
delete user.resetToken;
delete user.resetShortToken;
if (hook.params.provider && user) { // noop if initiated by server
delete user.verifyExpires;
delete user.resetExpires;
delete user.verifyChanges;
if (!ifReturnTokens) {
delete user.verifyToken;
delete user.verifyShortToken;
delete user.resetToken;
delete user.resetShortToken;
}
}
}
})
// Replace the items within the hook
replaceItems(hook, isArray ? users : users[0])
};
19 changes: 17 additions & 2 deletions test/removeVerification.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('hook:remove verification', () => {
assert.notProperty(user, 'verifyChanges');
});

it('works with unverified used', () => {
it('works with unverified user', () => {
hookIn.result.isVerified = false;

assert.doesNotThrow(() => { hooks.removeVerification()(hookIn); });
Expand Down Expand Up @@ -74,7 +74,22 @@ describe('hook:remove verification', () => {
assert.property(user, 'verifyChanges');
});

it('throws with damaged hook', () => {
it('works with multiple verified user', () => {
hookIn.result = [hookIn.result, hookIn.result]
assert.doesNotThrow(() => { hooks.removeVerification()(hookIn); });

hookIn.result.forEach(user => {
assert.property(user, 'isVerified');
assert.equal(user.isVerified, true);
assert.notProperty(user, 'verifyToken');
assert.notProperty(user, 'verifyExpires');
assert.notProperty(user, 'resetToken');
assert.notProperty(user, 'resetExpires');
assert.notProperty(user, 'verifyChanges');
})
});

it('does not throw with damaged hook', () => {
delete hookIn.result;

assert.doesNotThrow(() => { hooks.removeVerification()(hookIn); });
Expand Down