-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
223 lines (187 loc) · 6.33 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
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const session = require("express-session");
const bcrypt = require("bcrypt"); // Import bcrypt for password hashing
require('dotenv').config();
const mongoose = require('mongoose');
const Todo = require("./models/todo");
const User = require("./models/user"); // Import your User model
const port = process.env.PORT || 3000;
// Middleware setup
app.set('view engine', 'ejs');
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Session management
app.use(session({
secret: process.env.SESSION_SECRET || 'mydemosecretkey123', // Use a strong secret in production
resave: false,
saveUninitialized: true,
cookie: { secure: false } // Set to true in production with HTTPS
}));
// Connect to MongoDB Atlas using the URI from the .env file
const dburl = process.env.MONGO_URI;
mongoose.connect(dburl, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('Connected to MongoDB Atlas'))
.catch(err => {
console.error('Error connecting to MongoDB Atlas:', err);
process.exit(1);
});
// Fetch all todos and render them on the homepage
app.get("/", async (req, res) => {
try {
const todos = await Todo.find();
res.render("index", { todos, user: req.session.user }); // Pass user session to views
} catch (error) {
console.error("Error occurred while fetching todos:", error);
res.status(500).send("Internal Server Error");
}
});
// Define the calendar route
app.get('/features/calendar', (req, res) => {
console.log('Calendar route accessed');
res.render('calendar');
});
// Define the clock route
app.get('/features/clock', (req, res) => {
console.log('Clock route accessed');
res.render('clock');
});
// Define the calculator route
app.get('/features/culculator', (req, res) => {
console.log('culculator route accessed');
res.render('culculator');
});
// Serve the About page
app.get("/about", (req, res) => {
res.render("about");
});
// Serve the Contact us page
app.get("/contact", (req, res) => {
res.render("contact");
});
// Serve the Login/Signup page
app.get("/login", (req, res) => {
res.render("auth");
});
// Handle user login
app.post("/login", async (req, res) => {
const { email, password } = req.body;
try {
// Log the received email for debugging
console.log("Received login attempt for email:", email);
// Find the user by email
const user = await User.findOne({ email });
// If user is found, compare passwords
if (user) {
const isMatch = await bcrypt.compare(password, user.password);
console.log("Password match result:", isMatch);
if (isMatch) {
// Store user in session
req.session.user = { email };
return res.redirect("/");
return res.alert("Log In successfully!!!");
}
}
// If user not found or password doesn't match
res.status(401).send("Invalid email or password");
} catch (error) {
console.error("Error during login:", error);
res.status(500).send("Internal Server Error");
}
});
// Handle user signup
app.post("/signup", async (req, res) => {
const { fullName, email, password } = req.body;
try {
const existingUser = await User.findOne({ email });
if (existingUser) {
return res.status(400).send("User with this email already exists.");
}
// Hash the password before saving
const hashedPassword = await bcrypt.hash(password, 10);
const newUser = new User({
fullName,
email,
password: hashedPassword,
});
await newUser.save();
console.log(`User signed up: ${fullName}, ${email}`);
// Automatically log the user in after signup
req.session.user = { email }; // Store user in session
res.redirect("/");
} catch (error) {
console.error("Error occurred during signup:", error);
res.status(500).send("Internal Server Error");
}
});
// Add a new todo
app.post("/", async (req, res) => {
const todo = new Todo({
todo: req.body.todoValue
});
try {
await todo.save();
res.redirect("/");
} catch (error) {
console.error("Error occurred while saving todo:", error);
res.status(500).send("Internal Server Error");
}
});
// Delete a todo by ID
app.delete("/:id", async (req, res) => {
const id = req.params.id;
if (!id) {
return res.status(400).send("ID parameter is missing");
}
try {
const deletedTodo = await Todo.findByIdAndDelete(id);
if (!deletedTodo) {
return res.status(404).send("Todo not found");
}
res.sendStatus(200);
} catch (error) {
console.error("Error occurred while deleting todo:", error);
res.status(500).send("Internal Server Error");
}
});
// Get a specific todo for editing
app.get("/edit/:id", async (req, res) => {
const id = req.params.id;
if (!id) {
return res.status(400).send("ID parameter is missing");
}
try {
const todo = await Todo.findById(id);
if (!todo) {
return res.status(404).send("Todo not found");
}
res.render("edit", { todo });
} catch (error) {
console.error("Error occurred while fetching todo for editing:", error);
res.status(500).send("Internal Server Error");
}
});
// Update a todo by ID
app.post("/update/:id", async (req, res) => {
const id = req.params.id;
const updatedTodoText = req.body.todo;
if (!id || !updatedTodoText) {
return res.status(400).send("Missing required parameters");
}
try {
const updatedTodo = await Todo.findByIdAndUpdate(id, { todo: updatedTodoText }, { new: true });
if (!updatedTodo) {
return res.status(404).send("Todo not found");
}
res.redirect("/");
} catch (error) {
console.error("Error occurred while updating todo:", error);
res.status(500).send("Internal Server Error");
}
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});