From fa40be6fdf3ee4a58022cc2c08744c4e681191a9 Mon Sep 17 00:00:00 2001 From: Giuseppe Zep Valentino Date: Mon, 8 Jul 2019 16:08:12 -0400 Subject: [PATCH] added verify for postgresql --- functions/databases/mongoose/verify.js | 3 +- functions/databases/postgres/verify.js | 56 +++++++++++++++----------- tests/create.js | 8 ++-- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/functions/databases/mongoose/verify.js b/functions/databases/mongoose/verify.js index a67bdbc..be1bcee 100644 --- a/functions/databases/mongoose/verify.js +++ b/functions/databases/mongoose/verify.js @@ -19,8 +19,7 @@ function verify(userID, code) { ) ); - return this.client.verify - .services(sid) + return this.client.verify.services(sid) .verificationChecks.create({ to: phone, code diff --git a/functions/databases/postgres/verify.js b/functions/databases/postgres/verify.js index e73e038..0e4ef88 100644 --- a/functions/databases/postgres/verify.js +++ b/functions/databases/postgres/verify.js @@ -1,35 +1,45 @@ module.exports = function(userID, code) { - const { client, pgConnect } = this; + const { pgConnect } = this; return new Promise((resolve, reject) => { pgConnect() .then(({ database, done }) => { //Query to just check if the user was created - const query = "SELECT userid FROM twoauthusers WHERE userid=$1"; + const query = "SELECT * FROM twoauthusers WHERE userid=$1"; const values = [String(userID)]; - database.query(query, values).then(res => { - const { sid, phone } = res.rows[0]; + database + .query(query, values) + .then(res => { + const { sid, phone } = res.rows[0]; - 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." - ) - ); + 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 - }) - .then(verification => { - if (verification.status === "approved") resolve(true); - resolve(false); - }); - }); + return this.client.verify + .services(sid) + .verificationChecks.create({ + to: phone, + code + }) + .then(verification => { + if (verification.status === "approved") resolve(true); + resolve(false); + }) + .catch(err => { + done(); + resolve(false) + }); + }) + .catch(err => { + done(); + reject(new Error("Could not find Database at Connection URI.")); + }); }) .catch(err => { done(); diff --git a/tests/create.js b/tests/create.js index a2edf16..4df7cd4 100644 --- a/tests/create.js +++ b/tests/create.js @@ -1,10 +1,12 @@ const client = require("../index")(process.env.SID, process.env.AUTH, { isPostgres: true, - connectionURI: "postgres://student:ilovetesting@localhost/twoauthtests" + appName:'DATABASE two-factor', + connectionURI: "postgres://zepvalue:linux@localhost/twoauthtests" }); -client.create("ian", "+17604207520"); - +//client.create("ian", "+17604207520"); +//client.send("ian") +client.verify("ian", '851701') // console.log("client is", Client); // Client.create("ian", "17604207520")