-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
385 lines (315 loc) · 10.4 KB
/
server.mjs
File metadata and controls
385 lines (315 loc) · 10.4 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import express from 'express';
import cors from 'cors';
import db from './db/conn.mjs';
import session from 'express-session';
import passport from 'passport';
import { Strategy as GoogleStrategy } from 'passport-google-oauth20';
import {v2 as cloudinary} from 'cloudinary';
import multer from 'multer';
cloudinary.config({
cloud_name: 'dx53dzhsi',
api_key: '365261683399358',
api_secret: 'svIa002YmKorEASm_i0y2_qufaU'
});
const upload = multer({ dest: 'uploads/' });
const app = express();
app.use(express.json());
app.use(cors());
app.use(
session({
secret: 'your-secret-key',
resave: false,
saveUninitialized: true,
})
);
app.use(passport.initialize());
app.use(passport.session());
const database = db.collection('users');
// Set up Google OAuth strategy
passport.use(
new GoogleStrategy(
{
clientID: '210428474446-7sd68r5p5bnvcphf2bt38ai0v8ql1944.apps.googleusercontent.com',
clientSecret: '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',
callbackURL: 'http://localhost:3001/auth/google/callback',
},
async (accessToken, refreshToken, profile, done) => {
// Use the profile information to find or create a user in your database
// You might need to adjust this based on your database structure
try {
let user = await database.findOne({ googleId: profile.id });
if (!user) {
// Create a new user if not found
const newUser = {
name: profile.displayName,
googleId: profile.id,
profilePic: profile.photos[0].value,
threads: [],
username: '',
description: '',
};
const result = await database.insertOne(newUser);
if (result.acknowledged) {
user = newUser;
}
}
return done(null, user);
} catch (error) {
return done(error);
}
}
)
);
// Serialize user to session
passport.serializeUser((user, done) => {
done(null, user);
});
// Deserialize user from session
passport.deserializeUser((user, done) => {
done(null, user);
});
// Route to start Google OAuth
app.get('/auth/google', passport.authenticate('google', { scope: ['profile'] }));
// Callback route after successful Google OAuth
// Callback route after successful Google OAuth
app.get(
'/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
(req, res) => {
// Redirect to the frontend after successful authentication with Google ID
res.redirect(`http://localhost:3000/home/${req.user.googleId}`);
}
);
app.get('/', async (req, res) => {
try {
const users = await database.find({}).toArray();
res.json(users);
} catch (error) {
console.error(error);
res.status(500).json('internal server error');
}
});
app.post('/register', async (req, res) => {
try {
const { googleId, username, description } = req.body;
// Find the user by Google ID and update the username and description
const updatedUser = await database.findOneAndUpdate(
{ googleId },
{ $set: { username, description } },
{ new: true }
);
if (!updatedUser) {
return res.status(404).json({ message: 'User not found' });
}
console.log(updatedUser);
return res.status(200).json(updatedUser);
} catch (error) {
console.error(error);
return res.status(500).json({ message: 'Server error' });
}
});
app.post('/update' , async (req, res) => {
try {
const { name, username, description, googleId } = req.body; // Assuming the request body contains these fields
// Find the user by Google ID and update the fields
const updatedUser = await database.findOneAndUpdate(
{ googleId },
{ $set: { name, username, description } },
{ new: true }
);
if (!updatedUser) {
return res.status(404).json({ message: 'User not found' });
}
return res.status(200).json({ message: 'Profile updated successfully', user: updatedUser });
} catch (error) {
console.error(error);
return res.status(500).json({ message: 'Server error' });
}
});
app.post('/upload', upload.single('image'), async (req, res) => {
try {
const result = await cloudinary.uploader.upload(req.file.path);
res.status(200).json({ message: 'Image uploaded successfully', imageUrl: result.secure_url });
} catch (error) {
console.error('Error uploading image:', error);
res.status(500).json({ message: 'Server error' });
}
});
app.get('/profile/:id', async (req, res) => {
try {
const { id } = req.params;
const user = await database.findOne({ id });
if (user) {
res.json('success');
}
else {
res.status('400').json('user not found');
}
} catch (error) {
console.error(error);
res.status(500).json('internal server error');
}
});
app.put('/threads/:id', async (req, res) => {
try {
const { id } = req.params;
const index = database.threads.findIndex(thread => thread.id === id);
if (index !== -1) {
database.threads[index].likes++; // Increment the likes
res.json(database.threads[index].likes); // Send back the updated likes count
} else {
res.status(400).json('Thread not found');
}
} catch (error) {
console.error(error);
res.status(500).json('Internal server error');
}
});
app.get('/user/:googleId', async (req, res) => {
try {
const { googleId } = req.params;
const user = await database.findOne({ googleId });
if (user) {
res.json(user);
} else {
res.status(400).json('user not found');
}
} catch (error) {
console.error(error);
res.status(500).json('internal server error');
}
});
app.post('/post', async (req, res) => {
try {
const { content, googleId } = req.body;
if (!content) {
return res.status(400).json({ message: 'Content is required.' });
}
const user = await database.findOne({googleId});
const newPost = {
id: user.threads.length,
content: content,
likes: 0,
};
const updatedUser = await database.findOneAndUpdate(
{ googleId },
{ $push: { threads: newPost } },
{ new: true }
);
console.log(updatedUser);
return res.status(201).json({ message: 'Post added successfully', user: user });
} catch (error) {
console.error(error);
return res.status(500).json({ message: 'Server error' });
}
});
app.post('/reply', async (req, res) => {
try {
const { content, googleId, threadId } = req.body;
if (!content) {
return res.status(400).json({ message: 'Content is required.' });
}
const user = await database.findOne({ googleId });
if (!user) {
return res.status(404).json({ message: 'User not found.' });
}
const threadIndex = user.threads.findIndex(thread => thread.id === threadId);
if (threadIndex === -1) {
return res.status(404).json({ message: 'Thread not found.' });
}
console.log(user.threads)
if (!user.threads[threadIndex].replies) {
user.threads[threadIndex].replies = [];
}
const newReply = {
id: user.threads[threadIndex].replies.length,
content: content,
likes: 0,
};
if (!user.threads[threadIndex].replies) {
user.threads[threadIndex].replies = [];
}
user.threads[threadIndex].replies.push(newReply);
const updatedUser = await database.updateOne(
{ googleId },
{
$push: {
'threads.$[thread].replies': newReply
}
},
{
arrayFilters: [{ 'thread.id': threadId }],
}
);
if (updatedUser.matchedCount === 0) {
return res.status(404).json({ message: 'User not found or thread not found' });
}
return res.status(201).json({ message: 'Reply added successfully', user: user });
} catch (error) {
console.error(error);
return res.status(500).json({ message: 'Server error' });
}
});
app.delete('/thread/:username/:threadId', async (req, res) => {
try {
const { username, threadId } = req.params;
// Find the user by username
const user = await database.findOne({ username });
if (!user) {
return res.status(404).json({ message: 'User not found' });
}
// Find the index of the thread to be deleted in the user's threads array
const threadIndex = user.threads.findIndex(thread => thread.id === parseInt(threadId));
if (threadIndex === -1) {
return res.status(404).json({ message: 'Thread not found' });
}
// Remove the thread from the user's threads array
user.threads.splice(threadIndex, 1);
// Update the user in the database
const updatedUser = await database.findOneAndUpdate(
{ username },
{ $set: { threads: user.threads } },
{ new: true }
);
return res.status(200).json({ message: 'Thread deleted successfully', user: updatedUser });
} catch (error) {
console.error(error);
return res.status(500).json({ message: 'Server error' });
}
});
app.delete('/thread/:username/:threadId/:replyId', async (req, res) => {
try {
const { username, threadId, replyId } = req.params;
// Find the user by username
const user = await database.findOne({ username });
if (!user) {
return res.status(404).json({ message: 'User not found' });
}
// Find the index of the thread to be deleted in the user's threads array
const threadIndex = user.threads.findIndex(thread => thread.id === parseInt(threadId));
if (threadIndex === -1) {
return res.status(404).json({ message: 'Thread not found' });
}
const updateResult = await database.updateOne(
{ username },
{
$pull: {
'threads.$[thread].replies': { id: parseInt(replyId) }
}
},
{
arrayFilters: [{ 'thread.id': parseInt(threadId) }],
}
);
if (updateResult.matchedCount === 0) {
return res.status(404).json({ message: 'User not found or thread not found' });
}
return res.status(200).json({ message: 'Thread deleted successfully', user: user });
} catch (error) {
console.error(error);
return res.status(500).json({ message: 'Server error' });
}
});
app.listen(3001, () => {
console.log('Server is running on port 3001');
});