-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
112 lines (104 loc) · 3.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const twilio = require("twilio");
const mongoose = require("mongoose");
const create = require("./functions/create.js");
const send = require("./functions/send.js");
const verify = require("./functions/verify.js");
// import mongoose functions
const mongooseCreate = require("./functions/databases/mongoose/create");
const mongooseSend = require("./functions/databases/mongoose/send");
const mongooseVerify = require("./functions/databases/mongoose/verify");
const userSchema = require("./functions/databases/mongoose/userSchema");
//import postgres functions
const generatePool = require("./functions/databases/postgres/configure");
const createTable = require("./functions/databases/postgres/createtable");
const postgresCreate = require("./functions/databases/postgres/create");
const postgresSend = require("./functions/databases/postgres/send");
const postgresVerify = require("./functions/databases/postgres/verify");
const connect = (
AccSID,
AuthToken,
options = {
appName: "",
connectionURI: null,
isPostgres: false
}
) => {
if (typeof options === "string")
throw new Error(
"Options config must be an object, as specified in the documentation."
);
if (!options.hasOwnProperty("isPostgres")) {
options.isPostgres = false;
}
if (!options.hasOwnProperty("appName")) {
options.appName = "";
}
return new Client(AccSID, AuthToken, options);
};
// EXAMPLE CONFIG OBJECT
// options = {
// appName: "",
// connectionURI: null,
// isPostgres: null
// }
class Client {
constructor(AccSID, AuthToken, options) {
this.appName = options.appName;
this.AccSID = AccSID;
this.AuthToken = AuthToken;
this.client = twilio(this.AccSID, this.AuthToken);
this.users = {};
if (options.connectionURI !== null && options.isPostgres === false) {
mongoose
.connect(options.connectionURI)
.then(db => {
console.log("Two Factor successfully connected to Mongo");
})
.catch(err => {
throw new Error("Two Factor Unable to connect to Mongo");
});
this.TwoAuthUser = mongoose.model("two-auth-user", userSchema);
this.create = mongooseCreate;
this.send = mongooseSend;
this.verify = mongooseVerify;
} else if (options.connectionURI !== null && options.isPostgres === true) {
// connect the database and assign a reference to it to our client object
const pgPool = generatePool(options.connectionURI);
let tableCreated = false;
this.pgConnect = function() {
return new Promise((resolve, reject) => {
// connection using created pool
pgPool.connect(function(err, database, done) {
if (err) reject(new Error("Error connecting to Postgres Pool."));
if (!database) {
throw new Error("Could not find Database at Connection URI.");
}
// if table not created yet, create it and modify closure to reflect
if (tableCreated === false) {
database
.query(createTable)
.then(res => {
resolve({ database, done });
tableCreated = true;
})
.catch(e =>
reject(new Error("Error connecting to Postgres Pool."))
);
} else {
resolve({ database, done });
}
});
});
};
// assign the postgres functions to our client object
this.create = postgresCreate;
this.send = postgresSend;
this.verify = postgresVerify;
} else {
this.create = create;
this.send = send;
this.verify = verify;
}
}
}
module.exports = connect;