Skip to content

Commit

Permalink
feat: update account confirmation fn signature
Browse files Browse the repository at this point in the history
  • Loading branch information
kasongoyo committed Jul 17, 2019
1 parent a4032b9 commit 88bcede
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ mongoose.model('User', UserSchema)
* `Model.unregister(criteria)` - It unregister account
* `Model.authenticate(alias, password)`
* `Instance.changePassword(newPassword)`
* `Model.confirm(alias, confirmationToken)` - It calls account confirmation
* `Model.confirm(username, confirmationToken)` - It calls account confirmation, username can either be email or phone number
* `Instance.sendConfirmationInstructions` - It send out account confirmtion instructions.
* `Model.passwordReset(alias, newPassword, recoveryToken)` - It reset password
* `Instance.sendPasswordResetInstructions()` - It send out password reset instructions.
Expand Down
23 changes: 9 additions & 14 deletions lib/confirmable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const path = require('path');
const randomize = require('randomatic');
const createError = require('http-errors');
const deepmerge = require('deepmerge');
const validator = require('validator');
const Utils = require(path.join(__dirname, '..', 'utils'));

function Confirmable(schema, opts = {}) {
Expand Down Expand Up @@ -123,28 +124,22 @@ function Confirmable(schema, opts = {}) {
* @param {String} username - Email or phone number
* @param {String} confirmationToken - confirmation token sent during registration
*/
schema.statics.confirm = function (username, channel, confirmationToken) {
schema.statics.confirm = function (username, confirmationToken) {
//this refer to model static context
const Confirmable = this;
if (!username || !channel || !confirmationToken) {
if (!username || !confirmationToken) {
// confirmationToken or username is not specified
return Promise.reject(new createError(400, 'Invalid confirmation details'));
}
//TODO sanitize confirmationToken
let credential;
if (channel.toUpperCase() === 'PHONE') {
credential = { phoneNumber: username };
}
if (channel.toUpperCase() === 'EMAIL') {
const isEmail = validator.isEmail(username);
if (isEmail) {
credential = { email: username.toLowerCase() };
} else {
credential = { phoneNumber: username };
}
if (!credential) {
// The specified channel is not supported by jabali
// jabali so far support only email and phone number
return Promise.reject(new createError(400, 'Unrecognized channel'));
}
// const credentials = [{ email: username.toLowerCase() }, { phoneNumber: username }];
// const users = credentials.map(credential => );

return Confirmable
.findOne(credential)
.exec()
Expand All @@ -165,7 +160,7 @@ function Confirmable(schema, opts = {}) {
// token has expired
throw new createError(400, 'Token has expired')
}
if (channel.toUpperCase() === 'EMAIL') {
if (isEmail) {
confirmable.emailVerifiedAt = new Date();
} else {
confirmable.phoneVerifiedAt = new Date();
Expand Down
4 changes: 2 additions & 2 deletions lib/confirmable/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('Confirmable', function () {

it('should be able to confirm account through email', function (done) {
User
.confirm(email, 'email', token)
.confirm(email, token)
.then(confirmable => {
expect(confirmable.confirmedAt).to.not.be.null;
expect(confirmable.emailVerifiedAt).to.not.be.null;
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('Confirmable', function () {

it('should be able to confirm account through phone number', function (done) {
User
.confirm(phoneNumber, 'phone', token)
.confirm(phoneNumber, token)
.then(confirmable => {
expect(confirmable.confirmedAt).to.not.be.null;
expect(confirmable.phoneVerifiedAt).to.not.be.null;
Expand Down

0 comments on commit 88bcede

Please sign in to comment.