-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
160 lines (147 loc) · 5.13 KB
/
server.js
File metadata and controls
160 lines (147 loc) · 5.13 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
// 1. IMPORTS
const express = require('express');
const cors = require('cors');
const sqlite3 = require('sqlite3').verbose();
const path = require('path');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const JWT_SECRET = 'your-super-secret-key-that-should-be-long-and-random';
// 2. APP SETUP
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname)));
// 3. DATABASE CONNECTION
const db = new sqlite3.Database('./tasks.db', (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the tasks database.');
db.run('CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE, password TEXT)');
db.run('CREATE TABLE IF NOT EXISTS tasks(id INTEGER PRIMARY KEY AUTOINCREMENT, description TEXT, completed BOOLEAN, user_id INTEGER, FOREIGN KEY(user_id) REFERENCES users(id))');
});
// 4. AUTHENTICATION MIDDLEWARE
const auth = (req, res, next) => {
const token = req.header('Authorization')?.replace('Bearer ', '');
if (!token) {
return res.status(401).json({ error: 'No token, authorization denied' });
}
try {
const decoded = jwt.verify(token, JWT_SECRET);
req.user = decoded.user;
next();
} catch (err) {
res.status(401).json({ error: 'Token is not valid' });
}
};
// 5. API ROUTES
// --- AUTH ROUTES ---
app.post('/register', async (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
return res.status(400).json({ error: 'Username and password are required' });
}
try {
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);
const sql = 'INSERT INTO users (username, password) VALUES (?, ?)';
db.run(sql, [username, hashedPassword], function(err) {
if (err) {
return res.status(500).json({ error: 'Username already exists' });
}
res.status(201).json({ id: this.lastID, username: username });
});
} catch (error) {
res.status(500).json({ error: 'Server error during registration' });
}
});
app.post('/login', (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
return res.status(400).json({ error: 'Username and password are required' });
}
const sql = 'SELECT * FROM users WHERE username = ?';
db.get(sql, [username], async (err, user) => {
if (err || !user) {
return res.status(400).json({ error: 'Invalid credentials' });
}
const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch) {
return res.status(400).json({ error: 'Invalid credentials' });
}
const payload = { user: { id: user.id } };
jwt.sign(payload, JWT_SECRET, { expiresIn: '1h' }, (err, token) => {
if (err) throw err;
res.json({ token });
});
});
});
// --- TASK ROUTES (PROTECTED & AUTHORIZED) ---
// GET: Fetch tasks for the logged-in user
app.get('/tasks', auth, (req, res) => {
const sql = "SELECT * FROM tasks WHERE user_id = ? ORDER BY id";
db.all(sql, [req.user.id], (err, rows) => {
if (err) {
return res.status(500).json({ error: err.message });
}
res.json(rows);
});
});
// POST: Create a new task for the logged-in user
app.post('/tasks', auth, (req, res) => {
const { description } = req.body;
if (!description) {
return res.status(400).json({ error: 'Description is required' });
}
const sql = 'INSERT INTO tasks (description, completed, user_id) VALUES (?, ?, ?)';
const params = [description, false, req.user.id];
db.run(sql, params, function(err) {
if (err) {
return res.status(500).json({ error: err.message });
}
res.status(201).json({
id: this.lastID,
description: description,
completed: false,
user_id: req.user.id
});
});
});
// PUT: Update a task belonging to the logged-in user
app.put('/tasks/:id', auth, (req, res) => {
const { id } = req.params;
const { completed } = req.body;
if (typeof completed !== 'boolean') {
return res.status(400).json({ error: 'Completed status is required' });
}
const sql = 'UPDATE tasks SET completed = ? WHERE id = ? AND user_id = ?';
const params = [completed, id, req.user.id];
db.run(sql, params, function(err) {
if (err) {
return res.status(500).json({ error: err.message });
}
if (this.changes === 0) {
return res.status(404).json({ error: 'Task not found or user not authorized' });
}
res.status(200).json({ message: 'Task updated successfully' });
});
});
// DELETE: Delete a task belonging to the logged-in user
app.delete('/tasks/:id', auth, (req, res) => {
const { id } = req.params;
const sql = 'DELETE FROM tasks WHERE id = ? AND user_id = ?';
db.run(sql, [id, req.user.id], function(err) {
if (err) {
return res.status(500).json({ error: err.message });
}
if (this.changes === 0) {
return res.status(404).json({ error: 'Task not found or user not authorized' });
}
res.status(200).json({ message: 'Task deleted successfully' });
});
});
// 6. START THE SERVER
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`🚀 Server is running on port ${PORT}`);
});