-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
218 lines (180 loc) · 6.56 KB
/
server.js
File metadata and controls
218 lines (180 loc) · 6.56 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
const express = require('express');
const mongoose = require('mongoose');
const session = require('express-session');
const bcrypt = require('bcrypt');
const path = require('path');
const User = require('./models/User');
const app = express();
// ===== Middleware =====
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Disable caching globally
app.use((req, res, next) => {
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
res.setHeader('Surrogate-Control', 'no-store');
next();
});
// ===== Sessions =====
app.use(
session({
secret: '/* YOUR_SESSION_SECRET */',
resave: false,
saveUninitialized: false,
})
);
// ===== Database Connection =====
mongoose
.connect('/* YOUR_MONGODB_ATLAS_CONNECTION_STRING */')
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('MongoDB connection error:', err));
// ===== Auth Middleware =====
function requireLogin(req, res, next) {
if (!req.session.userId) return res.redirect('/auth.html');
next();
}
// ===== Static Files =====
app.use(express.static(path.join(__dirname, 'views'), { index: false }));
// ===== Routes =====
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'views', 'index.html'));
});
// --- Signup
app.post('/signup', async (req, res) => {
const { email, password } = req.body;
if (!email || !email.includes('@'))
return res.redirect('/auth.html?form=signup&error=Invalid email');
if (!password || password.length < 8)
return res.redirect('/auth.html?form=signup&error=Password must be at least 8 characters');
try {
const existing = await User.findOne({ email });
if (existing)
return res.redirect('/auth.html?form=signup&error=User already exists');
const hashed = await bcrypt.hash(password, 10);
const user = new User({ email, password: hashed });
await user.save();
req.session.userId = user._id;
console.log('✅ User signed up:', email);
res.redirect('/index.html');
} catch (err) {
console.error('Signup error:', err);
res.redirect('/auth.html?form=signup&error=Server error occurred');
}
});
// --- Login
app.post('/login', async (req, res) => {
const { email, password } = req.body;
if (!email || !email.includes('@'))
return res.redirect('/auth.html?form=login&error=Invalid email');
if (!password || password.length < 8)
return res.redirect('/auth.html?form=login&error=Password must be at least 8 characters');
try {
const user = await User.findOne({ email });
if (!user)
return res.redirect('/auth.html?form=login&error=User not found');
const match = await bcrypt.compare(password, user.password);
if (!match)
return res.redirect('/auth.html?form=login&error=Incorrect password');
req.session.userId = user._id;
console.log('User logged in:', email);
res.redirect('/index.html');
} catch (err) {
console.error('Login error:', err);
res.redirect('/auth.html?form=login&error=Server error occurred');
}
});
// --- Logout
app.post('/logout', (req, res) => {
req.session.destroy(() => res.redirect('/auth.html'));
});
// --- Session Check
app.get('/check-session', async (req, res) => {
try {
if (req.session.userId) {
const user = await User.findById(req.session.userId).select("email");
if (user) return res.json({ loggedIn: true, email: user.email });
}
res.json({ loggedIn: false });
} catch {
res.json({ loggedIn: false });
}
});
// --- Protected Pages
app.get('/index.html', requireLogin, (req, res) => {
res.sendFile(path.join(__dirname, 'views', 'index.html'));
});
app.get('/favorites.html', requireLogin, (req, res) => {
res.sendFile(path.join(__dirname, 'views', 'favorites.html'));
});
// --- Save Favorite
app.post('/favorites', async (req, res) => {
if (!req.session.userId)
return res.status(401).json({ error: "You must be logged in." });
const { title, content } = req.body;
if (!title || !content)
return res.status(400).json({ error: "Title and content required." });
try {
const user = await User.findById(req.session.userId);
user.favorites.push({ title, content });
await user.save();
console.log('✅ Favorite saved for:', user.email);
res.json({ success: true });
} catch (err) {
console.error("Save favorite error:", err);
res.status(500).json({ error: "Server error" });
}
});
// --- Delete Favorite
app.delete("/favorites/:id", async (req, res) => {
try {
const userId = req.session.userId;
if (!userId) return res.status(401).json({ error: "Unauthorized" });
const user = await User.findById(userId);
user.favorites = user.favorites.filter(f => f._id.toString() !== req.params.id);
await user.save();
res.json({ success: true });
} catch (err) {
console.error("Delete favorite error:", err);
res.status(500).json({ error: "Failed to delete" });
}
});
// --- Get Favorites
app.get('/favorites', requireLogin, async (req, res) => {
try {
const user = await User.findById(req.session.userId).select("favorites");
res.json(user?.favorites || []);
} catch (err) {
console.error("Fetch favorites error:", err);
res.status(500).json({ error: "Server error" });
}
});
// ===== Gemini API Integration =====
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
// Replace with your actual Gemini API key or use environment variable
const GEMINI_API_KEY = "/* YOUR_GEMINI_API_KEY */";
app.post('/generate-recipe', async (req, res) => {
try {
const { ingredients } = req.body;
if (!ingredients || !Array.isArray(ingredients) || !ingredients.length)
return res.status(400).json({ error: 'Invalid ingredients array' });
const apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${GEMINI_API_KEY}`;
const payload = {
contents: [{ parts: [{ text: `Recipe prompt with ${ingredients.join(', ')}` }] }],
generationConfig: { temperature: 0.7, maxOutputTokens: 2048 },
};
const apiResp = await fetch(apiUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
const data = await apiResp.json();
res.json(data);
} catch (err) {
console.error("Generate recipe error:", err);
res.status(500).json({ error: "Internal server error" });
}
});
// ===== Server Start =====
const PORT = 3000; // or process.env.PORT
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));