merged
This commit is contained in:
commit
7bdfb02f9a
@ -1,9 +1,10 @@
|
||||
const { run } = require("./modules/IoT-sensor");
|
||||
const { IoTdataGenerator } = require("./modules/IoT-sensor");
|
||||
const client = require("./modules/mqtt");
|
||||
|
||||
async function publishData() {
|
||||
try {
|
||||
const data = await run();
|
||||
let iothub = new IoTdataGenerator();
|
||||
let data = await iothub.generateData();
|
||||
console.log(data);
|
||||
client.publish("iot-data", JSON.stringify(data));
|
||||
} catch (err) {
|
||||
|
@ -18,19 +18,19 @@ class IoTdataGenerator {
|
||||
async generateData() {
|
||||
try {
|
||||
const { loc, sen } = await this.getLocationAndSensorId();
|
||||
const dataAray = []; // create a new array for each call
|
||||
|
||||
for (let i = 0; i < sen.length; i++) {
|
||||
//console.log(sen[i].id);
|
||||
//console.log(loc[i].id);
|
||||
//console.log("you should appear 6 times only")
|
||||
dataAray.push(firstDataRow(sen[i].id, loc[i].id));
|
||||
}
|
||||
}
|
||||
|
||||
return dataAray;
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
return dataAray;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//helper function to generate random data
|
||||
function firstDataRow(sensorId, locationId) {
|
||||
@ -57,15 +57,7 @@ function firstDataRow(sensorId, locationId) {
|
||||
2) loop through each sensor id and location id and generate random data and pass to mqtt
|
||||
*/
|
||||
|
||||
async function run() {
|
||||
let iotData = new IoTdataGenerator();
|
||||
const result = await iotData.generateData();
|
||||
console.log(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
module.exports = { run };
|
||||
module.exports = { IoTdataGenerator };
|
||||
|
||||
|
||||
|
||||
|
4
api.MD
4
api.MD
@ -169,9 +169,7 @@ http://localhost/api/v0/sensor-data/filter?windspeed=highest&limit=1
|
||||
http://localhost/api/v0/sensor-data/data?week=1&sensorid=1&locationid=1&page=2&pagesize=10
|
||||
|
||||
|
||||
|
||||
|
||||
curl localhost/api/v0/user/register -H "Content-Type: application/json" -X POST -d '{"username": "testuser123", "password": "thisisthesystemuserpasswordnoob", "email": "testuser123@ecosaver.com", "address": "Nanyang Polytechnic 180 Ang Mo Kio Avenue 8 Singapore 569830", "phone": "12345678"}'
|
||||
|
||||
|
||||
curl localhost:3000/api/v0/apikey/new -H "Content-Type: application/json" -X POST -d '{"userid": "5", "permission": "canRead"}'
|
||||
curl localhost:3000/api/v0/token/new -H "Content-Type: application/json" -X POST -d '{"userid": "5", "permission": "canRead"}'
|
||||
|
@ -7,6 +7,8 @@ const ejs = require("ejs");
|
||||
|
||||
module.exports = app;
|
||||
|
||||
process.nextTick(() => require("./mqttApp"));
|
||||
|
||||
app.use(express.json());
|
||||
app.set("json spaces", 2);
|
||||
|
||||
@ -14,12 +16,16 @@ app.set("json spaces", 2);
|
||||
const limiter = rateLimit({
|
||||
windowMs: 15 * 60 * 1000, // 15 minutes
|
||||
limit: 600, // Limit each IP to 100 requests per `window` (here, per 15 minutes).
|
||||
standardHeaders: 'draft-7', // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
|
||||
standardHeaders: "draft-7", // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
|
||||
legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
|
||||
});
|
||||
|
||||
// Hold list of functions to run when the server is ready
|
||||
app.onListen = [function(){console.log('Express is ready')}];
|
||||
app.onListen = [
|
||||
function () {
|
||||
console.log("Express is ready");
|
||||
},
|
||||
];
|
||||
|
||||
// Apply the rate limiting middleware to all requests.
|
||||
app.use(limiter);
|
||||
@ -34,10 +40,9 @@ app.set("view engine", "ejs");
|
||||
// Have express server static content( images, CSS, browser JS) from the public
|
||||
app.use(express.static(path.join(__dirname, "./public")));
|
||||
|
||||
|
||||
//route logic
|
||||
app.use("/api/seed/v0" ,require("./routes/seed_route.js"));
|
||||
app.use("/api/v0", require("./routes/api_routes"));
|
||||
app.use("/api/seed/v0", require("./routes/seed_route.js"));
|
||||
app.use("/api/v0", require("./routes/api_routes"));
|
||||
|
||||
//render logic
|
||||
app.use("/", require("./routes/render"));
|
||||
@ -45,19 +50,17 @@ app.use("/", require("./routes/render"));
|
||||
// Catch 404 and forward to error handler. If none of the above routes are
|
||||
// used, this is what will be called.
|
||||
app.use(function (req, res, next) {
|
||||
if (req.is("application/json")) {
|
||||
var err = new Error("Not Found");
|
||||
err.message = "Page not found";
|
||||
err.status = 404;
|
||||
next(err);
|
||||
}
|
||||
else{
|
||||
//application/json; charset=utf-8
|
||||
if (req.is("application/json" || "application/json; charset=utf-8")) {
|
||||
var err = new Error("Not Found");
|
||||
err.message = "Page not found";
|
||||
err.status = 404;
|
||||
next(err);
|
||||
} else {
|
||||
res.status(404).render("404");
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
// Error handler. This is where `next()` will go on error
|
||||
app.use(function (err, req, res, next) {
|
||||
console.error(err.status || res.status, err.name, req.method, req.url);
|
||||
@ -76,14 +79,20 @@ app.use(function (err, req, res, next) {
|
||||
keyErrors[item.path] = item.message;
|
||||
}
|
||||
}
|
||||
res.status = 422;
|
||||
}
|
||||
|
||||
if (![404, 422].includes(err.status || res.status)) {
|
||||
console.error(err.message);
|
||||
console.error(err.stack);
|
||||
console.error("=========================================");
|
||||
}
|
||||
|
||||
|
||||
res.status(err.status || 500);
|
||||
console.log(keyErrors);
|
||||
res.json({
|
||||
name: err.name,
|
||||
name: err.name || "Unknown error",
|
||||
message: err.message,
|
||||
keyErrors,
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -5,7 +5,6 @@
|
||||
*/
|
||||
|
||||
const app = require('../app');
|
||||
// const mqttApp = require('../mqttApp');
|
||||
const debug = require('debug')('proxy-api:server');
|
||||
const http = require('http');
|
||||
const path = require('path');
|
||||
|
@ -4,8 +4,8 @@ const { sequelize } = require("../mySQL");
|
||||
const { userModel } = require("./userModel");
|
||||
|
||||
//sequelize.sync();
|
||||
const apikeyModel = sequelize.define(
|
||||
"apikey",
|
||||
const tokenModel = sequelize.define(
|
||||
"token",
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
@ -28,7 +28,7 @@ const apikeyModel = sequelize.define(
|
||||
key: "id",
|
||||
},
|
||||
},
|
||||
apikey: {
|
||||
token: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
length: 255,
|
||||
@ -45,7 +45,14 @@ const apikeyModel = sequelize.define(
|
||||
validate: {
|
||||
notEmpty: true,
|
||||
len: [1, 255],
|
||||
isIn: [["canRead", "canWrite" , "auto-generated"]],
|
||||
isIn: [["canRead", "canWrite",]],
|
||||
},
|
||||
},
|
||||
expiration: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: false,
|
||||
validate: {
|
||||
isDate: true,
|
||||
},
|
||||
},
|
||||
createdAt: {
|
||||
@ -61,9 +68,9 @@ const apikeyModel = sequelize.define(
|
||||
timestamps: true,
|
||||
}
|
||||
);
|
||||
apikeyModel.belongsTo(userModel);
|
||||
tokenModel.belongsTo(userModel);
|
||||
|
||||
module.exports = { apikeyModel };
|
||||
module.exports = { tokenModel };
|
||||
|
||||
/*
|
||||
class AuthToken extends Model {
|
@ -1,5 +1,5 @@
|
||||
const { hash, compareHash } = require("./bcrypt.js");
|
||||
const { apikeyModel } = require("../database/model/apiKeyModel");
|
||||
const { tokenModel } = require("../database/model/tokenModel.js");
|
||||
const { generateUUID } = require("./generateUUID.js");
|
||||
|
||||
/*
|
||||
@ -11,37 +11,38 @@ const { generateUUID } = require("./generateUUID.js");
|
||||
6) store in database
|
||||
*/
|
||||
//can be used for api key or token. Both are the same logic
|
||||
async function addAPIKey(userId, permission) {
|
||||
let hashtoken = await generateUUID();
|
||||
let apikey = await hash(hashtoken);
|
||||
async function addToken(userId, permission , expiry) {
|
||||
let uuid = await generateUUID();
|
||||
let hashtoken = await hash(uuid);
|
||||
|
||||
let token = await apikeyModel.create({
|
||||
let token = await tokenModel.create({
|
||||
userid: userId,
|
||||
apikey: apikey,
|
||||
token: hashtoken,
|
||||
permission: permission,
|
||||
expiration: expiry,
|
||||
});
|
||||
|
||||
//user token with - tokenid is table id
|
||||
return token.id + "-" + hashtoken;
|
||||
return token.id + "-" + uuid;
|
||||
}
|
||||
|
||||
async function checkAPikey(SuppliedKey, rowid) {
|
||||
async function checkToken(Supplied, rowid) {
|
||||
try {
|
||||
const retrivedKey = await apikeyModel.findOne({
|
||||
const retrivedToken = await tokenModel.findOne({
|
||||
raw: true,
|
||||
attributes: ["apikey", "permission"],
|
||||
attributes: ["token", "permission"],
|
||||
where: {
|
||||
id: rowid,
|
||||
},
|
||||
});
|
||||
//console.log(retrivedKey.apikey);
|
||||
if (compareHash(SuppliedKey, retrivedKey.apikey)) {
|
||||
if (compareHash(Supplied, retrivedToken.token)) {
|
||||
//return true;
|
||||
return retrivedKey.permission;
|
||||
return retrivedToken.permission;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { addAPIKey , checkAPikey };
|
||||
module.exports = { addToken , checkToken };
|
20
consumerWebsite/functions/isValid.js
Normal file
20
consumerWebsite/functions/isValid.js
Normal file
@ -0,0 +1,20 @@
|
||||
const moment = require("moment");
|
||||
const currentTime = moment().format("YYYY-MM-DD HH:mm:ss");
|
||||
|
||||
//time is taken from the token
|
||||
function isValid(time){
|
||||
const timeDiff = moment(currentTime).diff(time, "minutes");
|
||||
|
||||
if (timeDiff > 1) {
|
||||
console.log(timeDiff);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
module.exports = { isValid };
|
@ -6,7 +6,6 @@ async function getLocation() {
|
||||
}
|
||||
|
||||
async function addLocation(name, added_by, description) {
|
||||
console.log(name, added_by, description);
|
||||
const location = await locationModel.create({
|
||||
name: name,
|
||||
added_by: added_by,
|
||||
@ -30,7 +29,6 @@ async function updateLocation(id, name, added_by, description) {
|
||||
}
|
||||
|
||||
async function deleteLocation(id) {
|
||||
//delete by id
|
||||
const location = await locationModel.destroy({
|
||||
where: {
|
||||
id: id,
|
||||
@ -38,6 +36,7 @@ async function deleteLocation(id) {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
async function getLocationById(id) {
|
||||
const location = await locationModel.findAll({
|
||||
where: {
|
||||
|
@ -1,5 +1,4 @@
|
||||
const {sensorModel} = require("../database/model/sensorModel");
|
||||
|
||||
const { sensorModel } = require("../database/model/sensorModel");
|
||||
|
||||
async function getSensor() {
|
||||
const sensor = await sensorModel.findAll();
|
||||
@ -49,12 +48,12 @@ async function updateSensor(
|
||||
async function deleteSensor(id) {
|
||||
//delete by id
|
||||
const sensor = await sensorModel.destroy({
|
||||
//cascade delete
|
||||
onDelete: "cascade",
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
async function getSensorById(id) {
|
||||
@ -72,4 +71,4 @@ module.exports = {
|
||||
updateSensor,
|
||||
deleteSensor,
|
||||
getSensorById,
|
||||
};
|
||||
};
|
||||
|
@ -25,7 +25,7 @@ async function addSensorData(id_sensor, id_location, sensordata) {
|
||||
locationid: id_location,
|
||||
measurement: sensordata,
|
||||
});
|
||||
io().emit('sensordata:new', sensorData)
|
||||
io().emit('sensorData:new', sensorData)
|
||||
|
||||
return sensorData;
|
||||
}
|
||||
|
@ -1,13 +1,14 @@
|
||||
const { Op } = require('sequelize')
|
||||
const { hash, compareHash } = require("./bcrypt.js");
|
||||
const { addAPIKey } = require("./api");
|
||||
const { addToken } = require("./api");
|
||||
const { userModel } = require("../database/model/userModel");
|
||||
moment = require('moment')
|
||||
|
||||
|
||||
|
||||
//getuser
|
||||
//api/v0/user/me
|
||||
async function getUserID(userid) {
|
||||
async function getUserByID(userid) {
|
||||
//console.log(userid);
|
||||
//console.log(userid.id);
|
||||
let userRes = await userModel.findByPk(userid.id, {
|
||||
@ -20,28 +21,6 @@ async function getUserID(userid) {
|
||||
return userRes;
|
||||
}
|
||||
|
||||
//register
|
||||
//api/v0/auth/register
|
||||
async function addUser(user) {
|
||||
//hash password
|
||||
let hashed = await hash(user.password);
|
||||
|
||||
const addRes = await userModel.create({
|
||||
firstname: user.firstname,
|
||||
lastname: user.lastname,
|
||||
username: user.username,
|
||||
password: hashed,
|
||||
email: user.email,
|
||||
address: user.address,
|
||||
phone: user.phone,
|
||||
});
|
||||
if (addRes) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//api/v0/auth/register
|
||||
/* Registering new user
|
||||
1) req.body is taken from html form or wtv
|
||||
@ -92,9 +71,9 @@ async function loginUser(user) {
|
||||
if (!match) return false;
|
||||
//console.log('loginUser', userRes.id, userRes.username);
|
||||
|
||||
//generate token
|
||||
let token = await addAPIKey(userRes.id, "auto-generated");
|
||||
|
||||
//generate token and permission and experiation time
|
||||
const currentTime = moment().format('YYYY-MM-DD HH:mm:ss');
|
||||
let token = await addToken(userRes.id , "canRead" , currentTime);
|
||||
return { token: token, userid: userRes.id, username: userRes.username };
|
||||
}
|
||||
|
||||
@ -152,7 +131,7 @@ async function updateProfile(user, body) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getUserID,
|
||||
getUserByID,
|
||||
addUser,
|
||||
loginUser,
|
||||
updateProfile,
|
||||
|
@ -1,65 +0,0 @@
|
||||
const { checkAPikey } = require("../functions/api.js");
|
||||
async function apikeyCheck(req, res, next) {
|
||||
//const authHeader = req.headers.authorization
|
||||
try {
|
||||
let apikey = req.headers.authorization;
|
||||
if (!apikey) {
|
||||
res.status(401).json({
|
||||
message: "No API key was supplied. Invalid request",
|
||||
});
|
||||
//throw new Error("No API key was supplied. Invalid request");
|
||||
} else {
|
||||
//split the string by the -
|
||||
let splitAPIkey = apikey.split("-");
|
||||
let rowid = splitAPIkey[0];
|
||||
|
||||
//rejoin withouth the rowid
|
||||
let SuppliedKey = splitAPIkey.slice(1).join("-");
|
||||
if (checkAPikey(SuppliedKey, rowid)) {
|
||||
//get permission
|
||||
let permission = await checkAPikey(SuppliedKey, rowid);
|
||||
console.log(permission);
|
||||
if (req.method === "GET" && permission === "canRead") {
|
||||
return next();
|
||||
}
|
||||
//['POST', 'PUT', 'PATCH', 'DELETE'].includes(req.method)
|
||||
if (
|
||||
["GET", "POST", "PUT", "DELETE"].includes(req.method) &&
|
||||
permission === "canWrite"
|
||||
) {
|
||||
console.log("write");
|
||||
return next();
|
||||
}
|
||||
//throw status 403
|
||||
res.status(403).json({
|
||||
message:
|
||||
"Your API key does not have the correct permissions to access this resource",
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { apikeyCheck };
|
||||
|
||||
/*
|
||||
//web server microservice
|
||||
1) take user supplied rowid-apikey
|
||||
2) split the string by -
|
||||
3) get the rowid or table id
|
||||
4) get the apikey
|
||||
5) compare the apikey with the one in database
|
||||
6) if match, return true
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
I plan to seed some data in user and api
|
||||
Than use the system info and my API middleware will somehow check the supplied API key and check
|
||||
If it's correct API key and has canWrite perms
|
||||
I allow it to access put and post
|
||||
|
||||
|
||||
*/
|
@ -1,31 +1,68 @@
|
||||
const { apikeyModel } = require("../database/model/apiKeyModel");
|
||||
const { tokenModel } = require("../database/model/tokenModel");
|
||||
const { userModel } = require("../database/model/userModel");
|
||||
const { compareHash } = require("../functions/bcrypt");
|
||||
const { checkToken } = require("../functions/api");
|
||||
const { isValid } = require("../functions/isValid");
|
||||
|
||||
async function auth(req, res, next){
|
||||
try{
|
||||
// let user = await Auth.checkToken({token: req.header('auth-token')});
|
||||
let authToken = req.header('auth-token');
|
||||
let splitAuthToken = authToken.split('-');
|
||||
let rowid = splitAuthToken[0];
|
||||
let suppliedToken = splitAuthToken.slice(1).join('-');
|
||||
|
||||
//get from db
|
||||
let token = await apikeyModel.findByPk(rowid, {include: userModel});
|
||||
if (!token) return false;
|
||||
async function auth(req, res, next) {
|
||||
try {
|
||||
const authToken = req.header("auth-token");
|
||||
if (!authToken) {
|
||||
const error = new Error("No Token key was supplied. Invalid request");
|
||||
throw error;
|
||||
}
|
||||
|
||||
//compare
|
||||
let isMatch = await compareHash(suppliedToken, token.apikey);
|
||||
if (!isMatch) return false;
|
||||
const splitAuthToken = authToken.split("-");
|
||||
const rowid = splitAuthToken[0];
|
||||
const suppliedToken = splitAuthToken.slice(1).join("-");
|
||||
|
||||
//else do logic
|
||||
//pass hashed token to req.token (IMPORTANT ITS NOT PASSED TO CLIENT)
|
||||
req.token = token
|
||||
req.user = await token.getUser(); //taking user seq obj from usermodel
|
||||
next();
|
||||
}catch(error){
|
||||
const token = await tokenModel.findByPk(rowid, { include: userModel });
|
||||
|
||||
if (!token) {
|
||||
const error = new Error("Token key not found. Invalid request");
|
||||
throw error;
|
||||
}
|
||||
|
||||
const isMatch = await compareHash(suppliedToken, token.token);
|
||||
|
||||
console.log(isMatch);
|
||||
if (!isMatch) {
|
||||
const error = new Error("Token key not found. Invalid request");
|
||||
throw error;
|
||||
}
|
||||
//if token is a match
|
||||
req.token = token;
|
||||
req.user = await token.getUser();
|
||||
const permission = await checkToken(suppliedToken, rowid);
|
||||
|
||||
const route = req.originalUrl.split("?")[0]; // Removing query parameters
|
||||
//if route is from user/ and permission is canRead allow it to do CRUD
|
||||
if (route.includes("/user/") && permission === "canRead") {
|
||||
next();
|
||||
}
|
||||
if ((req.method === "GET" && permission === "canRead") || (["GET", "POST", "PUT", "DELETE"].includes(req.method) && permission === "canWrite")) {
|
||||
next();
|
||||
}
|
||||
|
||||
if (!isValid(token.expiration)){
|
||||
req.token.destroy();
|
||||
throw new Error("Token expired");
|
||||
}
|
||||
|
||||
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { auth };
|
||||
module.exports = { auth };
|
||||
|
||||
/*
|
||||
else {
|
||||
const error = new Error("Insufficient permission");
|
||||
error.status = 401;
|
||||
throw error;
|
||||
}
|
||||
|
||||
*/
|
@ -9,8 +9,8 @@ let transporter = nodemailer.createTransport({
|
||||
port: 587,
|
||||
secure: false,
|
||||
auth: {
|
||||
user:
|
||||
pass:
|
||||
user: process.env.euser,
|
||||
pass: process.env.epass
|
||||
},
|
||||
});
|
||||
module.exports = { transporter };
|
@ -165,6 +165,7 @@ app.socket = (function (app) {
|
||||
});
|
||||
return socket;
|
||||
})(app);
|
||||
|
||||
//sensor data
|
||||
app.sensordata = (function (app) {
|
||||
|
||||
@ -183,30 +184,21 @@ app.auth = (function (app) {
|
||||
}
|
||||
|
||||
function isLoggedIn(callback) {
|
||||
|
||||
if (getToken()) {
|
||||
console.log("you shldnt appear at all");
|
||||
return app.api.get("user/me", function (error, data) {
|
||||
console.log(error, data);
|
||||
if (!error) app.auth.user = data;
|
||||
//for navbar to show username
|
||||
if (!location.pathname === "/login")
|
||||
{
|
||||
$.scope.getUsername.update(data);
|
||||
|
||||
}
|
||||
|
||||
//for edit profile to show user details
|
||||
//if not in edit profile page, it will not show
|
||||
if (location.pathname === "/profile") {
|
||||
$.scope.getUserDetails.update(data);
|
||||
}
|
||||
return callback(error, data);
|
||||
});
|
||||
} else {
|
||||
callback(null, false);
|
||||
callback(true);
|
||||
}
|
||||
}
|
||||
|
||||
function logOut(callback) {
|
||||
//call logout route
|
||||
console.log("Logging out");
|
||||
$.ajax({
|
||||
type: "DELETE",
|
||||
url: "/api/v0/user/logout",
|
||||
|
@ -2,42 +2,21 @@
|
||||
const router = require('express').Router();
|
||||
const { auth } = require("../middleware/authChecker")
|
||||
const { APIlogger } = require('../middleware/apiLogger.js');
|
||||
const { apikeyCheck } = require('../middleware/apiKey.js');
|
||||
|
||||
router.use('/auth', require('./auth'));
|
||||
|
||||
router.use('/apikey', require('./apikey'));
|
||||
router.use('/token', [auth, APIlogger], require('./token.js'));
|
||||
|
||||
router.use('/user', [auth, APIlogger], require('./user'));
|
||||
|
||||
//TO REFACTOR INTO ONE MIDDLWARE
|
||||
//location route
|
||||
router.use('/location', [auth, APIlogger], require('./location.js'));
|
||||
|
||||
//location route
|
||||
router.use('/location', [apikeyCheck , APIlogger], require('./location.js'));
|
||||
router.use('/sensor', [auth, APIlogger], require('./sensor.js'));
|
||||
|
||||
//location route
|
||||
router.use('/sensor', [apikeyCheck , APIlogger], require('./sensor.js'));
|
||||
|
||||
//location route
|
||||
router.use('/sensor-data', [apikeyCheck, APIlogger], require('./sensorData.js'));
|
||||
//sensor data route
|
||||
router.use('/sensor-data', [auth, APIlogger], require('./sensorData.js'));
|
||||
|
||||
module.exports = router;
|
||||
|
||||
|
||||
/*
|
||||
|
||||
'use strict';
|
||||
const router = require('express').Router();
|
||||
const { auth } = require("../middleware/authChecker")
|
||||
|
||||
|
||||
router.use('/auth', require('./auth'));
|
||||
|
||||
router.use('/apikey', require('./apikey'));
|
||||
|
||||
router.use('/user', auth ,require('./user'));
|
||||
|
||||
module.exports = router;
|
||||
|
||||
|
||||
*/
|
@ -51,7 +51,6 @@ router.post("/login", async (req, res, next) => {
|
||||
|
||||
//contact
|
||||
//auth/contact
|
||||
|
||||
router.post("/contact", async (req, res, next) => {
|
||||
|
||||
});
|
||||
|
@ -25,7 +25,6 @@ router.get("/", async (req, res, next) => {
|
||||
//add location
|
||||
router.post("/new", async (req, res, next) => {
|
||||
try {
|
||||
console.log(req.body);
|
||||
const { name, added_by, description } = req.body;
|
||||
await addLocation(name, added_by, description);
|
||||
res.sendStatus(200)
|
||||
|
@ -1,42 +1,3 @@
|
||||
/*
|
||||
'use strict';
|
||||
|
||||
var router = require('express').Router();
|
||||
const conf = require('../conf')
|
||||
|
||||
const values ={
|
||||
title: conf.environment !== 'production' ? `<i class="fa-brands fa-dev"></i>` : ''
|
||||
}
|
||||
|
||||
router.get('/', async function(req, res, next) {
|
||||
res.render('runner', {...values});
|
||||
});
|
||||
|
||||
|
||||
router.get('/topics', function(req, res, next) {
|
||||
res.render('topics', {...values});
|
||||
});
|
||||
|
||||
router.get('/chat', function(req, res, next) {
|
||||
res.render('chat', {...values});
|
||||
});
|
||||
|
||||
router.get('/login*', function(req, res, next) {
|
||||
res.render('login', {redirect: req.query.redirect, ...values});
|
||||
});
|
||||
|
||||
router.get('/runner', function(req, res, next) {
|
||||
res.render('runner', {...values});
|
||||
});
|
||||
|
||||
router.get('/worker', function(req, res, next) {
|
||||
res.render('worker', {...values});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
var router = require("express").Router();
|
||||
|
@ -1,4 +1,4 @@
|
||||
const { addAPIKey } = require("../functions/api");
|
||||
const { addToken } = require("../functions/api");
|
||||
|
||||
|
||||
const express = require("express");
|
||||
@ -10,14 +10,13 @@ const router = express.Router();
|
||||
3) hash the api key
|
||||
4) store the api key in database
|
||||
*/
|
||||
//token/new
|
||||
//curl localhost:3000/api/v0/token/new -H "Content-Type: application/json" -X POST -d
|
||||
//'{"userid": "5", "permission": "canRead" ,}'
|
||||
router.post("/new", async (req, res, next) => {
|
||||
try {
|
||||
//curl localhost/api/v0/apikey/new -H "Content-Type: application/json" -X POST -d
|
||||
//'{"userid": 1, "permission": "canWrite"}'
|
||||
const apikey = await addAPIKey(req.body.userid, req.body.permission);
|
||||
//console.log(typeof req.body.userid);
|
||||
//console.log(typeof req.body.permission);
|
||||
res.json({apikey: apikey});
|
||||
const token = await addToken(req.body.userid, req.body.permission , "2204-01-24 07:34:36" );
|
||||
res.json({token: token});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
@ -1,4 +1,4 @@
|
||||
const { getUserID, updateProfile } = require("../functions/user");
|
||||
const { getUserByID, updateProfile } = require("../functions/user");
|
||||
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
@ -7,12 +7,8 @@ const router = express.Router();
|
||||
//getbyid
|
||||
router.get("/me", async function (req, res, next) {
|
||||
try {
|
||||
let user = await getUserID(req.user);
|
||||
if (!user) {
|
||||
let error = new Error("User not found");
|
||||
error.status = 400;
|
||||
return next(error);
|
||||
}
|
||||
let user = await getUserByID(req.user);
|
||||
console.log(user);
|
||||
res.json({
|
||||
user: user,
|
||||
});
|
||||
|
@ -1,4 +1,5 @@
|
||||
<%- include('top') %>
|
||||
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
|
@ -1,41 +1,39 @@
|
||||
<%- include('top') %>
|
||||
|
||||
<style>
|
||||
#sensorDataList{
|
||||
padding-top: 2.5em;
|
||||
}
|
||||
</style>
|
||||
<style>
|
||||
#sensorDataList {
|
||||
padding-top: 2.5em;
|
||||
}
|
||||
</style>
|
||||
|
||||
<ul id="sensorDataList">
|
||||
<li jq-repeat='sensorData'>
|
||||
rowid: {{ id }}
|
||||
sensorId: {{ sensorid }}
|
||||
created: {{ createdAt }}
|
||||
location: {{ locationid }}
|
||||
<br/ >
|
||||
co: {{ measurement.co }}
|
||||
humidity: {{ measurement.humidity }}
|
||||
no2: {{ measurement.no2 }}
|
||||
o3: {{ measurement.o3 }}
|
||||
psi: {{ measurement.psi }}
|
||||
so2: {{ measurement.so2 }}
|
||||
temperature: {{ measurement.temperature }}
|
||||
windspeed: {{ measurement.windspeed }}
|
||||
<hr />
|
||||
</li>
|
||||
<li jq-repeat-defualt='sensorData'>
|
||||
Loading...
|
||||
</li>
|
||||
</ul>
|
||||
<ul id="sensorDataList">
|
||||
<li jq-repeat='sensorData'>
|
||||
rowid: {{ id }}
|
||||
sensorId: {{ sensorid }}
|
||||
created: {{ createdAt }}
|
||||
location: {{ locationid }}
|
||||
<br />
|
||||
co: {{ measurement.co }}
|
||||
humidity: {{ measurement.humidity }}
|
||||
no2: {{ measurement.no2 }}
|
||||
o3: {{ measurement.o3 }}
|
||||
psi: {{ measurement.psi }}
|
||||
so2: {{ measurement.so2 }}
|
||||
temperature: {{ measurement.temperature }}
|
||||
windspeed: {{ measurement.windspeed }}
|
||||
<hr />
|
||||
</li>
|
||||
<li jq-repeat-defualt='sensorData'>
|
||||
Loading...
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<script type="text/javascript">
|
||||
$(document).ready(async function(){
|
||||
$.scope.sensorData.push(...await app.api.get('sensor-data/data?order=DESC&limit=40'));
|
||||
<script type="text/javascript">
|
||||
$(document).ready(async function () {
|
||||
app.api.get('sensor-data/data?order=DESC&limit=40', function(error, data){
|
||||
$.scope.sensorData.push(...data);
|
||||
})
|
||||
})
|
||||
|
||||
app.socket.on('sensordata:new', function(data){
|
||||
$.scope.sensorData.unshift(data);
|
||||
});
|
||||
})
|
||||
|
||||
</script>
|
||||
<%- include('bot') %>
|
||||
</script>
|
||||
<%- include('bot') %>
|
@ -56,7 +56,13 @@
|
||||
$(document).ready(function () {
|
||||
//check if user is logged in
|
||||
app.auth.isLoggedIn(function (error, data) {
|
||||
if (data) {
|
||||
if (!error) {
|
||||
console.log(error);
|
||||
$.scope.getUsername.update(data);
|
||||
|
||||
if (location.pathname == "/profile") {
|
||||
$.scope.getUserDetails.update(data);
|
||||
}
|
||||
$("#cl-logout-button").show("fast");
|
||||
$("#cl-api-button").show("fast");
|
||||
$("#cl-profile-button").show("fast");
|
||||
@ -127,7 +133,4 @@
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
</nav>
|
45
contact.js
45
contact.js
@ -1,43 +1,4 @@
|
||||
//import('dotenv').config({ path: path.resolve(__dirname, '../../../.env') })
|
||||
|
||||
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const form = document.getElementById('form');
|
||||
|
||||
// Set the new value for the access_key input field
|
||||
form.querySelector('input[name="access_key"]').value = process.env.emailKey;
|
||||
|
||||
form.addEventListener('submit', async (event) => {
|
||||
event.preventDefault(); // Prevent default form submission
|
||||
|
||||
// Create a FormData object to include the key
|
||||
const formData = new FormData(form);
|
||||
|
||||
// Submit the form using fetch API
|
||||
try {
|
||||
const response = await fetch('https://api.web3forms.com/submit', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
// Handle the API response
|
||||
console.log(result);
|
||||
|
||||
if (result.success) {
|
||||
// Form submitted successfully, display notification
|
||||
alert('Form submitted successfully!');
|
||||
location.reload()
|
||||
} else {
|
||||
// Form submission failed, display error notification
|
||||
alert('Form submission failed. Please try again.');
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
//console.error('Error:', error);
|
||||
}
|
||||
});
|
||||
});
|
||||
moment = require('moment')
|
||||
|
||||
//current time
|
||||
console.log(moment().format('hh:mm:ss a'))
|
Loading…
x
Reference in New Issue
Block a user