-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
140 lines (121 loc) · 4.1 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
const bcrypt = require('bcrypt');
const chalk =require('chalk');
const mysql = require('mysql2');
const express = require('express');
const path = require('path');
const session = require('express-session');
const crypto = require('crypto');
const secret = crypto.randomBytes(32).toString('hex');
const app = express();
const port = 3000;
const saltRounds = 10;
// Middleware setup
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
require('dotenv').config();
app.use(session({
secret: 'process.env.SESSION_SECRET,',
resave: false,
saveUninitialized: true,
}));
app.set('view engine', 'hbs');
// MySQL connection
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '12345',
database: 'register'
});
// Connection to another database, e.g., 'contacts'
const db2 = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '12345',
database: 'usersquery'
});
db.connect(err => {
if (err) throw err;
console.log(chalk.blue.italic('Connected to MySQL database.'));
});
// Routes
app.get('/', (req, res) => {
res.render('index');
});
app.get('/login', (req, res) => {
res.render('login');
});
app.get('dashboard', (req, res) => {
// Assuming user data is available in req.user (like from a session or authentication middleware)
if (req.user) {
res.render('dashboard', { user: req.user });
} else {
// Handle the case where there is no user data (e.g., redirect to login)
res.redirect('/login');
}
});
app.get('/register', (req, res) => {
res.render('register');
});
app.post('/register', (req, res) => {
const { username, email, password } = req.body;
bcrypt.hash(password, saltRounds, (err, hashedPassword) => {
if (err) {
res.status(500).send('Error hashing password');
return;
}
const query = 'INSERT INTO users (username, email, password) VALUES (?, ?, ?)';
db.execute(query, [username, email, hashedPassword], (err, results) => {
if (err) {
res.status(500).send('Username Must be Unique : Please try again ');
return;
}
res.send('User registered successfully');
});
});
});
app.post('/login', (req, res) => {
const { username, password } = req.body;
const query = 'SELECT * FROM users WHERE username = ?';
db.execute(query, [username], (err, results) => {
if (err) {
res.status(500).send('Database query error');
return;
}
if (results.length > 0) {
const user = results[0];
bcrypt.compare(password, user.password, (err, isMatch) => {
if (err) {
res.status(500).send('Error comparing passwords');
return;
}
if (isMatch) {
console.log(user);
req.session.user = user;
res.redirect('/dashboard');
} else {
res.status(401).send('Invalid credentials');
}
});
} else {
res.status(401).send('Invalid credentials');
}
});
});
const dashboardRouter = require('./routes/dashboard');
app.use('/', dashboardRouter);
// POST route for handling contact form submissions
app.post('/contact', (req, res) => {
const { name, email, message } = req.body;
const query = 'INSERT INTO contacts (name, email, message) VALUES (?, ?, ?)';
db2.query(query, [name, email, message], (err, result) => {
if (err) {
console.error('Error saving contact information:', err);
return res.status(500).send('Server error, please try again later.');
}
res.send('Thank you for your message!');
});
});
app.listen(port, () => {
console.log(chalk.green.bold.inverse(`Server is running at http://localhost:${port}`));
});