-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.js
More file actions
103 lines (80 loc) · 2.55 KB
/
model.js
File metadata and controls
103 lines (80 loc) · 2.55 KB
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
// model.js DEALS WITH THE DATATBASE
const auth = require("./assets/auth.js");
const mongoose = require("mongoose");
const md5 = require("md5");
let authenticated = -1;
const options = {
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true
};
mongoose.connect(auth.getDBURL(), options, (error) => {
if (error) {
console.log("Something happened at MongoDB Headquarters: " + error.reason);
} else {
console.log("Connected to MongoDB Atlas!");
}
});
let db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB Connection Error: "));
mongoose.Promise = global.Promise;
let Schema = mongoose.Schema;
let accountSchema = new Schema({
fname: String,
lname: String,
username: String,
email: String,
password: String,
creationDate: Date,
lastLogin: Date,
projectID: String
});
let accountModel = new mongoose.model("accounts", accountSchema);
// async function searchDB(searchCriteria) {
// return new Promise((resolve, reject) => {
// accountModel.find(searchCriteria, (error, results) => {});
// });
// }
async function checkLogin(username, password) {
let hashedAndSaltedPassword = md5(password + auth.getSalt());
console.log(hashedAndSaltedPassword);
let searchCriteria = {
username: username,
password: hashedAndSaltedPassword
};
//shorter way to do the commented out code below.. makes into promise... // exec makes it to a promise
return accountModel.find(searchCriteria).exec();
//converts into a Promise "exec" mongoose exec
// let result = accountModel.find(searchCriteria, (error, results) => {
// if (error) {console.log(error.reason);}
//}).exec();
// return result;
}
//function updateAuthentication(value) {authenticated = value;}
async function createAccount(newAccount) {
let returnValue = null;
checklogin(newAccount.username, newAccount.password).then((results) => {
if (results.length >= 1) {
return null;
} else {
let account = new accountModel({
fname: newAccount.fname,
lname: newAccount.lname,
username: newAccount.username,
email: newAccount.email,
password: md5(newAccount.password + auth.getSalt()),
creationDate: new Date(),
lastLogin: new Date(),
projectID: Math.floor( (Math.random() * 100000) + 1)
});
//FIX
let temp = account.save()
console.log(temp);
return temp;
}
});
}
module.exports = {
checkLogin: checkLogin,
createAccount: createAccount
}