update
This commit is contained in:
@ -5,7 +5,7 @@ const bodyParser = require('body-parser');
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
require('dotenv').config()
|
||||
require('dotenv').config();
|
||||
|
||||
// MySQL setup (replace with your MySQL connection details)
|
||||
const mysqlConfig = {
|
||||
@ -13,6 +13,7 @@ const mysqlConfig = {
|
||||
user: process.env.user,
|
||||
password: process.env.password,
|
||||
database: process.env.database,
|
||||
timezone: 'Z', // Set the timezone to UTC
|
||||
};
|
||||
|
||||
const mysqlConnection = mysql.createConnection(mysqlConfig);
|
||||
@ -25,7 +26,6 @@ app.get('/login', (req, res) => {
|
||||
res.render('login');
|
||||
});
|
||||
|
||||
// Check if the user is authenticated before accessing certain routes
|
||||
function isAuthenticated(req, res, next) {
|
||||
if (req.session && req.session.authenticated) {
|
||||
return next();
|
||||
@ -34,41 +34,91 @@ function isAuthenticated(req, res, next) {
|
||||
}
|
||||
}
|
||||
|
||||
// Login route
|
||||
app.post('/login', (req, res) => {
|
||||
let { username, password } = req.body;
|
||||
|
||||
// Trim leading and trailing spaces from username
|
||||
username = username.trim();
|
||||
|
||||
// Validate username and password against MySQL
|
||||
const sql = 'SELECT * FROM users WHERE username = ? AND password = ?';
|
||||
mysqlConnection.query(sql, [username, password], (error, results) => {
|
||||
const loginSql = 'SELECT * FROM users WHERE username = ? AND password = ?';
|
||||
const updateLastLoginSql = 'UPDATE users SET lastLogin = CURRENT_TIMESTAMP WHERE username = ?';
|
||||
|
||||
// Check credentials and retrieve user information
|
||||
const connection = mysql.createConnection(mysqlConfig);
|
||||
|
||||
connection.connect();
|
||||
|
||||
connection.query(loginSql, [username, password], (error, results) => {
|
||||
if (error) {
|
||||
console.error('Error executing login query:', error);
|
||||
res.status(500).send('Internal Server Error');
|
||||
connection.end(); // Close the connection in case of an error
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('SQL Query:', sql, [username, password]);
|
||||
console.log('Query Results:', results);
|
||||
|
||||
if (results.length === 0) {
|
||||
res.status(401).send('Invalid username or password');
|
||||
connection.end(); // Close the connection when not needed anymore
|
||||
} else {
|
||||
// Set session data for authentication
|
||||
req.session.authenticated = true;
|
||||
req.session.username = username;
|
||||
// Update lastLogin field for the user
|
||||
connection.query(updateLastLoginSql, [username], (updateError, updateResults) => {
|
||||
if (updateError) {
|
||||
console.error('Error updating lastLogin:', updateError);
|
||||
res.status(500).send('Internal Server Error');
|
||||
connection.end(); // Close the connection in case of an error
|
||||
return;
|
||||
}
|
||||
|
||||
// Redirect to the home page or any other protected route
|
||||
res.redirect('/home');
|
||||
// Check if the update affected any rows
|
||||
if (updateResults.affectedRows > 0) {
|
||||
// Set session data for authentication
|
||||
req.session.authenticated = true;
|
||||
req.session.username = username;
|
||||
|
||||
// Redirect to the home page or any other protected route
|
||||
res.redirect('/home');
|
||||
} else {
|
||||
res.status(500).send('Error updating lastLogin. No rows affected.');
|
||||
}
|
||||
|
||||
connection.end(); // Close the connection when not needed anymore
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Home route (protected by authentication)
|
||||
// Update your /home route to retrieve the overall last 10 logins for all users
|
||||
app.get('/home', isAuthenticated, (req, res) => {
|
||||
res.render('home', { username: req.session.username });
|
||||
// Retrieve the overall last 10 logins for all users
|
||||
const loginsQuery = 'SELECT username, lastLogin FROM users ORDER BY lastLogin DESC LIMIT 10';
|
||||
|
||||
mysqlConnection.query(loginsQuery, (error, loginResults) => {
|
||||
if (error) {
|
||||
console.error('Error executing login logs query:', error);
|
||||
res.status(500).send('Internal Server Error');
|
||||
return;
|
||||
}
|
||||
|
||||
// Log the results on the server side
|
||||
console.log('Login Logs on Server:', loginResults);
|
||||
|
||||
// Render the home page with login logs data
|
||||
res.render('home', { username: req.session.username, loginLogs: loginResults });
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/inusers', isAuthenticated, (req, res) => {
|
||||
// Fetch all user data from the database
|
||||
const allUsersQuery = 'SELECT * FROM users';
|
||||
|
||||
mysqlConnection.query(allUsersQuery, (error, allUsers) => {
|
||||
if (error) {
|
||||
console.error('Error fetching all users:', error);
|
||||
res.status(500).send('Internal Server Error');
|
||||
return;
|
||||
}
|
||||
|
||||
// Render the inusers page with all user data
|
||||
res.render('inusers', { allUsers: allUsers });
|
||||
});
|
||||
});
|
||||
|
||||
app.use(express.static('views'));
|
||||
@ -76,5 +126,3 @@ app.use(express.static('views'));
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server is running on port ${PORT}`);
|
||||
});
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user