-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from two-factor/zep/verifypostgresql
added the postgresql logic
- Loading branch information
Showing
1 changed file
with
43 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,50 @@ | ||
module.exports = function(userID, phone) { | ||
module.exports = function(userID, code) { | ||
const { pgConnect } = this; | ||
return new Promise((resolve, reject) => { | ||
this.pgConnect() | ||
pgConnect() | ||
.then(({ database, done }) => { | ||
// pgClient.query... blah blah logic | ||
//invoke done before your resolve this promise | ||
done(); | ||
//Query to just check if the user was created | ||
const query = "SELECT * FROM twoauthusers WHERE userid=$1"; | ||
const values = [String(userID)]; | ||
|
||
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." | ||
) | ||
); | ||
|
||
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) => { | ||
//invoke done before you reject | ||
.catch(err => { | ||
done(); | ||
reject(err); | ||
reject(new Error("Could not find Database at Connection URI.")); | ||
}); | ||
}); | ||
//invoke done before you reject | ||
}; |