Skip to content

Commit 9c4eef5

Browse files
committed
resend validation email
1 parent 4e2e2c5 commit 9c4eef5

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

plugins/emailstore.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,39 @@
722722
});
723723
};
724724

725+
emailPlugin._parseSecret = function (value) {
726+
var obj = null;
727+
try {
728+
obj = JSON.parse(value);
729+
} catch (e) {}
730+
731+
if (obj && _.isObject(obj)) {
732+
return obj.secret;
733+
}
734+
735+
return value;
736+
};
737+
738+
emailPlugin.resendEmail = function(request, response) {
739+
emailPlugin.authorizeRequestWithoutKey(request, function(err, email) {
740+
if (err) {
741+
return emailPlugin.returnError(err, response);
742+
}
743+
emailPlugin.db.get(pendingKey(email), function(err, value) {
744+
if (err) {
745+
logger.error('error retrieving secret for email', email, err);
746+
return emailPlugin.returnError(err, response);
747+
}
748+
749+
var secret = emailPlugin._parseSecret(value);
750+
751+
emailPlugin.sendVerificationEmail(email, secret);
752+
return response.json({
753+
success: true
754+
}).end();
755+
});
756+
});
757+
};
725758

726759
/**
727760
* Marks an email as validated
@@ -751,14 +784,7 @@
751784
}, response);
752785
}
753786

754-
var parsed = null;
755-
try {
756-
parsed = JSON.parse(value);
757-
} catch (e) {}
758-
759-
if (parsed && _.isObject(parsed)) {
760-
value = parsed.secret;
761-
}
787+
value = emailPlugin._parseSecret(value);
762788

763789
if (value !== secret) {
764790
return emailPlugin.returnError(emailPlugin.errors.INVALID_CODE, response);

test/test.EmailStore.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,25 @@ describe('emailstore test', function() {
406406
});
407407
});
408408

409+
describe.only('resend validation email', function () {
410+
var email = 'fake@email.com';
411+
var secret = '123';
412+
beforeEach(function() {
413+
request.param.onFirstCall().returns(email);
414+
response.json.returnsThis();
415+
response.redirect = sinon.stub();
416+
});
417+
418+
it('should resend validation email when pending', function () {
419+
plugin.authorizeRequestWithoutKey = sinon.stub().callsArgWith(1, null, email);
420+
leveldb_stub.get.onFirstCall().callsArgWith(1, null, JSON.stringify({ secret: secret, created: new Date() }));
421+
plugin.sendVerificationEmail = sinon.spy();
422+
plugin.resendEmail(request, response);
423+
plugin.sendVerificationEmail.calledOnce.should.be.true;
424+
plugin.sendVerificationEmail.calledWith(email, secret).should.be.true;
425+
});
426+
});
427+
409428
describe('removing items', function() {
410429
var fakeEmail = 'fake@email.com';
411430
var fakeKey = 'nameForData';

0 commit comments

Comments
 (0)