diff --git a/Sean/inusers.js b/Sean/inusers.js new file mode 100644 index 0000000..a2f8062 --- /dev/null +++ b/Sean/inusers.js @@ -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; diff --git a/Sean/server.js b/Sean/server.js index 7b77c86..e5e3d2b 100644 --- a/Sean/server.js +++ b/Sean/server.js @@ -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}`); }); - - diff --git a/Sean/views/allusers.ejs b/Sean/views/allusers.ejs new file mode 100644 index 0000000..10b46ae --- /dev/null +++ b/Sean/views/allusers.ejs @@ -0,0 +1,30 @@ + + + + + + + + All Users + + + +

All Users

+ + + + + + + + + <% for (let i = 0; i < users.length; i++) { %> + + + + + <% } %> + +
Username
<%= users[i].username %>
+ + diff --git a/Sean/views/home.ejs b/Sean/views/home.ejs index ce02120..c54445f 100644 --- a/Sean/views/home.ejs +++ b/Sean/views/home.ejs @@ -1,270 +1,162 @@ - - + - - - - - GeeksForGeeks - - - , - + + - - + + + + Home + + + -
-
-

150

-

Likes

-
+ - likes -
+ -
-
-

320

-

Comments

-
+
+

Welcome to the Home Page, <%= username %>!

+

Last 10 Logins:

+ + + + + + + + + <% loginLogs.forEach(log => { %> + + + + + <% }); %> + +
UsernameLast Login Time
<%= log.username %><%= new Date(log.lastLogin).toLocaleString('en-US', { timeZone: 'Asia/Singapore' }) %>
- comments -
+ +
-
-
-

70

-

Published

-
+ - published -
- - -
-
-

Recent Articles

- -
- -
-
-

Article

-

Views

-

Comments

-

Status

-
- -
-
-

Article 73

-

2.9k

-

210

-

Published

-
- -
-

Article 72

-

1.5k

-

360

-

Published

-
- -
-

Article 71

-

1.1k

-

150

-

Published

-
- -
-

Article 70

-

1.2k

-

420

-

Published

-
- -
-

Article 69

-

2.6k

-

190

-

Published

-
- -
-

Article 68

-

1.9k

-

390

-

Published

-
- -
-

Article 67

-

1.2k

-

580

-

Published

-
- -
-

Article 66

-

3.6k

-

160

-

Published

-
- -
-

Article 65

-

1.3k

-

220

-

Published

-
- -
-
-
- - - - - diff --git a/Sean/views/index.js b/Sean/views/index.js index bab9fa2..5ee6953 100644 --- a/Sean/views/index.js +++ b/Sean/views/index.js @@ -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'); - - userLogins.forEach(userLogin => { - const item = document.createElement('div'); - item.classList.add('item1'); - item.innerHTML = ` -

${userLogin.username}

-

${userLogin.name}

-

${userLogin.email}

-

${userLogin.lastLogin}

- `; - itemsContainer.appendChild(item); - }); - }) - .catch(error => console.error('Error fetching recent user logins:', error)); - }); \ No newline at end of file +// 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 +}; + +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; diff --git a/Sean/views/inusers.ejs b/Sean/views/inusers.ejs new file mode 100644 index 0000000..f7a4ffd --- /dev/null +++ b/Sean/views/inusers.ejs @@ -0,0 +1,208 @@ + + + + + + + + + In-House Users + + + + + + + + +
+

Welcome to the In-House Users Page

+ +
+

All Users

+ + + + + + + + + + + + + + <% if (allUsers && allUsers.length > 0) { %> + <% allUsers.forEach(user => { %> + + + + + + + + + + <% }); %> + <% } else { %> + + + + <% } %> + +
NameUsernameEmailPasswordLast LoginJob Title
<%= user.name %><%= user.username %><%= user.email %><%= user.password %><%= new Date(user.lastLogin).toLocaleString('en-US', { timeZone: 'Asia/Singapore' }) %><%= user.jobTitle %>
No users available.
+
+ + + + + + +
+ + + + diff --git a/package-lock.json b/package-lock.json index 6f14034..fdd1b5b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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" }, diff --git a/package.json b/package.json index e28763b..e42c589 100644 --- a/package.json +++ b/package.json @@ -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"