-
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 #23 from two-factor/mongo
added first iteration of mongoose functionality to lib
- Loading branch information
Showing
14 changed files
with
364 additions
and
94 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 +1,2 @@ | ||
node_modules | ||
node_modules | ||
.env |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -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)); |
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 |
---|---|---|
@@ -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.
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
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Takes in a userID and phone number associated with that user | ||
// returns a promise | ||
// if resolved, adds a user Object with the userId, sid, and phone associated with the service for that user... | ||
// 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 TwoFactorUser = this.TwoFactorUser; | ||
|
||
return new Promise((resolve, reject) => { | ||
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: `Service for ${userID}` }) | ||
.then(service => { | ||
const { sid } = service; | ||
TwoFactorUser.create({ | ||
userID, | ||
sid, | ||
phone | ||
}) | ||
.then(user => { | ||
resolve(user); | ||
}) | ||
.catch(err => { | ||
reject(err); | ||
}); | ||
}) | ||
.catch(err => reject(new Error(String(err)))); | ||
}); | ||
} | ||
|
||
module.exports = create; |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// 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(userID) { | ||
const users = this.users; | ||
const client = this.client; | ||
const TwoFactorUser = this.TwoFactorUser; | ||
return new Promise((resolve, reject) => { | ||
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." | ||
) | ||
); | ||
|
||
client.verify | ||
.services(sid) | ||
.verifications.create({ | ||
to: phone, | ||
channel: "sms" | ||
}) | ||
.then(verification => { | ||
resolve(verification); | ||
}) | ||
.catch(err => reject(err)); | ||
}) | ||
.catch(err => { | ||
reject(err); | ||
//"userID Error: This userID has not been created yet." | ||
}); | ||
}); | ||
} | ||
|
||
module.exports = send; |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const { Schema } = require("mongoose"); | ||
|
||
const userSchema = new Schema({ | ||
userID: String, | ||
sid: String, | ||
phone: String | ||
}); | ||
|
||
module.exports = userSchema; |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// takes in a username and code | ||
// checks whether that code is valid for that username | ||
// returns a promise | ||
// 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(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 | ||
}) | ||
.then(verification => { | ||
if (verification.status === "approved") resolve(true); | ||
return reject(false); | ||
}); | ||
}) | ||
.catch(err => { | ||
reject(false); | ||
}); | ||
}); | ||
} | ||
|
||
module.exports = verify; |
Oops, something went wrong.