-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbService.js
174 lines (132 loc) · 3.91 KB
/
dbService.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
var MongoClient = require('mongodb').MongoClient;
const bcrypt = require('bcryptjs');
const url = 'mongodb://localhost:27017/';
let instance = null;
var usersCol;
var sessionsCol;
var db;
try {
MongoClient
.connect(url, {
useUnifiedTopology: true
})
.then(client => {
console.log('Connected to Database')
db = client.db('app-prototype')
usersCol = db.collection('users')
sessionsCol = db.collection('sessions')
});
} catch (error){
console.log("DB Connect Error:", error);
}
class DbService {
static getDbServiceInstance(){
return instance ? instance : new DbService();
}
async registerUser(email, passwordHash){
try {
//check if user exists
var check = await usersCol.findOne({"email": email})
//create new user
if (check === null){
var user = {
"email" : email,
"password": passwordHash
}
await usersCol.insertOne(user)
return null;
//if user already exists
} else {
return "User already exists"
}
} catch (error) {
console.log(error);
return error;
}
}
async registerSession(email, sessionToken){
try{
//check if session exists
var check = await sessionsCol.findOne({"email": email})
//create new session if first session
if (check === null){
var session = {
"email": email,
"token": sessionToken
}
await sessionsCol.insertOne(session)
//otherwise, update session
} else {
var newItem = {
"token": sessionToken
}
await sessionsCol.updateOne({"email": email}, { $set: newItem })
}
} catch (error) {
console.log(error);
return
}
}
async findSession(sessionToken){
try{
//check if session exists
var session = await sessionsCol.findOne({"token": sessionToken})
//create new session if first session
if (session === null){
return null
} else {
return session
}
} catch (error) {
console.log(error);
return
}
}
async signIn(email, enteredPassword){
try {
//pull user from database
var user = await usersCol.findOne({"email": email})
if (user){
var password = user.password
console.log(user)
console.log(user.password)
const passwordMatches = await bcrypt.compare(enteredPassword, password);
if (passwordMatches){
return null
} else{
return "Incorrect password"
}
} else{
return "Username not found"
}
} catch (error) {
console.log(error);
return error;
}
}
async addMap(email, lat, long, bbox, points){
try{
var newItem = {
"latitude": lat,
"longitude": long,
"bbox": bbox,
"points": points
}
await usersCol.updateOne({"email": email}, { $set: newItem });
} catch (error){
console.log(error)
return error
}
}
async getUser(email){
try{
var user = await usersCol.findOne({"email": email})
return user
}
catch (error){
console.log(error)
return error
}
}
}
module.exports = DbService;