From 2c62a2f0c09cd4de71cfeb83d26d87ab05e87a33 Mon Sep 17 00:00:00 2001
From: BIG2EYEZ
Date: Mon, 22 Jan 2024 18:07:44 +0800
Subject: [PATCH 01/33] csp removed sensor wip
---
Sean/server.js | 39 +++++-----
Sean/views/home.ejs | 3 +-
Sean/views/inusers.ejs | 20 +++--
Sean/views/location.js | 92 ++++++++++++++++++++++
Sean/views/locations.ejs | 73 ++++++++++++++++++
Sean/views/sensor.js | 160 +++++++++++++++++++++++++++++++++++++++
Sean/views/sensors.ejs | 87 +++++++++++++++++++++
Sean/views/style.css | 1 +
8 files changed, 445 insertions(+), 30 deletions(-)
create mode 100644 Sean/views/location.js
create mode 100644 Sean/views/locations.ejs
create mode 100644 Sean/views/sensor.js
create mode 100644 Sean/views/sensors.ejs
diff --git a/Sean/server.js b/Sean/server.js
index 278a374..38f9b31 100644
--- a/Sean/server.js
+++ b/Sean/server.js
@@ -10,17 +10,13 @@ const otpGenerator = require('otp-generator');
const { body, validationResult } = require('express-validator');
const validator = require('validator');
const { format } = require('date-fns');
-const helmet = require('helmet');
+
const { Sequelize } = require('sequelize');
const { transporter } = require("./modules/nodeMailer");
const { sequelize, User } = require("./modules/mysql");
const userLogs= require('./models/userLogs')(sequelize); // Adjust the path based on your project structure
const app = express();
-const nonce = crypto.randomBytes(16).toString('base64');
-
-console.log('Nonce:', nonce);
-
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser());
@@ -30,17 +26,7 @@ require("dotenv").config();
app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "ejs");
-app.use(
- helmet.contentSecurityPolicy({
- directives: {
- defaultSrc: ["'self'",`'nonce-${nonce}'`],
- scriptSrc: ["'self'",`'nonce-${nonce}'`,"'strict-dynamic'", 'cdn.jsdelivr.net', 'fonts.googleapis.com', 'stackpath.bootstrapcdn.com', 'code.jquery.com', 'cdnjs.cloudflare.com'],
- styleSrc: ["'self'",`'nonce-${nonce}'`, 'cdn.jsdelivr.net', 'fonts.googleapis.com'],
- imgSrc: ["'self'"],
- fontSrc: ["'self'", 'fonts.gstatic.com'],
- },
- })
- );
+
app.use(session({
secret: process.env.key,
@@ -294,9 +280,8 @@ app.post("/verify-otp", [
});
const currentUsername = req.session.username;
-
// Render the inusers page with JSON data
- res.render("inusers", { nonce: nonce, allUsers, csrfToken: csrfTokenSession, currentUsername });
+ res.render("inusers", {allUsers, csrfToken: csrfTokenSession, currentUsername });
} catch (error) {
console.error("Error fetching all users:", error);
res.status(500).send("Internal Server Error");
@@ -814,6 +799,24 @@ app.get('/api/getLogs', async (req, res) => {
}
});
+app.get("/locations", isAuthenticated, async (req, res) => {
+ try {
+ // Render the inusers page with JSON data
+ res.render("locations");
+ } catch (error) {
+ console.error("Error fetching all users:", error);
+ res.status(500).send("Internal Server Error");
+ }
+});
+app.get("/sensors", isAuthenticated, async (req, res) => {
+ try {
+ // Render the inusers page with JSON data
+ res.render("sensors");
+ } catch (error) {
+ console.error("Error fetching all users:", error);
+ res.status(500).send("Internal Server Error");
+ }
+});
app.use(express.static("views"));
app.listen(PORT, () => {
diff --git a/Sean/views/home.ejs b/Sean/views/home.ejs
index 31a082a..4d3222d 100644
--- a/Sean/views/home.ejs
+++ b/Sean/views/home.ejs
@@ -20,7 +20,8 @@
diff --git a/Sean/views/inusers.ejs b/Sean/views/inusers.ejs
index 1975b70..2dea943 100644
--- a/Sean/views/inusers.ejs
+++ b/Sean/views/inusers.ejs
@@ -173,21 +173,19 @@
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ ID |
+ Location |
+ Descriptions |
+
+
+
+
+
+
+
+
+
+
+
+ Welcome to the Location
+
+
+
+
diff --git a/Sean/views/location.js b/Sean/views/location.js
new file mode 100644
index 0000000..79db94e
--- /dev/null
+++ b/Sean/views/location.js
@@ -0,0 +1,92 @@
+$(document).ready(function () {
+ // Function to fetch and display locations
+ function fetchLocations() {
+ // Make a GET request to retrieve all locations
+ fetch('/api/v0/location', {
+ method: 'GET',
+ headers: {
+ 'Authorization': '2-eb0c08b0-250a-4249-8a87-11141e2ff8fb'
+ },
+ })
+ .then(response => {
+ if (response.ok) {
+ return response.json();
+ } else {
+ throw new Error(`HTTP error! Status: ${response.status}`);
+ }
+ })
+ .then(locations => {
+ // Clear existing table rows
+ $('#locationTableBody').empty();
+
+ // Populate the table with location information
+ locations.forEach(location => {
+ $('#locationTableBody').append(`
+
+ `);
+ });
+ })
+ .catch(error => {
+ console.error('Error fetching locations:', error);
+ // Handle error as needed
+ });
+ }
+ // Call the fetchLocations function when the page loads
+ fetchLocations();
+});
+
+$(document).ready(function () {
+ $('#allLocationLink').on('click', function () {
+ $('#locationContainer').show();
+ $('#createLocationForm').hide();
+ });
+ $('#addLocationLink').on('click', function () {
+ $('#locationContainer').hide();
+ $('#createLocationForm').show();
+ });
+ });
+
+
+$('#locationForm').on('submit', function (e) {
+ e.preventDefault();
+
+ const location = $('#location').val();
+ const user = req.session.jobTitle
+ const description = $('#description').val();
+
+ fetch('/api/v0/location/new', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'Authorization': '2-eb0c08b0-250a-4249-8a87-11141e2ff8fb'
+ },
+ body: JSON.stringify({
+ name: location,
+ added_by: user,
+ description: description
+ }),
+ })
+ .then(response => {
+ if (response.ok) {
+ // Status 201 indicates successful creation
+ return response.json();
+ } else {
+ return response.json().then(data => {
+ throw new Error(data.message || `HTTP error! Status: ${response.status}`);
+ });
+ }
+})
+.then(data => {
+ console.log(`Location added successfully. Message: ${data.message}`);
+ alert('Location added successfully!');
+ resetFormFields();
+})
+.catch(error => {
+ console.error('Location not added successfully', error);
+ // Handle error as needed
+});
+ });
diff --git a/Sean/views/locations.ejs b/Sean/views/locations.ejs
new file mode 100644
index 0000000..63eb54d
--- /dev/null
+++ b/Sean/views/locations.ejs
@@ -0,0 +1,73 @@
+
+
+
+
+
+