-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
158 lines (133 loc) · 4.29 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
const cors = require('cors');
const bodyparser = require('body-parser');
const express = require('express');
const app = express();
const mysql = require('mysql2');
const router = express.Router();
const bcrypt = require('bcrypt');
const jwt=require('jsonwebtoken');
app.use(cors());
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(bodyparser.json());
app.use(express.static('public'));
app.get('/signup', (req, res) => {
res.sendFile(__dirname + '/public/signup.html');
});
app.get('/login', (req, res) => {
res.sendFile(__dirname + '/public/login.html');
});
app.get('/dashboard', (req, res) => {
res.sendFile(__dirname + '/public/dashboard.html');
});
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/home.html');
});
const db = mysql.createPool({
host: 'localhost',
user: 'root',
password: 'rakhal18',
database: 'user',
connectionLimit:10,
})
db.getConnection((err, connection) => {
if (err) {
console.error('Error connecting to the database:', err);
return;
}
console.log('Connected to the database');
connection.release();
});
db.query('SELECT * FROM user',(err, res) => {
if (err){
console.error(err);
return;
}
console.log(res);
});
// Assuming you have already established a database connection
router.post('/api/login', (req, res) => {
const { username, password } = req.body;
// Check if the username exists in the database
db.query('SELECT * FROM user WHERE username = ?', [username], (error, results) => {
if (error) {
console.error(error);
res.status(500).json({ message: 'Internal server error' });
return;
}
// If the username does not exist
if (results.length === 0) {
res.status(401).json({ message: 'Invalid credentials' });
return;
}
const user = results[0];
// Compare the provided password with the hashed password stored in the database
bcrypt.compare(password, user.password, (bcryptError, isMatch) => {
if (bcryptError) {
console.error(bcryptError);
res.status(500).json({ message: 'Internal server error' });
return;
}
// If the passwords do not match
if (!isMatch) {
res.status(401).json({ message: 'Invalid credentials' });
return;
}
// If the passwords match, create a JSON Web Token (JWT) for authentication
const payload = {
user_id: user.id,
username: user.username,
};
const secretKey = 'your_secret_key'; // Replace with your own secret key
const options = { expiresIn: '1h' }; // Set the expiration time for the token
jwt.sign(payload, secretKey, options, (jwtError, token) => {
if (jwtError) {
console.error(jwtError);
res.status(500).json({ message: 'Internal server error' });
return;
}
// Return the token to the client
res.json({ token });
});
});
});
});
router.post('/api/signup', (req, res) => {
const { username, password } = req.body;
// Check if the username already exists in the database
db.query('SELECT * FROM user WHERE username = ?', [username], (error, results) => {
if (error) {
console.error(error);
res.status(500).json({ message: 'Internal server error' });
return;
}
// If the username already exists
if (results.length > 0) {
res.status(409).json({ message: 'Username already exists' });
return;
}
// Hash the password before saving it to the database
bcrypt.hash(password, 10, (bcryptError, hashedPassword) => {
if (bcryptError) {
console.error(bcryptError);
res.status(500).json({ message: 'Internal server error' });
return;
}
// Save the user information to the database
const newUser = { username, password: hashedPassword };
db.query('INSERT INTO user SET ?', newUser, (insertError, insertResult) => {
if (insertError) {
console.error(insertError);
res.status(500).json({ message: 'Internal server error' });
return;
}
else{
res.json({ message: 'Signup successful' });}
});
});
});
});
app.use("/",router)
app.listen(3003, () => {
console.log('Server is running on http://localhost:3003');
});