|
|
|
const userController = require("./controllers/userController");
|
|
|
|
const tanksController = require("./controllers/tanksController");
|
|
|
|
const createConnectionController = require("./controllers/createConnectionController");
|
|
|
|
const cors = require("cors");
|
|
|
|
const swagger = require("./config/swagger");
|
|
|
|
const rawBody = require('raw-body')
|
|
|
|
const uuidv4 = require("uuid").v4;
|
|
|
|
const fastify = require("fastify")({
|
|
|
|
logger: true,
|
|
|
|
//disableRequestLogging: true,
|
|
|
|
genReqId(req) {
|
|
|
|
// you get access to the req here if you need it - must be a synchronous function
|
|
|
|
return uuidv4();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// const Fastify = require("fastify");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const Fastify = require("fastify");
|
|
|
|
// const server = Fastify({
|
|
|
|
// logger: true,
|
|
|
|
// // ajv: { plugins: [ajvPlugin] },
|
|
|
|
// genReqId(req) {
|
|
|
|
// // you get access to the req here if you need it - must be a synchronous function
|
|
|
|
// return uuidv4();
|
|
|
|
// },
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// fastify.register(View).ready((err) => {
|
|
|
|
// if (err) console.error(err);
|
|
|
|
|
|
|
|
// console.log(fastify.config.PORT); // or fastify[options.confKey]
|
|
|
|
// // output: { PORT: 3000 }
|
|
|
|
// engine: {
|
|
|
|
// ejs: require('ejs'),
|
|
|
|
// },
|
|
|
|
// root: join(__dirname, 'views/html'),
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
const now = () => Date.now();
|
|
|
|
|
|
|
|
const fastifyEnv = require("fastify-env");
|
|
|
|
|
|
|
|
const schema = {
|
|
|
|
type: "object",
|
|
|
|
required: ["PORT"],
|
|
|
|
properties: {
|
|
|
|
PORT: {
|
|
|
|
type: "string",
|
|
|
|
default: 3000,
|
|
|
|
},
|
|
|
|
APIVERSION: {
|
|
|
|
type: "string",
|
|
|
|
default: "1.0.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
confKey: "config", // optional, default: 'config'
|
|
|
|
schema: schema,
|
|
|
|
// data: data // optional, default: process.env
|
|
|
|
};
|
|
|
|
fastify.register(fastifyEnv, options).ready((err) => {
|
|
|
|
if (err) console.error(err);
|
|
|
|
|
|
|
|
console.log(fastify.config.PORT); // or fastify[options.confKey]
|
|
|
|
// output: { PORT: 3000 }
|
|
|
|
fastify.decorate("conf", {
|
|
|
|
port: fastify.config.PORT,
|
|
|
|
APIVERSION: fastify.config.APIVERSION,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const apiversion = "1.0.0";
|
|
|
|
const path = require("path");
|
|
|
|
|
|
|
|
// Using static content for swagger documentation. Generated swagger UI is not user friendly.
|
|
|
|
fastify.register(require("fastify-static"), {
|
|
|
|
root: path.join(__dirname, "api-docs"),
|
|
|
|
prefix: "/api-docs", // optional: default '/'
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fastify.register(require("fastify-swagger"), swagger.options);
|
|
|
|
|
|
|
|
const customJwtAuth = require("./customAuthJwt");
|
|
|
|
fastify.register(customJwtAuth);
|
|
|
|
//login route - accept user credentials and send a token with role . "user" role is required to use the app.
|
|
|
|
|
|
|
|
// support login using application/x-www-form-urlencoded so users can login via a web form in addition to api
|
|
|
|
|
|
|
|
fastify.register(require("fastify-formbody"));
|
|
|
|
// fastify.register(require('fastify-multipart'))
|
|
|
|
// fastify.register(require("fastify-cors"), {
|
|
|
|
// // put your options here
|
|
|
|
// origin: [
|
|
|
|
// new RegExp("http://localhost"),
|
|
|
|
// new RegExp("http://simply-backoffice.true2air.com"),
|
|
|
|
// new RegExp("http://localhost:3000"),
|
|
|
|
// ],
|
|
|
|
// credentials: true,
|
|
|
|
// optionsSuccessStatus: 200,
|
|
|
|
// });
|
|
|
|
|
|
|
|
fastify.register((fastify, opts, done) => {
|
|
|
|
fastify.addContentTypeParser(
|
|
|
|
"application/json",
|
|
|
|
{ parseAs: "buffer" },
|
|
|
|
function (_req, body, done) {
|
|
|
|
try {
|
|
|
|
done(null, body)
|
|
|
|
} catch (error) {
|
|
|
|
error.statusCode = 400
|
|
|
|
done(error, undefined)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
done(null)
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fastify.register(require('point-of-view'), {
|
|
|
|
engine: {
|
|
|
|
nunjucks: require('nunjucks')
|
|
|
|
},
|
|
|
|
root: path.join(__dirname, "views"),
|
|
|
|
includeViewExtension: true,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// * This is for login user as a simply user *
|
|
|
|
|
|
|
|
fastify.post("/api/login", {
|
|
|
|
schema: {
|
|
|
|
description: "This is for Login User",
|
|
|
|
tags: ["Login"],
|
|
|
|
summary: "This is for User Login",
|
|
|
|
body: {
|
|
|
|
type: "object",
|
|
|
|
required: ["username", "password"],
|
|
|
|
properties: {
|
|
|
|
username: { type: "string" },
|
|
|
|
password: { type: "string" },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
async handler(req, reply) {
|
|
|
|
loginObject = await userController.loginUser(req);
|
|
|
|
if (loginObject.same) {
|
|
|
|
const phoneVerified = loginObject.user.phoneVerified;
|
|
|
|
const oneTimePasswordSetFlag = loginObject.user.oneTimePasswordSetFlag;
|
|
|
|
console.log(
|
|
|
|
"oneTimePasswordSetFlag is ......",
|
|
|
|
oneTimePasswordSetFlag,
|
|
|
|
typeof oneTimePasswordSetFlag,
|
|
|
|
typeof phoneVerified
|
|
|
|
);
|
|
|
|
if (!phoneVerified) {
|
|
|
|
reply.send({
|
|
|
|
simplydata: {
|
|
|
|
error: false,
|
|
|
|
phoneVerified: false,
|
|
|
|
|
|
|
|
phone: loginObject.user.phone,
|
|
|
|
oneTimePasswordSetFlag: oneTimePasswordSetFlag,
|
|
|
|
message: "Please Verify your phone number",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else if (oneTimePasswordSetFlag) {
|
|
|
|
reply.send({
|
|
|
|
simplydata: {
|
|
|
|
error: false,
|
|
|
|
phoneVerified: phoneVerified,
|
|
|
|
phone: loginObject.user.phone,
|
|
|
|
oneTimePasswordSetFlag: true,
|
|
|
|
message: "Password must be reset",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const token = fastify.jwt.sign(
|
|
|
|
{
|
|
|
|
username: loginObject.user.username,
|
|
|
|
userId: loginObject.user._id,
|
|
|
|
roles: loginObject.user.profile.role,
|
|
|
|
},
|
|
|
|
//expiresIn: expressed in seconds or a string describing a time span zeit/ms. Eg: 60, "2 days", "10h", "7d".
|
|
|
|
//A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc),
|
|
|
|
//otherwise milliseconds unit is used by default ("120" is equal to "120ms").
|
|
|
|
{ expiresIn: "12h" }
|
|
|
|
);
|
|
|
|
var arr = loginObject.user.profile.role;
|
|
|
|
var arrayToString = JSON.stringify(Object.assign({}, arr)); // convert array to string
|
|
|
|
var stringToJsonObject = JSON.parse(arrayToString); // convert string to json object
|
|
|
|
|
|
|
|
// console.log({
|
|
|
|
// username: loginObject.user.username,
|
|
|
|
// roles: loginObject.user.profile.role,
|
|
|
|
// rolesasobj: stringToJsonObject,
|
|
|
|
// });
|
|
|
|
// console.log("sending token \n");
|
|
|
|
// console.log(token);
|
|
|
|
reply.send({
|
|
|
|
simplydata: {
|
|
|
|
error: false,
|
|
|
|
apiversion: fastify.config.APIVERSION,
|
|
|
|
access_token: token,
|
|
|
|
email: loginObject.user.emails,
|
|
|
|
phone: loginObject.user.phone,
|
|
|
|
username: loginObject.user.username,
|
|
|
|
address1: loginObject.user.profile.address1,
|
|
|
|
address2: loginObject.user.profile.address2,
|
|
|
|
phoneVerified: loginObject.user.phoneVerified,
|
|
|
|
oneTimePasswordSetFlag: loginObject.user.oneTimePasswordSetFlag,
|
|
|
|
type: loginObject.user.profile.role,
|
|
|
|
typeasobj: stringToJsonObject,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error = {
|
|
|
|
simplydata: {
|
|
|
|
error: true,
|
|
|
|
code: 400,
|
|
|
|
message: "Invalid UserId , Password supplied",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
reply.send(error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
fastify.get("/api/reset_token/:username", {
|
|
|
|
async handler(req, reply) {
|
|
|
|
try {
|
|
|
|
const get_user = await userController.getSingleUser(req);
|
|
|
|
const token = fastify.jwt.sign(
|
|
|
|
{
|
|
|
|
username: get_user.username,
|
|
|
|
userId: get_user._id,
|
|
|
|
roles: get_user.profile.role,
|
|
|
|
},
|
|
|
|
{ expiresIn: "12h" }
|
|
|
|
);
|
|
|
|
reply.send({ access_token: token, username: get_user.username });
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
error = {
|
|
|
|
simplydata: {
|
|
|
|
error: true,
|
|
|
|
code: 400,
|
|
|
|
message: "Reset Token failed",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
reply.status(401).send(error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
fastify.get('/testtemp', (req, reply) => {
|
|
|
|
reply.view('layouts/main', {});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
//fastify-auth plugin is required so we can define routes in seperate files and verify jwt supplied in preHandlers for each request.
|
|
|
|
const multer = require("fastify-multer");
|
|
|
|
fastify.register(require("fastify-auth"));
|
|
|
|
const dbConnection = require("./config/config");
|
|
|
|
fastify.register(dbConnection);
|
|
|
|
fastify.register(multer.contentParser);
|
|
|
|
const { Schema } = require("mongoose");
|
|
|
|
// fastify.register(dbConnection);
|
|
|
|
|
|
|
|
fastify.register(require("./routes/usersRoute"));
|
|
|
|
|
|
|
|
fastify.register(require("./routes/tanksRoute"));
|
|
|
|
fastify.register(require("./routes/createConnectionsRoute"));
|
|
|
|
|
|
|
|
// Testing route allows for retrieving a user by phone so one can see what is the phone verification code sent for a given user's phone
|
|
|
|
// Also allows deletion of a user with a given phone number
|
|
|
|
fastify.register(require("./routes/forTestingRoute"));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// fastify.get("/", (req, reply) => {
|
|
|
|
// reply.view("/templates/index.ejs", { text: "text" });
|
|
|
|
// });
|
|
|
|
// Run the server!
|
|
|
|
const start = async () => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
|
|
await fastify.listen(3000, "0.0.0.0");
|
|
|
|
fastify.log.info(`listening on ${fastify.server.address().port}`);
|
|
|
|
fastify.log.info(`server listening on ${fastify.config}`);
|
|
|
|
} catch (err) {
|
|
|
|
fastify.log.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
start();
|