-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
272 lines (244 loc) · 8.8 KB
/
server.js
File metadata and controls
272 lines (244 loc) · 8.8 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
const express = require('express');
const mongoose = require('mongoose');
const session = require('express-session');
const bcrypt = require('bcryptjs');
const path = require('path');
const marked = require('marked');
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
require('dotenv').config();
const app = express();
const port = process.env.PORT || 3000;
// MongoDB Connection
mongoose.connect(process.env.MONGODB_URI || 'mongodb+srv://srujal:srujal%40m0ng0db@srujal.aitn3br.mongodb.net/?retryWrites=true&w=majority&appName=Srujal', {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('MongoDB connection error:', err));
// Passport Configuration
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: "https://ideadump-five.vercel.app/auth/google/callback"
},
async (accessToken, refreshToken, profile, done) => {
try {
let user = await User.findOne({ googleId: profile.id });
if (!user) {
// Check if a user with the same email exists
user = await User.findOne({ email: profile.emails[0].value });
if (user) {
// Link Google account to existing user
user.googleId = profile.id;
await user.save();
} else {
// Create a new user
user = new User({
googleId: profile.id,
username: profile.displayName,
email: profile.emails[0].value
});
await user.save();
}
}
return done(null, user);
} catch (error) {
return done(error, null);
}
}));
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser(async (id, done) => {
try {
const user = await User.findById(id);
done(null, user);
} catch (error) {
done(error, null);
}
});
// MongoDB Schema and Model (Assuming you have a User and Idea model)
const UserSchema = new mongoose.Schema({
username: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
password: {
type: String,
required: function() {
return !this.googleId;
}
},
googleId: { type: String },
// Add any other user fields
});
const User = mongoose.model('User', UserSchema);
const IdeaSchema = new mongoose.Schema({
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
title: { type: String, required: true },
description: String,
createdAt: { type: Date, default: Date.now },
updatedAt: { type: Date, default: Date.now }
});
const Idea = mongoose.model('Idea', IdeaSchema);
// Middleware
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: { secure: process.env.NODE_ENV === 'production' }
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Middleware to check if user is authenticated
const isAuthenticated = (req, res, next) => {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login');
};
// Routes
// GET routes for static pages
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.get('/signup', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'signup.html'));
});
app.get('/login', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'login.html'));
});
// Google OAuth Routes
app.get('/auth/google',
passport.authenticate('google', { scope: ['profile', 'email'] })
);
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
(req, res) => {
res.redirect('/dashboard');
}
);
app.get('/dashboard', isAuthenticated, async (req, res) => {
try {
const ideas = await Idea.find({ userId: req.user._id }).sort({ createdAt: 'desc' });
res.render('dashboard', {
user: req.user,
ideas: ideas,
marked: marked
});
} catch (error) {
console.error('Error fetching ideas:', error);
res.status(500).send('Error fetching your ideas.');
}
});
app.get('/logout', (req, res) => {
req.logout(() => {
res.redirect('/');
});
});
// API routes
// POST route for signup
app.post('/signup', async (req, res) => {
try {
const { username, email, password } = req.body;
const existingUser = await User.findOne({ $or: [{ username }, { email }] });
if (existingUser) {
return res.status(400).json({ message: 'Username or email already exists.' });
}
const hashedPassword = await bcrypt.hash(password, 10);
const newUser = new User({ username, email, password: hashedPassword });
await newUser.save();
req.session.userId = newUser._id;
req.session.username = newUser.username;
res.status(201).json({ message: 'User registered successfully.' });
} catch (error) {
console.error('Registration error:', error);
res.status(500).json({ message: 'Failed to register user.' });
}
});
// POST route for login
app.post('/login', async (req, res) => {
try {
const { email, password } = req.body;
const user = await User.findOne({ email });
if (!user || !(await bcrypt.compare(password, user.password))) {
return res.status(401).json({ message: 'Invalid credentials.' });
}
req.login(user, (err) => {
if (err) {
console.error('Login error:', err);
return res.status(500).json({ message: 'Failed to login.' });
}
return res.status(200).json({ message: 'Logged in successfully.' });
});
} catch (error) {
console.error('Login error:', error);
res.status(500).json({ message: 'Failed to login.' });
}
});
// GET route to fetch user info (for displaying username on dashboard)
app.get('/api/user', isAuthenticated, (req, res) => {
res.json({ username: req.user.username, userId: req.user._id });
});
// GET route to fetch all ideas for the logged-in user
app.get('/api/ideas', isAuthenticated, async (req, res) => {
try {
const ideas = await Idea.find({ userId: req.user._id }).sort({ createdAt: 'desc' });
res.json(ideas);
} catch (error) {
console.error('Error fetching ideas:', error);
res.status(500).json({ message: 'Failed to fetch ideas.' });
}
});
// POST route to save a new idea
app.post('/api/ideas', isAuthenticated, async (req, res) => {
try {
const { title, description } = req.body;
const newIdea = new Idea({ userId: req.user._id, title, description });
await newIdea.save();
res.status(201).json({ message: 'Idea saved successfully!', idea: newIdea });
} catch (error) {
console.error('Error saving idea:', error);
res.status(500).json({ message: 'Failed to save idea.' });
}
});
// DELETE route to delete an idea
app.delete('/api/ideas/:id', isAuthenticated, async (req, res) => {
const ideaId = req.params.id;
try {
const idea = await Idea.findOneAndDelete({ _id: ideaId, userId: req.user._id });
if (!idea) {
return res.status(404).json({ message: 'Idea not found or you are not authorized to delete it.' });
}
res.status(200).json({ message: 'Idea deleted successfully.' });
} catch (error) {
console.error('Error deleting idea:', error);
res.status(500).json({ message: 'Failed to delete idea.' });
}
});
// PUT route to update an existing idea
app.put('/api/ideas/:id', isAuthenticated, async (req, res) => {
const ideaId = req.params.id;
const { title, description } = req.body;
try {
const updatedIdea = await Idea.findOneAndUpdate(
{ _id: ideaId, userId: req.user._id },
{ title, description, updatedAt: Date.now() },
{ new: true } // Return the updated document
);
if (!updatedIdea) {
return res.status(404).json({ message: 'Idea not found or you are not authorized to update it.' });
}
res.status(200).json({ message: 'Idea updated successfully!', idea: updatedIdea });
} catch (error) {
console.error('Error updating idea:', error);
res.status(500).json({ message: 'Failed to update idea.' });
}
});
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});