This commit is contained in:
newtbot 2023-12-17 20:56:00 +08:00
commit 7fbd9ae2e7
8 changed files with 541 additions and 293 deletions

30
Sean/inusers.js Normal file
View File

@ -0,0 +1,30 @@
// inusers.js
const express = require('express');
const router = express.Router();
// Middleware to check if the user is authenticated
function isAuthenticated(req, res, next) {
if (req.session && req.session.authenticated) {
return next();
} else {
res.redirect('/login');
}
}
// InUsers route (renders the InUsers tab)
router.get('/', isAuthenticated, (req, res) => {
res.render('inusers');
});
// User Data route
router.get('/userdata', isAuthenticated, (req, res) => {
res.render('user-data');
});
// Edit User Data route
router.get('/edituserdata', isAuthenticated, (req, res) => {
res.render('edit-user-data');
});
module.exports = router;

View File

@ -5,13 +5,14 @@ const bodyParser = require('body-parser');
const app = express();
const PORT = process.env.PORT || 3000;
require('dotenv').config()
require('dotenv').config();
const mysqlConfig = {
host: process.env.host,
user: process.env.user,
password: process.env.password,
database: process.env.database,
timezone: 'Z', // Set the timezone to UTC
};
const mysqlConnection = mysql.createConnection(mysqlConfig);
@ -24,7 +25,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();
@ -33,41 +33,91 @@ function isAuthenticated(req, res, next) {
}
}
// Login route
app.post('/login', (req, res) => {
let { username, password } = req.body;
// Trim whitespace
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'));
@ -75,5 +125,3 @@ app.use(express.static('views'));
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});

30
Sean/views/allusers.ejs Normal file
View File

@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>All Users</title>
<!-- Add any additional styles or dependencies here -->
</head>
<body>
<h2>All Users</h2>
<table>
<thead>
<tr>
<th>Username</th>
<!-- Add additional columns as needed -->
</tr>
</thead>
<tbody>
<% for (let i = 0; i < users.length; i++) { %>
<tr>
<td><%= users[i].username %></td>
<!-- Add additional columns as needed -->
</tr>
<% } %>
</tbody>
</table>
</body>
</html>

View File

@ -1,270 +1,162 @@
<!-- views/home.ejs -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>GeeksForGeeks</title>
<link rel="stylesheet"
href="style.css">
<link rel="stylesheet"
href="responsive.css">
,
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<style>
body {
margin: 0;
font-family: 'Arial', sans-serif;
}
#sidebar {
height: 100%;
width: 250px;
position: fixed;
background-color: #333;
padding-top: 60px;
transition: 0.5s;
}
#sidebar img {
width: 100%;
margin-bottom: 20px;
}
#sidebar a {
padding: 10px 15px;
text-decoration: none;
font-size: 18px;
color: white;
display: block;
transition: 0.3s;
}
#sidebar a:hover {
background-color: #555;
}
#content {
margin-left: 250px;
padding: 16px;
}
@media screen and (max-width: 600px) {
#sidebar {
width: 0;
overflow-x: hidden;
}
#content {
margin-left: 0;
}
}
#sidebarCollapse {
width: 40px;
height: 40px;
position: absolute;
top: 10px;
left: 10px;
cursor: pointer;
z-index: 1;
}
#sidebarCollapse span {
display: block;
background: white;
height: 5px;
width: 30px;
margin: 6px auto;
transition: 0.3s;
}
#sidebarCollapse:hover span:nth-child(1) {
transform: rotate(-45deg) translate(-5px, 6px);
}
#sidebarCollapse:hover span:nth-child(2) {
opacity: 0;
}
#sidebarCollapse:hover span:nth-child(3) {
transform: rotate(45deg) translate(-5px, -6px);
}
table {
border-collapse: collapse;
width: 80%;
margin: 20px 0;
font-size: 16px;
}
th, td {
border: 1px solid #dddddd;
text-align: left;
padding: 12px;
}
th {
background-color: #f2f2f2;
}
tr:hover {
background-color: #f5f5f5;
}
td {
white-space: nowrap;
}
</style>
</head>
<body>
<div id="sidebar">
<img src="LOGO.PNG" alt="Custom Image">
<div id="sidebarCollapse">
<span></span>
<span></span>
<span></span>
</div>
<a href="/inusers">In-House Users</a>
<a href="#">Users</a>
<a href="#">Data Analysis</a>
<a href="#">Logout</a>
</div>
<header>
<div id="content">
<h2>Welcome to the Home Page, <%= username %>!</h2>
<h3>Last 10 Logins:</h3>
<table>
<thead>
<tr>
<th>Username</th>
<th>Last Login Time</th>
</tr>
</thead>
<tbody>
<% loginLogs.forEach(log => { %>
<tr>
<td><%= log.username %></td>
<td><%= new Date(log.lastLogin).toLocaleString('en-US', { timeZone: 'Asia/Singapore' }) %></td>
</tr>
<% }); %>
</tbody>
</table>
<div class="logosec">
<div class="logo">Eco Saver</div>
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20221210182541/Untitled-design-(30).png"
class="icn menuicn"
id="menuicn"
alt="menu-icon">
</div>
<script>
document.getElementById('sidebarCollapse').addEventListener('click', function () {
document.getElementById('sidebar').style.width = (document.getElementById('sidebar').style.width === '250px') ? '0' : '250px';
});
</script>
</div>
<div class="searchbar">
<input type="text"
placeholder="Search">
<div class="searchbtn">
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20221210180758/Untitled-design-(28).png"
class="icn srchicn"
alt="search-icon">
</div>
</div>
<div class="message">
<div class="circle"></div>
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20221210183322/8.png"
class="icn"
alt="">
<div class="dp">
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20221210180014/profile-removebg-preview.png"
class="dpicn"
alt="dp">
</div>
</div>
</header>
<div class="main-container">
<div class="navcontainer">
<nav class="nav">
<div class="nav-upper-options">
<div class="nav-option option1">
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20221210182148/Untitled-design-(29).png"
class="nav-img"
alt="dashboard">
<h3> Dashboard</h3>
</div>
<div class="option2 nav-option">
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20221210183322/9.png"
class="nav-img"
alt="articles">
<h3> Articles</h3>
</div>
<div class="nav-option option3">
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20221210183320/5.png"
class="nav-img"
alt="report">
<h3> Report</h3>
</div>
<div class="nav-option option4">
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20221210183321/6.png"
class="nav-img"
alt="institution">
<h3> Institution</h3>
</div>
<div class="nav-option option5">
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20221210183323/10.png"
class="nav-img"
alt="blog">
<h3> Profile</h3>
</div>
<div class="nav-option option6">
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20221210183320/4.png"
class="nav-img"
alt="settings">
<h3> Settings</h3>
</div>
<div class="nav-option logout">
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20221210183321/7.png"
class="nav-img"
alt="logout">
<h3>Logout</h3>
</div>
</div>
</nav>
</div>
<div class="main">
<div class="searchbar2">
<input type="text"
name=""
id=""
placeholder="Search">
<div class="searchbtn">
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20221210180758/Untitled-design-(28).png"
class="icn srchicn"
alt="search-button">
</div>
</div>
<div class="box-container">
<div class="box box1">
<div class="text">
<h2 class="topic-heading">60.5k</h2>
<h2 class="topic">Article Views</h2>
</div>
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20221210184645/Untitled-design-(31).png"
alt="Views">
</div>
<div class="box box2">
<div class="text">
<h2 class="topic-heading">150</h2>
<h2 class="topic">Likes</h2>
</div>
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20221210185030/14.png"
alt="likes">
</div>
<div class="box box3">
<div class="text">
<h2 class="topic-heading">320</h2>
<h2 class="topic">Comments</h2>
</div>
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20221210184645/Untitled-design-(32).png"
alt="comments">
</div>
<div class="box box4">
<div class="text">
<h2 class="topic-heading">70</h2>
<h2 class="topic">Published</h2>
</div>
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20221210185029/13.png" alt="published">
</div>
</div>
<div class="report-container">
<div class="report-header">
<h1 class="recent-Articles">Recent Articles</h1>
<button class="view">View All</button>
</div>
<div class="report-body">
<div class="report-topic-heading">
<h3 class="t-op">Article</h3>
<h3 class="t-op">Views</h3>
<h3 class="t-op">Comments</h3>
<h3 class="t-op">Status</h3>
</div>
<div class="items">
<div class="item1">
<h3 class="t-op-nextlvl">Article 73</h3>
<h3 class="t-op-nextlvl">2.9k</h3>
<h3 class="t-op-nextlvl">210</h3>
<h3 class="t-op-nextlvl label-tag">Published</h3>
</div>
<div class="item1">
<h3 class="t-op-nextlvl">Article 72</h3>
<h3 class="t-op-nextlvl">1.5k</h3>
<h3 class="t-op-nextlvl">360</h3>
<h3 class="t-op-nextlvl label-tag">Published</h3>
</div>
<div class="item1">
<h3 class="t-op-nextlvl">Article 71</h3>
<h3 class="t-op-nextlvl">1.1k</h3>
<h3 class="t-op-nextlvl">150</h3>
<h3 class="t-op-nextlvl label-tag">Published</h3>
</div>
<div class="item1">
<h3 class="t-op-nextlvl">Article 70</h3>
<h3 class="t-op-nextlvl">1.2k</h3>
<h3 class="t-op-nextlvl">420</h3>
<h3 class="t-op-nextlvl label-tag">Published</h3>
</div>
<div class="item1">
<h3 class="t-op-nextlvl">Article 69</h3>
<h3 class="t-op-nextlvl">2.6k</h3>
<h3 class="t-op-nextlvl">190</h3>
<h3 class="t-op-nextlvl label-tag">Published</h3>
</div>
<div class="item1">
<h3 class="t-op-nextlvl">Article 68</h3>
<h3 class="t-op-nextlvl">1.9k</h3>
<h3 class="t-op-nextlvl">390</h3>
<h3 class="t-op-nextlvl label-tag">Published</h3>
</div>
<div class="item1">
<h3 class="t-op-nextlvl">Article 67</h3>
<h3 class="t-op-nextlvl">1.2k</h3>
<h3 class="t-op-nextlvl">580</h3>
<h3 class="t-op-nextlvl label-tag">Published</h3>
</div>
<div class="item1">
<h3 class="t-op-nextlvl">Article 66</h3>
<h3 class="t-op-nextlvl">3.6k</h3>
<h3 class="t-op-nextlvl">160</h3>
<h3 class="t-op-nextlvl label-tag">Published</h3>
</div>
<div class="item1">
<h3 class="t-op-nextlvl">Article 65</h3>
<h3 class="t-op-nextlvl">1.3k</h3>
<h3 class="t-op-nextlvl">220</h3>
<h3 class="t-op-nextlvl label-tag">Published</h3>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="index.js"></script>
</body>
</html>

View File

@ -1,28 +1,64 @@
let menuicn = document.querySelector(".menuicn");
let nav = document.querySelector(".navcontainer");
const express = require('express');
const router = express.Router();
const mysql = require('mysql');
menuicn.addEventListener("click", () => {
nav.classList.toggle("navclose");
})
document.addEventListener('DOMContentLoaded', () => {
// Fetch recent user logins from your server
fetch('/api/recentUserLogins')
.then(response => response.json())
.then(userLogins => {
// Populate the recent user logins section
const itemsContainer = document.querySelector('.items');
// Replace with your MySQL connection details
const mysqlConfig = {
host: process.env.host,
user: process.env.user,
password: process.env.password,
database: process.env.database,
timezone: 'Z', // Set the timezone to UTC
};
userLogins.forEach(userLogin => {
const item = document.createElement('div');
item.classList.add('item1');
item.innerHTML = `
<h3 class="t-op-nextlvl">${userLogin.username}</h3>
<h3 class="t-op-nextlvl">${userLogin.name}</h3>
<h3 class="t-op-nextlvl">${userLogin.email}</h3>
<h3 class="t-op-nextlvl label-tag">${userLogin.lastLogin}</h3>
`;
itemsContainer.appendChild(item);
});
})
.catch(error => console.error('Error fetching recent user logins:', error));
const mysqlConnection = mysql.createConnection(mysqlConfig);
// Middleware to check if the user is authenticated
function isAuthenticated(req, res, next) {
if (req.session && req.session.authenticated) {
return next();
} else {
res.redirect('/login');
}
}
// InUsers route (renders the InUsers tab)
router.get('/', isAuthenticated, (req, res) => {
// Fetch all user data from the database
const userDataQuery = 'SELECT * FROM users';
mysqlConnection.query(userDataQuery, (error, userData) => {
if (error) {
console.error('Error fetching user data:', error);
res.status(500).send('Internal Server Error');
return;
}
// Render the inusers page with user data
res.render('inusers', { userData: userData });
});
});
// User Data route
router.get('/userdata', isAuthenticated, (req, res) => {
// Fetch all user data from the database
const userDataQuery = 'SELECT * FROM users';
mysqlConnection.query(userDataQuery, (error, userData) => {
if (error) {
console.error('Error fetching user data:', error);
res.status(500).send('Internal Server Error');
return;
}
// Render the user-data page with user data
res.render('user-data', { userData: userData });
});
});
// Edit User Data route
router.get('/edituserdata', isAuthenticated, (req, res) => {
res.render('edit-user-data');
});
module.exports = router;

208
Sean/views/inusers.ejs Normal file
View File

@ -0,0 +1,208 @@
<!-- views/inusers.ejs -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>In-House Users</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<style>
body {
margin: 0;
font-family: 'Arial', sans-serif;
}
#sidebar {
height: 100%;
width: 250px;
position: fixed;
background-color: #333;
padding-top: 60px;
transition: 0.5s;
}
#sidebar img {
width: 100%;
margin-bottom: 20px;
}
#sidebar a {
padding: 10px 15px;
text-decoration: none;
font-size: 18px;
color: white;
display: block;
transition: 0.3s;
}
#sidebar a:hover {
background-color: #555;
}
#content {
margin-left: 250px;
padding: 16px;
}
@media screen and (max-width: 600px) {
#sidebar {
width: 0;
overflow-x: hidden;
}
#content {
margin-left: 0;
}
}
#sidebarCollapse {
width: 40px;
height: 40px;
position: absolute;
top: 10px;
left: 10px;
cursor: pointer;
z-index: 1;
}
#sidebarCollapse span {
display: block;
background: white;
height: 5px;
width: 30px;
margin: 6px auto;
transition: 0.3s;
}
#sidebarCollapse:hover span:nth-child(1) {
transform: rotate(-45deg) translate(-5px, 6px);
}
#sidebarCollapse:hover span:nth-child(2) {
opacity: 0;
}
#sidebarCollapse:hover span:nth-child(3) {
transform: rotate(45deg) translate(-5px, -6px);
}
/* Add additional styles specific to inusers.ejs below */
#userDataContainer {
margin-top: 20px;
}
table {
border-collapse: collapse;
width: 100%;
margin-top: 20px;
}
th, td {
border: 1px solid #dddddd;
text-align: left;
padding: 12px;
}
th {
background-color: #f2f2f2;
}
tr:hover {
background-color: #f5f5f5;
}
td {
white-space: nowrap;
}
</style>
</head>
<body>
<div id="sidebar">
<img src="LOGO.PNG" alt="Custom Image">
<div id="sidebarCollapse">
<span></span>
<span></span>
<span></span>
</div>
<a href="#" id="userDataLink">User Data</a>
<a href="/inusers/edituserdata">Edit User Data</a>
</div>
<div id="content">
<h2>Welcome to the In-House Users Page</h2>
<div id="userDataContainer">
<h3>All Users</h3>
<table>
<thead>
<tr>
<th>Name</th>
<th>Username</th>
<th>Email</th>
<th>Password</th>
<th>Last Login</th>
<th>Job Title</th>
<!-- Add more table headers as needed -->
</tr>
</thead>
<tbody>
<% if (allUsers && allUsers.length > 0) { %>
<% allUsers.forEach(user => { %>
<tr>
<td><%= user.name %></td>
<td><%= user.username %></td>
<td><%= user.email %></td>
<td><%= user.password %></td>
<td><%= new Date(user.lastLogin).toLocaleString('en-US', { timeZone: 'Asia/Singapore' }) %></td>
<td><%= user.jobTitle %></td>
<!-- Add more table data cells as needed -->
</tr>
<% }); %>
<% } else { %>
<tr>
<td colspan="6">No users available.</td>
</tr>
<% } %>
</tbody>
</table>
</div>
<!-- Additional content for In-House Users page goes here -->
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script>
document.getElementById('sidebarCollapse').addEventListener('click', function () {
document.getElementById('sidebar').style.width = (document.getElementById('sidebar').style.width === '250px') ? '0' : '250px';
});
$(document).ready(function () {
function displayUserData() {
$.ajax({
url: '/inusers/userdata',
method: 'GET',
dataType: 'html',
success: function (data) {
$('#userDataContainer').html(data);
},
error: function (error) {
console.error('Error fetching user data:', error);
// Handle error as needed
}
});
}
// Call the function when the page loads
displayUserData();
});
</script>
</div>
</body>
</html>

4
package-lock.json generated
View File

@ -120,7 +120,7 @@
"integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==",
"dependencies": {
"bytes": "3.1.2",
"content-type": "~1.0.4",
"content-type": "~1.0.5",
"debug": "2.6.9",
"depd": "2.0.0",
"destroy": "1.2.0",
@ -128,7 +128,7 @@
"iconv-lite": "0.4.24",
"on-finished": "2.4.1",
"qs": "6.11.0",
"raw-body": "2.5.1",
"raw-body": "2.5.2",
"type-is": "~1.6.18",
"unpipe": "1.0.0"
},

View File

@ -17,9 +17,13 @@
},
"homepage": "https://github.com/Newtbot/MP#readme",
"dependencies": {
"body-parser": "^1.20.2",
"coap": "^1.3.0",
"dotenv": "^16.3.1",
"ejs": "^3.1.9",
"express": "^4.18.2",
"express-session": "^1.17.3",
"mysql": "^2.18.1",
"mysql2": "^3.6.5",
"sequelize": "^6.35.2",
"validator": "^13.11.0"