-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
276 lines (225 loc) · 7.72 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const mongoose = require("mongoose");
const jwt = require("jsonwebtoken");
const cron = require("cron");
const axios = require("axios");
const app = express();
app.use(cors());
mongoose.connect(process.env.MONGODB_STRING);
var db = mongoose.connection;
db.on('error', console.log.bind(console, "connection error"));
db.once('open', function (callback) { console.log("connection succeeded"); })
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.json());
// Function to send a GET request to the check_availability endpoint dynamically
async function checkAvailability() {
try {
const emailToCheck = "test@connect2me.com";
const host = `https://connect2meapi.onrender.com/check_availability/${emailToCheck}`;
const hostLocal = `http://localhost:3000/check_availability/${emailToCheck}`;
const response = await axios.get(host);
console.log("Response from /check_availability API:", response);
} catch (error) {
console.error("Error making GET request to /check_availability API:", error);
}
}
// Set up a cron job to send the GET request every 14 minutes
const keepAliveCronJob = new cron.CronJob('*/14 * * * *', () => {
console.log("Cron job executed to check availability.");
checkAvailability();
});
// Start the cron job
keepAliveCronJob.start();
//SIGNUP
app.post('/sign_up/', async function (req, res) {
var username = req.body.username;
var email = req.body.email;
var password = req.body.password;
var data = {
username, email, password
}
db.collection('connect2me_logon_collection').insertOne(data, function (err, collection) {
if (err) throw err;
console.log("Record inserted Successfully : " + collection);
});
})
//VIEW-SIGNUP-DATA
app.get('/check_availability/:id', async function (req, res) {
var answer = await db.collection('connect2me_logon_collection').findOne({ email: req.params['id'].toLowerCase() }, function (err, collection) {
if (err) throw err;
console.log(collection)
return collection;
});
if (answer) { res.send({ _id: 'Does Exist', email: req.params.id.toLowerCase(), description: 'Email Address Does Exist!' }); }
else { res.send({ email: 'No', description: 'No Such Email Address Present In The Database!' }); }
res.end();
})
//SIGNIN
app.post('/sign_in/', async function (req, res) {
var answer = await db.collection('connect2me_logon_collection').findOne({ email: req.body['email'], password: req.body['password'] }, function (err, collection) {
if (err) throw err;
console.log(collection)
return collection;
});
if (answer) {
var email = req.body['email'];
var token = jwt.sign({ email: email }, process.env.ACCESS_TOKEN, { expiresIn: '24h' });
res.json({ auth: 'success', email: email, token: token });
}
else {
res.json({ auth: 'failure' });
}
res.end();
})
//SEND-EMAIL
app.post('/send_email/', function (req, res) {
try {
var token = req.body['token'];
var decoded = jwt.verify(token, process.env.ACCESS_TOKEN);
if (decoded.email) {
var from = decoded.email;
var to = req.body.to;
var dateTime = Buffer.from(req.body.dateandtime).toString('base64');
var message = Buffer.from(req.body.message).toString('base64');
var personalContent = req.body.personalContent;
var data = {
from, to, dateTime, message, personalContent
}
db.collection('connect2me_email_collection').insertOne(data, function (err, collection) {
if (err) throw err;
console.log("Record inserted Successfully : " + collection);
});
res.end();
}
}
catch (e) {
res.send({ error: e.message });
}
})
//VERIFY-TOKEN
app.post('/verify_token/', function (req, res) {
try {
var token = req.body['token'];
var decoded = jwt.verify(token, process.env.ACCESS_TOKEN);
if (decoded.email) {
res.send({ message: "success" });
}
}
catch (e) {
res.send({ message: e.message });
}
})
//VIEWING-EMAIL
app.get('/view_email/:limit/:skip/:token', async function (req, res) {
try {
var token = req.params['token'];
var decoded = jwt.verify(token, process.env.ACCESS_TOKEN);
if (decoded.email) {
var answer = await db.collection('connect2me_email_collection').find({ to: decoded.email }, function (err, collection) {
if (err) throw err;
console.log(collection)
return collection;
}).sort({ _id: -1 }).limit(parseInt(req.params['limit'])).skip(parseInt(req.params['skip'])).toArray();
if (answer) {
res.send(answer);
}
res.end();
}
}
catch (e) {
res.send({ error: e.message });
}
})
//HOW-MANY-EMAILS
app.get('/how_many_emails/:token', async function (req, res) {
try {
var token = req.params['token'];
var decoded = jwt.verify(token, process.env.ACCESS_TOKEN);
if (decoded.email) {
var answer = await db.collection('connect2me_email_collection').countDocuments({ to: decoded.email }, function (err, collection) {
if (err) throw err;
console.log(collection)
return collection;
});
if (answer) {
res.json({ emails: answer })
}
else {
res.json({ emails: 0 });
}
res.end();
}
}
catch (e) {
res.send({ error: e.message });
}
})
//DELETING-EMAIL
app.post('/delete_email/', async function (req, res) {
var ObjectId = require('mongodb').ObjectId;
const id = req.body.id;
const convertedObjectId = new ObjectId(id);
var answer = await db.collection('connect2me_email_collection').deleteOne({ _id: convertedObjectId }, function (err, collection) {
if (err) throw err;
console.log(collection)
return collection;
});
if (answer) {
res.send(answer);
}
res.end();
})
//DELETING-ALL-EMAILS
app.post('/delete_all_emails/', async function (req, res) {
try {
var token = req.body['token'];
var decoded = jwt.verify(token, process.env.ACCESS_TOKEN);
if (decoded.email) {
await db.collection('connect2me_email_collection').deleteMany({ to: decoded.email }, function (err, collection) {
if (err) throw err;
console.log(collection)
return collection;
});
res.send({ message: "all emails deleted" });
res.end();
}
}
catch (e) {
res.send({ error: e.message });
}
})
//DELETING-ACCOUNT
app.post('/delete_account/', async function (req, res) {
try {
var token = req.body['token'];
var decoded = jwt.verify(token, process.env.ACCESS_TOKEN);
if (decoded.email) {
await db.collection('connect2me_logon_collection').deleteOne({ email: decoded.email }, function (err, collection) {
if (err) throw err;
console.log(collection)
return collection;
});
await db.collection('connect2me_email_collection').deleteMany({ from: decoded.email }, function (err, collection) {
if (err) throw err;
console.log(collection)
return collection;
});
await db.collection('connect2me_email_collection').deleteMany({ to: decoded.email }, function (err, collection) {
if (err) throw err;
console.log(collection)
return collection;
});
res.send({ message: "account deleted" });
res.end();
}
}
catch (e) {
res.send({ error: e.message });
}
})
app.listen(process.env.PORT || 3000);