-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
199 lines (176 loc) · 6.55 KB
/
app.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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
const bcrypt = require('bcryptjs');
const session = require('express-session');
const app = express();
app.use(session({
secret: '@ymAnel123',
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}));
const User = require('./models/user');
app.use(bodyParser.json());
app.use(cors());
// Serve registration page
app.get('/register', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'register.html'));
});
// Registration endpoint
app.post('/api/register', async (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
res.status(400).json({ error: 'Username and password are required' });
return;
}
if (!/^[a-zA-Z0-9]+$/.test(username)) {
res.status(400).json({ error: 'Username must contain only letters and numbers' });
return;
}
try {
const [rows] = await db.execute('SELECT * FROM users WHERE username = ?', [username]);
if (rows.length > 0) {
res.status(400).json({ error: 'Username already exists' });
return;
}
const hashedPassword =bcrypt.hashSync(password, 10);
const [result] = await db.execute('INSERT INTO users SET username = ?, password = ?', [username, hashedPassword]);
res.json({ message: 'Registration successful!!', redirect: '/' }); // Return a JSON response with a redirect URL
} catch (err) {
console.error(err);
res.status(400).json({ error: 'Error registering user: ' + err.message });
}
});
// Serve login page
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'login.html'));
});
// Login endpoint
app.post('/api/login', async (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
res.status(400).json({ success: false, message: 'Username and password are required' });
return;
}
try {
const [rows] = await db.execute('SELECT * FROM users WHERE username = ?', [username]);
if (rows.length === 0) {
res.status(401).json({ success: false, message: 'Invalid username or password' });
} else {
const isValidPassword =bcrypt.compareSync(password, rows[0].password);
if (isValidPassword) {
req.session.userId = rows[0].id; // Set the user ID in the session
res.json({ success: true, message: 'Login successful!!', userId: rows[0].id });
} else {
res.status(401).json({ success: false, message: 'Invalid username or password' });
}
}
} catch (error) {
console.error('Error:', error.message, error.stack);
res.status(400).json({ success: false, message: 'Error logging in: ' + error.message });
}
});
const { db, getTasks } = require('./db');
// Get tasks endpoint
app.get('/api/tasks', async (req, res) => {
const userId = req.session.userId; // Get the user ID from the session
try {
const [tasks] = await db.execute('SELECT * FROM tasks WHERE userId = ?', [userId]);
res.json(tasks);
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Error fetching tasks' });
}
});
// Create task endpoint
app.post('/api/tasks', async (req, res) => {
const { title, description, dueDate, category } = req.body;
const userId = req.session.userId; // Get the user ID from the session
try {
const [rows] = await db.execute('SELECT * FROM tasks WHERE title = ? AND description = ? AND dueDate = ? AND category = ? AND userId = ?', [title, description, dueDate, category, userId]);
if (rows.length > 0) {
res.status(400).json({ error: 'Task already exists' });
} else {
const [result] = await db.execute('INSERT INTO tasks SET title = ?, description = ?, dueDate = ?, category = ?, userId = ?', [title, description, dueDate, category, userId]);
const taskId = result.insertId;
res.json({ message: 'Task created successfully ',taskId });
}
} catch (err) {
console.error(err);
res.status(400).send('Error creating task');
}
});
// Toggle task endpoint
app.put('/api/tasks/:id/toggle', async (req, res) => {
const id = req.params.id;
const userId = req.session.userId; // Get the user ID from the session
try {
const [rows] = await db.execute('SELECT * FROM tasks WHERE id = ? AND userId = ?', [id, userId]);
if (rows.length === 0) {
res.status(404).json({ error: 'Task not found' });
} else {
const task = rows[0];
task.completed = !task.completed;
await db.execute('UPDATE tasks SET completed = ? WHERE id = ?', [task.completed, id]);
res.json({ message: 'Task toggled successfully' });
}
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Error toggling task' });
}
});
// Update task endpoint
app.put('/api/tasks/:id', async (req, res) => {
const id = req.params.id;
const { title, description, dueDate, category } = req.body;
const userId = req.session.userId; // Get the user ID from the session
try {
const [rows] = await db.execute('SELECT * FROM tasks WHERE id = ? AND userId = ?', [id, userId]);
if (rows.length === 0) {
res.status(404).json({ error: 'Task not found' });
} else {
await db.execute('UPDATE tasks SET title = ?, description = ?, dueDate = ?, category = ? WHERE id = ?', [title, description, dueDate, category, id]);
res.json({ message: 'Task updated successfully' });
}
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Error updating task' });
}
});
// Delete task endpoint
app.delete('/api/tasks/:id', async (req, res) => {
const id = req.params.id;
const userId = req.session.userId; // Get the user ID from the session
try {
const [rows] = await db.execute('SELECT * FROM tasks WHERE id = ? AND userId = ?', [id, userId]);
if (rows.length === 0) {
res.status(404).json({ error: 'Task not found' });
} else {
await db.execute('DELETE FROM tasks WHERE id = ?', [id]);
res.json({ message: 'Task deleted successfully' });
}
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Error deleting task' });
}
});
// Static files
app.use(express.static('public'));
// Start server
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
app.post('/api/logout', (req, res) => {
console.log('Received logout request');
req.session.destroy((err) => {
if (err) {
console.error('Error logging out:', err);
res.status(500).send('Error logging out');
} else {
console.log('Logged out successfully');
res.send('Logged out successfully');
}
});
});