-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
251 lines (212 loc) · 7.21 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
require('dotenv').config();
const express = require('express')
const { MongoClient } = require('mongodb')
const { v4: uuidv4 } = require('uuid')
const jwt = require('jsonwebtoken')
const cors = require('cors')
const bcrypt = require('bcrypt')
// const uri = process.env.URI
// console.log(uri);
// const uri = 'mongodb+srv://suryatomar303:[email protected]/?retryWrites=true&w=majority';
// const PORT = process.env.PORT || 8000;
const app = express()
// app.use(cors({
// origin: ['https://tindercopied.netlify.app/', 'http://localhost:3000']
// }))
// app.use(express.json())
app.use(cors());
app.use(bodyParser.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static('public'));
// Default
app.get('/', (req, res) => {
res.json('Hello to my app')
})
app.post('/signup', async (req, res) => {
const client = new MongoClient(process.env.uri);
const { email, password } = req.body;
//creating user by adding hash password and uuid
const generatedUserId = uuidv4();
const hashedPassword = await bcrypt.hash(password, 10);
try {
await client.connect()
const database = client.db('app-data');
const users = database.collection('users');
const existingUser = await users.findOne({ email });
if (existingUser) {
return res.status(409).send('User already exists, Please Login');
}
const sanitizedEmail = email.toLowerCase();
const data = {
user_id: generatedUserId,
email: sanitizedEmail,
hashed_password: hashedPassword
}
const insertedUser = await users.insertOne(data);
const token = jwt.sign(insertedUser, sanitizedEmail, {
expiresIn: 60 * 24
});
// res.status(201).json({ token, user_id: generatedUserId });
res.status(201).json({ token, user_id: generatedUserId, email: sanitizedEmail });
}
catch (error) {
console.log(error);
}
})
// Log in to the Database
app.post('/login', async (req, res) => {
const client = new MongoClient(process.env.uri)
const { email, password } = req.body
try {
await client.connect()
const database = client.db('app-data')
const users = database.collection('users')
const user = await users.findOne({ email })
const correctPassword = await bcrypt.compare(password, user.hashed_password)
if (user && correctPassword) {
const token = jwt.sign(user, email, {
expiresIn: 60 * 24
})
res.status(201).json({ token, userId: user.user_id })
}
res.status(400).json('Invalid Credentials')
} catch (err) {
console.log(err)
} finally {
await client.close()
}
})
// Get individual user
app.get('/user', async (req, res) => {
const client = new MongoClient(process.env.uri)
const userId = req.query.userId
try {
await client.connect()
const database = client.db('app-data')
const users = database.collection('users')
const query = { user_id: userId }
const user = await users.findOne(query)
res.send(user)
} finally {
await client.close()
}
})
// Update User with a match
app.put('/addmatch', async (req, res) => {
const client = new MongoClient(process.env.uri)
const { userId, matchedUserId } = req.body
try {
await client.connect()
const database = client.db('app-data')
const users = database.collection('users')
const query = { user_id: userId }
const updateDocument = {
$push: { matches: { user_id: matchedUserId } }
}
const user = await users.updateOne(query, updateDocument)
res.send(user)
} finally {
await client.close()
}
})
// Get all Users by userIds in the Database
app.get('/users', async (req, res) => {
const client = new MongoClient(process.env.uri)
const userIds = JSON.parse(req.query.userIds)
try {
await client.connect()
const database = client.db('app-data')
const users = database.collection('users')
const pipeline =
[
{
'$match': {
'user_id': {
'$in': userIds
}
}
}
]
const foundUsers = await users.aggregate(pipeline).toArray()
res.json(foundUsers)
} finally {
await client.close()
}
})
// Get all the Gendered Users in the Database
app.get('/gendered-users', async (req, res) => {
const client = new MongoClient(process.env.uri)
const gender = req.query.gender
try {
await client.connect()
const database = client.db('app-data')
const users = database.collection('users')
const query = { gender_identity: { $eq: gender } }
const foundUsers = await users.find(query).toArray()
res.json(foundUsers)
} finally {
await client.close()
}
})
// Update a User in the Database
app.put('/user', async (req, res) => {
const client = new MongoClient(process.env.uri)
const formData = req.body.formData
try {
await client.connect()
const database = client.db('app-data')
const users = database.collection('users')
const query = { user_id: formData.user_id }
const updateDocument = {
$set: {
first_name: formData.first_name,
dob_day: formData.dob_day,
dob_month: formData.dob_month,
dob_year: formData.dob_year,
show_gender: formData.show_gender,
gender_identity: formData.gender_identity,
gender_interest: formData.gender_interest,
url: formData.url,
about: formData.about,
matches: formData.matches
},
}
const insertedUser = await users.updateOne(query, updateDocument)
res.json(insertedUser)
} finally {
await client.close()
}
})
// Get Messages by from_userId and to_userId
app.get('/messages', async (req, res) => {
const { userId, correspondingUserId } = req.query
const client = new MongoClient(process.env.uri)
try {
await client.connect()
const database = client.db('app-data')
const messages = database.collection('messages')
const query = {
from_userId: userId, to_userId: correspondingUserId
}
const foundMessages = await messages.find(query).toArray()
res.send(foundMessages)
} finally {
await client.close()
}
})
// Add a Message to our Database
app.post('/message', async (req, res) => {
const client = new MongoClient(process.env.uri)
const message = req.body.message
try {
await client.connect()
const database = client.db('app-data')
const messages = database.collection('messages')
const insertedMessage = await messages.insertOne(message)
res.send(insertedMessage)
} finally {
await client.close()
}
})
app.listen(process.env.PORT, () => console.log('server running on PORT ' + process.env.PORT))