Skip to content

Commit

Permalink
added send and verify mongoose functions, first iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
iangeckeler committed Jul 6, 2019
1 parent b790606 commit 0bd996a
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 54 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.env
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 0 additions & 9 deletions __tests__/mongoose.js

This file was deleted.

13 changes: 13 additions & 0 deletions __tests__/mongoose/mongoose-create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require("dotenv").config();

// put tests here

// const client = require("../index")(
// process.env.SID,
// process.env.AUTH,
// "mongodb://localhost/twofactortest"
// );
// client
// .create("ian", "+17604207520")
// .then(user => console.log(user))
// .catch(err => console.log(err));
20 changes: 20 additions & 0 deletions __tests__/mongoose/mongoose-send.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require("dotenv").config();

// put tests here

// const client = require("../index")(
// process.env.SID,
// process.env.AUTH,
// "mongodb://localhost/twofactortest"
// );
// client
// .create("ian", "+17604207520")
// .then(user => {
// client.send("ian");
// })
// .catch(err => console.log(err));

// client
// .verify("ian", "415408")
// .then(res => console.log(res))
// .catch(err => console.log(err));
Empty file.
50 changes: 27 additions & 23 deletions mongoose/send.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
// send takes in a username
// send takes in a userID
// it searches through users object to find sid and phone number
// then uses twilio api to send text message
// returns a promise
function send(username) {
function send(userID) {
const users = this.users;
const client = this.client;
const TwoFactorUser = this.TwoFactorUser;
return new Promise((resolve, reject) => {
if (!users[username])
reject(
new Error("Username Error: This username has not been created yet.")
);
TwoFactorUser.findOne({ userID: userID })
.then(user => {
const { sid, phone } = user;
if (!sid) reject(new Error("SID Error: No SID exists for this user."));
if (!phone)
reject(
new Error(
"Phone Number Error: No phone number exists for this user."
)
);

const { sid, phone } = users[username];

if (!sid) reject(new Error("SID Error: No SID exists for this user."));
if (!phone)
reject(
new Error("Phone Number Error: No phone number exists for this user.")
);

client.verify
.services(sid)
.verifications.create({
to: phone,
channel: "sms"
})
.then(verification => {
resolve(verification);
client.verify
.services(sid)
.verifications.create({
to: phone,
channel: "sms"
})
.then(verification => {
resolve(verification);
})
.catch(err => reject(err));
})
.catch(err => reject(err));
.catch(err => {
reject(err);
//"userID Error: This userID has not been created yet."
});
});
}

Expand Down
46 changes: 25 additions & 21 deletions mongoose/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,34 @@
// if there is a formatting error, promise rejects
// if there is no error, resolves to be the status of the verification
// status is true if verification succeeded, false if verification failed
function verify(username, code) {
return new Promise((resolve, reject) => {
if (!this.users[username])
reject(
new Error("Username Error: This username has not been created yet.")
);

const { sid, phone } = this.users[username];

if (!sid) reject(new Error("SID Error: No SID exists for this user."));
if (!phone)
reject(
new Error("Phone Number Error: No phone number exists for this user.")
);
function verify(userID, code) {
const TwoFactorUser = this.TwoFactorUser;
return new Promise((resolve, reject) => {
TwoFactorUser.findOne({ userID })
.then(user => {
const { sid, phone } = user;
if (!sid) reject(new Error("SID Error: No SID exists for this user."));
if (!phone)
reject(
new Error(
"Phone Number Error: No phone number exists for this user."
)
);

return this.client.verify
.services(sid)
.verificationChecks.create({
to: phone,
code
return this.client.verify
.services(sid)
.verificationChecks.create({
to: phone,
code
})
.then(verification => {
if (verification.status === "approved") resolve(true);
return reject(false);
});
})
.then(verification => {
if (verification.status === "approved") resolve(true);
return resolve(false);
.catch(err => {
reject(false);
});
});
}
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"twilio": "^3.33.0"
},
"devDependencies": {
"dotenv": "^8.0.0",
"jest": "^24.8.0"
}
}

0 comments on commit 0bd996a

Please sign in to comment.