-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
100 lines (82 loc) · 3.03 KB
/
server.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
const express = require('express');
const mysql = require('mysql');
const bcrypt = require('bcryptjs');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'Hoangson09112004@',
database: 'mySQL'
});
db.connect((err) => {
if (err) {
console.error('Error connecting to the database:', err);
return;
}
console.log('Connected to the database.');
});
app.post('/register', async (req, res) => {
const { username, password } = req.body;
const hashedPassword = await bcrypt.hash(password, 10);
const query = 'INSERT INTO users (username, password) VALUES (?, ?)';
db.query(query, [username, hashedPassword], (err, result) => {
if (err) {
console.error('Error inserting user:', err);
res.status(500).send('Server error');
return;
}
res.status(200).send('User registered');
});
});
app.post('/login', (req, res) => {
const { username, password } = req.body;
const query = 'SELECT * FROM users WHERE username = ?';
db.query(query, [username], async (err, results) => {
if (err) {
console.error('Error fetching user:', err);
res.status(500).send('Server error');
return;
}
if (results.length === 0) {
res.status(401).send('Invalid username or password');
return;
}
const user = results[0];
const passwordValid = await bcrypt.compare(password, user.password);
if (!passwordValid) {
res.status(401).send('Invalid username or password');
return;
}
res.status(200).send('Login successful');
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
app.post('/register', async (req, res) => {
const { username, password, bio, avatar } = req.body;
const hashedPassword = await bcrypt.hash(password, 10);
const createUserQuery = 'INSERT INTO users (username, password) VALUES (?, ?)';
const createProfileQuery = 'INSERT INTO profiles (user_id, bio, avatar) VALUES (?, ?, ?)';
db.beginTransaction(async (err) => {
if (err) {
console.error('Error starting transaction:', err);
res.status(500).send('Server error');
return;
}
try {
const [userResult] = await db.query(createUserQuery, [username, hashedPassword]);
await db.query(createProfileQuery, [userResult.insertId, bio, avatar]);
db.commit();
res.status(200).send('User registered');
} catch (err) {
console.error('Error inserting user and profile:', err);
db.rollback();
res.status(500).send('Server error');
}
});
});