Skip to content

Commit

Permalink
Merge pull request #31 from two-factor/dnaganog
Browse files Browse the repository at this point in the history
added appName support to all create functions; started the postgres c…
  • Loading branch information
Dnaganog committed Jul 8, 2019
2 parents 510f720 + dc7f2e4 commit 497c5c0
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 54 deletions.
12 changes: 0 additions & 12 deletions App.js

This file was deleted.

15 changes: 15 additions & 0 deletions __tests__/functions/databases/postgresql/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// const client = require('../../../../index')(process.env.SID, process.env.AUTH, {
// isPostgres: true,
// appName: 'testApp',
// connectionURI: 'postgres://postgres:yellowjacket@localhost/twoauthtest',
// });

// // client.create("ian", "+17604207520");

// client
// .create('ian', '+12016750593')
// .then(res => console.log(res))
// .catch(err => console.log(err));
// // describe("Tests for Postgres Send", () => {

// // });
17 changes: 8 additions & 9 deletions functions/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@
// returns a reference to that object
// if rejected, throws error from verify API
function create(userID, phone) {
const client = this.client;
const users = this.users;
const { client, users, appName } = this;

return new Promise((resolve, reject) => {
if (typeof phone !== "string") {
reject(new Error("typeof phone must be string"));
if (typeof phone !== 'string') {
reject(new Error('typeof phone must be string'));
}
if (phone.substring(0, 2) !== "+1") {
reject(new Error("phone must be string formatted as such: +1XXXXXXXXXX"));
if (phone.substring(0, 2) !== '+1') {
reject(new Error('phone must be string formatted as such: +1XXXXXXXXXX'));
}
client.verify.services
.create({ friendlyName: `Service for ${userID}` })
.then(service => {
.create({ friendlyName: `${appName}` })
.then((service) => {
const { sid } = service;
users[userID] = {
userID,
sid,
phone
phone,
};
resolve(users[userID]);
})
Expand Down
22 changes: 10 additions & 12 deletions functions/databases/mongoose/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,28 @@
// returns a reference to that object
// if rejected, throws error from verify API
function create(userID, phone) {
const client = this.client;
const users = this.users;
const TwoAuthUser = this.TwoAuthUser;
const { client, TwoAuthUser, appName } = this;

return new Promise((resolve, reject) => {
if (typeof phone !== "string") {
reject(new Error("typeof phone must be string"));
if (typeof phone !== 'string') {
reject(new Error('typeof phone must be string'));
}
if (phone.substring(0, 2) !== "+1") {
reject(new Error("phone must be string formatted as such: +1XXXXXXXXXX"));
if (phone.substring(0, 2) !== '+1') {
reject(new Error('phone must be string formatted as such: +1XXXXXXXXXX'));
}
client.verify.services
.create({ friendlyName: `Service for ${userID}` })
.then(service => {
.create({ friendlyName: `${appName}` })
.then((service) => {
const { sid } = service;
TwoAuthUser.create({
userID,
sid,
phone
phone,
})
.then(user => {
.then((user) => {
resolve(user);
})
.catch(err => {
.catch((err) => {
reject(err);
});
})
Expand Down
38 changes: 29 additions & 9 deletions functions/databases/postgres/create.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
module.exports = function(userID, phone) {
/* eslint-disable func-names */
// eslint-disable-next-line func-names

module.exports = function (userID, phone) {
const { client, appName } = this;
return new Promise((resolve, reject) => {
this.pgConnect()
.then(({ database, done }) => {
// pgClient.query... blah blah logic
//invoke done before your resolve this promise
done();
})
.catch((err, done) => {
//invoke done before you reject
done();
reject(err);
if (typeof phone !== 'string') {
reject(new Error('typeof phone must be string'));
}
if (phone.substring(0, 2) !== '+1') {
reject(new Error('phone must be string formatted as such: +1XXXXXXXXXX'));
}
client.verify.services
.create({ friendlyName: `${appName}` })
.then((service) => {
const { sid } = service;
database.query('INSERT INTO twoauthusers(userID, sid, phone) VALUES($1, $2, $3) RETURNING *', [userID, sid, phone])
.then((user) => {
done();
resolve(user);
})
.catch((err) => {
done();
reject(err);
});
})
.catch((err) => {
done();
reject(err);
});
});
});
};
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Client {
if (!options.hasOwnProperty("appName")) {
options.appName = "";
}
this.appName = options.appName;
this.AccSID = AccSID;
this.AuthToken = AuthToken;
this.client = twilio(this.AccSID, this.AuthToken);
Expand Down
12 changes: 0 additions & 12 deletions tests/create.js

This file was deleted.

0 comments on commit 497c5c0

Please sign in to comment.