mirror of
https://github.com/20kaushik02/express-sequelize-backend-template.git
synced 2025-12-06 06:34:06 +00:00
boom!
overall: formatting check, jsdoc type hints, express res/return stuff utils - changes in logger, dateformatter and removed unneeded ones .env file changes license check, readme update package.json update - version, deps, URLs server cleanup sequelize config check
This commit is contained in:
parent
7318e8e325
commit
32735ad7ff
5
.env
5
.env
@ -1,9 +1,12 @@
|
|||||||
PORT=5000
|
PORT=5000
|
||||||
|
TRUST_PROXY = 1
|
||||||
|
|
||||||
|
AUTOMAILER_SMTP_SERVICE
|
||||||
|
AUTOMAILER_SMTP_HOST
|
||||||
AUTOMAILER_ID = "mailerID@mailserver.domain"
|
AUTOMAILER_ID = "mailerID@mailserver.domain"
|
||||||
AUTOMAILER_APP_PASSWD = "mailerpasswd"
|
AUTOMAILER_APP_PASSWD = "mailerpasswd"
|
||||||
|
|
||||||
JWTSECRET="somerandomstring"
|
JWTSECRET="somerandomstring"
|
||||||
|
|
||||||
ADMIN_USER = "ADMIN_USER"
|
ADMIN_USER = "ADMIN_USER"
|
||||||
ADMIN_PASSWD = "SOME_ADMIN_PASSWD"
|
ADMIN_PASSWD = "SOME_ADMIN_PASSWD"
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
DB_USERNAME='your_local_db_username'
|
DB_USER = your_database_username
|
||||||
DB_PASSWORD='your_local_db_password'
|
DB_PASSWD = your_database_password
|
||||||
DB_NAME='your_local_db_name'
|
DB_NAME = your_database_name
|
||||||
|
DB_HOST = localhost
|
||||||
|
DB_PORT = your_database_port
|
||||||
|
DB_DIALECT = your_database_dialect
|
||||||
|
|
||||||
CAPTCHA_SECRET = "6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe"
|
CAPTCHA_SECRET = 'your-captcha-secret'
|
||||||
|
|||||||
3
.env.production
Normal file
3
.env.production
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
DB_URL = 'your_database_connection_string'
|
||||||
|
|
||||||
|
CAPTCHA_SECRET = 'your-captcha-secret'
|
||||||
101
.gitignore
vendored
101
.gitignore
vendored
@ -6,14 +6,101 @@ yarn-debug.log*
|
|||||||
yarn-error.log*
|
yarn-error.log*
|
||||||
lerna-debug.log*
|
lerna-debug.log*
|
||||||
|
|
||||||
|
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||||
|
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||||
|
|
||||||
|
# Runtime data
|
||||||
|
pids
|
||||||
|
*.pid
|
||||||
|
*.seed
|
||||||
|
*.pid.lock
|
||||||
|
|
||||||
|
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||||
|
lib-cov
|
||||||
|
|
||||||
|
# Coverage directory used by tools like istanbul
|
||||||
|
coverage
|
||||||
|
*.lcov
|
||||||
|
|
||||||
|
# nyc test coverage
|
||||||
|
.nyc_output
|
||||||
|
|
||||||
|
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||||
|
.grunt
|
||||||
|
|
||||||
|
# Bower dependency directory (https://bower.io/)
|
||||||
|
bower_components
|
||||||
|
|
||||||
|
# node-waf configuration
|
||||||
|
.lock-wscript
|
||||||
|
|
||||||
|
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||||
|
build/Release
|
||||||
|
|
||||||
# Dependency directories
|
# Dependency directories
|
||||||
node_modules/
|
node_modules/
|
||||||
|
|
||||||
# dotenv environment variables file
|
# TypeScript v1 declaration files
|
||||||
.env.development
|
typings/
|
||||||
.env.staging
|
|
||||||
.env.production
|
|
||||||
*.env
|
|
||||||
|
|
||||||
# Data files
|
# TypeScript cache
|
||||||
*/*.csv
|
*.tsbuildinfo
|
||||||
|
|
||||||
|
# Optional npm cache directory
|
||||||
|
.npm
|
||||||
|
|
||||||
|
# Optional eslint cache
|
||||||
|
.eslintcache
|
||||||
|
|
||||||
|
# Microbundle cache
|
||||||
|
.rpt2_cache/
|
||||||
|
.rts2_cache_cjs/
|
||||||
|
.rts2_cache_es/
|
||||||
|
.rts2_cache_umd/
|
||||||
|
|
||||||
|
# Optional REPL history
|
||||||
|
.node_repl_history
|
||||||
|
|
||||||
|
# Output of 'npm pack'
|
||||||
|
*.tgz
|
||||||
|
|
||||||
|
# Yarn Integrity file
|
||||||
|
.yarn-integrity
|
||||||
|
|
||||||
|
# dotenv environment variables file
|
||||||
|
.env.local
|
||||||
|
.env.*.local
|
||||||
|
|
||||||
|
# parcel-bundler cache (https://parceljs.org/)
|
||||||
|
.cache
|
||||||
|
|
||||||
|
# Next.js build output
|
||||||
|
.next
|
||||||
|
|
||||||
|
# Nuxt.js build / generate output
|
||||||
|
.nuxt
|
||||||
|
dist
|
||||||
|
|
||||||
|
# Gatsby files
|
||||||
|
.cache/
|
||||||
|
# Comment in the public line in if your project uses Gatsby and *not* Next.js
|
||||||
|
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||||
|
# public
|
||||||
|
|
||||||
|
# vuepress build output
|
||||||
|
.vuepress/dist
|
||||||
|
|
||||||
|
# Serverless directories
|
||||||
|
.serverless/
|
||||||
|
|
||||||
|
# FuseBox cache
|
||||||
|
.fusebox/
|
||||||
|
|
||||||
|
# DynamoDB Local files
|
||||||
|
.dynamodb/
|
||||||
|
|
||||||
|
# TernJS port file
|
||||||
|
.tern-port
|
||||||
|
|
||||||
|
# SQLite db
|
||||||
|
*.db
|
||||||
|
|||||||
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2023 Kaushik Narayan R
|
Copyright (c) 2024 Kaushik Narayan R
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|||||||
12
README.md
12
README.md
@ -1,21 +1,17 @@
|
|||||||
# Express-Sequelize backend server template
|
# Express-Sequelize backend server template
|
||||||
|
|
||||||
### To get started:
|
## To get started
|
||||||
|
|
||||||
- Clone this repo: `git clone https://github.com/20kaushik02/express-sequelize-backend-template`
|
- Clone this repo: `git clone https://github.com/20kaushik02/express-sequelize-backend-template`
|
||||||
- Reset the git remote repo URL: `git remote rm origin`
|
- Reset the git remote repo URL: `git remote rm origin`
|
||||||
- Set new git remote URL: `git remote add origin https://github.com/20kaushik02/new-repo-name`
|
- Set new git remote URL: `git remote add origin https://github.com/20kaushik02/<<new-repo-name>>`
|
||||||
- Remove the template environment files from git alone: `git rm -r --cached *.env*`
|
- Remove the template environment files from git alone: `git rm -r --cached *.env*`
|
||||||
|
|
||||||
### Project setup:
|
## Project setup
|
||||||
|
|
||||||
- Edit `package.json` to reflect the new name and URLs
|
- Edit `package.json` to reflect the new name and URLs
|
||||||
- Edit `README.md` to reflect project details
|
- Edit `README.md` to reflect project details
|
||||||
- Run `npm i` to install all dependencies
|
- Run `npm i` to install all dependencies
|
||||||
- Before running `sequelize-cli` commands while developing, make sure to set `$env:NODE_ENV='development'` on Windows, or `NODE_ENV=development` on Linux/MacOS
|
- Before running `sequelize-cli` commands while developing, make sure to set `$env:NODE_ENV='development'` on Windows, or `NODE_ENV=development` on Linux/MacOS
|
||||||
- Env config:
|
- [See here](https://github.com/kerimdzhanov/dotenv-flow?tab=readme-ov-file#files-under-version-control) for best practices for .env files configuration
|
||||||
- **.env** - All things common to all environments (port, etc.)
|
|
||||||
- **.env.development** - Development environment (dev captcha secret, mailer creds, JWT secret, dev DB details, dev admin data access creds)
|
|
||||||
- **.env.staging** - Staging environment (dev captcha secret, mailer creds, JWT secret, staging DB conn. string, staging admin data access creds) - **for sysadmins**
|
|
||||||
- **.env.production** - Production environment (production captcha secret, mailer creds, JWT secret, prod DB conn. string, production admin data access creds) - **for sysadmins**
|
|
||||||
- Staging: `npm run staging_prep` and `npm run staging` to deploy on Render after configuring a new web service on Render dashboard
|
- Staging: `npm run staging_prep` and `npm run staging` to deploy on Render after configuring a new web service on Render dashboard
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
## Boilerplates - reusable code templates
|
# Boilerplates - reusable code templates
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
const typedefs = require("../typedefs");
|
|
||||||
const logger = require("../utils/logger")(module);
|
const logger = require("../utils/logger")(module);
|
||||||
|
|
||||||
|
const typedefs = require("../typedefs");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Business logic to go in these controller functions.
|
* Business logic to go in these controller functions.
|
||||||
* Everything should be contained inside try-catch blocks
|
* Everything should be contained inside try-catch blocks
|
||||||
@ -12,11 +13,12 @@ const __controller_func = async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
res.sendStatus(500);
|
||||||
logger.error("__controller_func", { error });
|
logger.error("__controller_func", { error });
|
||||||
return res.status(500).send({ message: "Server Error. Try again." });
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
__controller_func
|
__controller_func
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,14 +1,13 @@
|
|||||||
const router = require("express").Router();
|
const router = require("express").Router();
|
||||||
|
|
||||||
const { validate } = require("../validators");
|
const { validate } = require("../validators");
|
||||||
const { __controller_func } = require("./controller");
|
|
||||||
|
|
||||||
router.get(
|
router.get(
|
||||||
// URL,
|
// URL,
|
||||||
// middleware,
|
// middleware,
|
||||||
// validators,
|
// validators,
|
||||||
// validate,
|
// validate,
|
||||||
// __controller_func
|
// controller
|
||||||
);
|
);
|
||||||
|
|
||||||
router.post(
|
router.post(
|
||||||
|
|||||||
@ -24,5 +24,4 @@ const __validator_func = async (req, res, next) => {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
__validator_func,
|
__validator_func,
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
## Configuration files and data
|
# Configuration files and data
|
||||||
|
|||||||
@ -1,23 +1,28 @@
|
|||||||
module.exports = {
|
const logger = require("../utils/logger")(module);
|
||||||
"development": {
|
|
||||||
"username": process.env.DB_USERNAME, // local PostgreSQL DB username
|
const connConfigs = {
|
||||||
"password": process.env.DB_PASSWORD, // local PostgreSQL DB password
|
development: {
|
||||||
"host": "127.0.0.1", // localhost
|
username: process.env.DB_USERNAME || 'postgres',
|
||||||
"database": process.env.DB_NAME, // local PostgreSQL DB name
|
password: process.env.DB_PASSWORD || '',
|
||||||
"dialect": "postgres"
|
database: process.env.DB_NAME || 'postgres',
|
||||||
},
|
host: process.env.DB_HOST || '127.0.0.1',
|
||||||
"staging": {
|
port: process.env.DB_PORT || 5432,
|
||||||
"use_env_variable": "DB_URL", // staging database connection string
|
},
|
||||||
"dialect": "postgres",
|
staging: {
|
||||||
"dialectOptions": {
|
use_env_variable: "DB_URL", // use connection string for non-dev env
|
||||||
"ssl": true,
|
},
|
||||||
},
|
production: {
|
||||||
},
|
use_env_variable: "DB_URL", // use connection string for non-dev env
|
||||||
"production": {
|
// dialectOptions: {
|
||||||
"use_env_variable": "DB_URL", // production database connection string
|
// ssl: true,
|
||||||
"dialect": "postgres",
|
// },
|
||||||
"dialectOptions": {
|
}
|
||||||
"ssl": true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// common config
|
||||||
|
for (const conf in connConfigs) {
|
||||||
|
connConfigs[conf]['logging'] = (msg) => logger.debug(msg);
|
||||||
|
connConfigs[conf]['dialect'] = process.env.DB_DIALECT || 'postgres';
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = connConfigs;
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
## Controllers - business logic functions, end of the API route
|
# Controllers - business logic functions, end of the API route
|
||||||
|
|||||||
37
index.js
37
index.js
@ -1,23 +1,35 @@
|
|||||||
require("dotenv-flow").config();
|
require("dotenv-flow").config();
|
||||||
|
|
||||||
|
const util = require('util');
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
|
|
||||||
const cors = require("cors");
|
const cors = require("cors");
|
||||||
const helmet = require("helmet");
|
const helmet = require("helmet");
|
||||||
|
|
||||||
const logger = require("./utils/logger")(module);
|
const logger = require("./utils/logger")(module);
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
app.use(express.json());
|
// Enable this if you run behind a proxy (e.g. nginx)
|
||||||
app.use(express.urlencoded({ extended: true }));
|
app.set('trust proxy', process.env.TRUST_PROXY);
|
||||||
|
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(helmet());
|
app.use(helmet());
|
||||||
app.disable("x-powered-by");
|
app.disable("x-powered-by");
|
||||||
|
|
||||||
|
app.use(express.json());
|
||||||
|
app.use(express.urlencoded({ extended: true }));
|
||||||
|
|
||||||
|
// Static
|
||||||
|
app.use(express.static(__dirname + '/static'));
|
||||||
|
|
||||||
// Put routes here
|
// Put routes here
|
||||||
|
|
||||||
app.use((_req, res) => {
|
// Fallbacks
|
||||||
return res.status(200).send("Back-end for");
|
app.use((req, res) => {
|
||||||
|
res.status(200).send("Back-end for");
|
||||||
|
logger.info("Unrecognized URL", { url: req.url });
|
||||||
|
return;
|
||||||
});
|
});
|
||||||
|
|
||||||
const port = process.env.PORT || 5000;
|
const port = process.env.PORT || 5000;
|
||||||
@ -25,3 +37,20 @@ const port = process.env.PORT || 5000;
|
|||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
logger.info(`App Listening on port ${port}`);
|
logger.info(`App Listening on port ${port}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const cleanupFunc = (signal) => {
|
||||||
|
if (signal)
|
||||||
|
logger.info(`${signal} signal received, shutting down now...`);
|
||||||
|
|
||||||
|
Promise.allSettled([
|
||||||
|
// handle DB conn, sockets, etc. here
|
||||||
|
util.promisify(server.close),
|
||||||
|
]).then(() => {
|
||||||
|
logger.info("Cleaned up, exiting.");
|
||||||
|
process.exit(0);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGTERM', 'SIGUSR1', 'SIGUSR2'].forEach((signal) => {
|
||||||
|
process.on(signal, () => cleanupFunc(signal));
|
||||||
|
});
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
## Keys - public/private key pairs for certificate QR signing and verification
|
# Keys - public/private key pairs for certificate QR signing and verification
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
## Middleware - functionalities that must be in the middle of the API route control flow
|
# Middleware - functionalities that must be in the middle of the API route control flow
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
const typedefs = require("../typedefs");
|
|
||||||
const logger = require("../utils/logger")(module);
|
const logger = require("../utils/logger")(module);
|
||||||
|
|
||||||
|
const typedefs = require("../typedefs");
|
||||||
|
|
||||||
const creds = JSON.parse(process.env.ADMIN_CREDS);
|
const creds = JSON.parse(process.env.ADMIN_CREDS);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -11,7 +12,7 @@ const creds = JSON.parse(process.env.ADMIN_CREDS);
|
|||||||
*/
|
*/
|
||||||
const adminQueryCreds = async (req, res, next) => {
|
const adminQueryCreds = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
/** @type {JSON} */
|
/** @type {any} */
|
||||||
const { user, access } = req.query;
|
const { user, access } = req.query;
|
||||||
if (creds[user] === access) {
|
if (creds[user] === access) {
|
||||||
logger.info("Admin access - " + user);
|
logger.info("Admin access - " + user);
|
||||||
@ -20,15 +21,17 @@ const adminQueryCreds = async (req, res, next) => {
|
|||||||
else {
|
else {
|
||||||
// we do a bit of trolling here
|
// we do a bit of trolling here
|
||||||
const unauthIP = req.headers['x-real-ip'] || req.ip
|
const unauthIP = req.headers['x-real-ip'] || req.ip
|
||||||
|
res.status(401).send("Intruder alert. IP address: " + unauthIP);
|
||||||
logger.warn("Intruder alert.", { ip: unauthIP });
|
logger.warn("Intruder alert.", { ip: unauthIP });
|
||||||
return res.status(401).send("Intruder alert. IP address: " + unauthIP);
|
return;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
res.sendStatus(500);
|
||||||
logger.error("adminQueryCreds", { error });
|
logger.error("adminQueryCreds", { error });
|
||||||
return res.status(500).send({ message: "Server Error. Try again." });
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
adminQueryCreds,
|
adminQueryCreds,
|
||||||
}
|
};
|
||||||
|
|||||||
@ -1,8 +1,9 @@
|
|||||||
const fetch = require("cross-fetch");
|
const fetch = require("cross-fetch");
|
||||||
|
|
||||||
const typedefs = require("../typedefs");
|
|
||||||
const logger = require("../utils/logger")(module);
|
const logger = require("../utils/logger")(module);
|
||||||
|
|
||||||
|
const typedefs = require("../typedefs");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Google ReCAPTCHA v2 verification
|
* Google ReCAPTCHA v2 verification
|
||||||
*
|
*
|
||||||
@ -19,18 +20,20 @@ const verifyCaptcha = async (req, res, next) => {
|
|||||||
const captchaResp = await fetch(verifyCaptchaURL);
|
const captchaResp = await fetch(verifyCaptchaURL);
|
||||||
const captchaData = await captchaResp.json();
|
const captchaData = await captchaResp.json();
|
||||||
if (captchaData.success !== undefined && !captchaData.success) {
|
if (captchaData.success !== undefined && !captchaData.success) {
|
||||||
logger.error("Recaptcha", { captchaData });
|
res.status(403).send({
|
||||||
return res.status(403).send({
|
|
||||||
message: "Failed captcha verification"
|
message: "Failed captcha verification"
|
||||||
});
|
});
|
||||||
|
logger.error("Recaptcha", { captchaData });
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("Error", { error });
|
res.sendStatus(500);
|
||||||
return res.status(500).send({ message: "Server Error. Try again." });
|
logger.error("verifyCaptcha", { error });
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
verifyCaptcha
|
verifyCaptcha
|
||||||
}
|
};
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
## Database (mainly Sequelize's) migrations
|
# Sequelize migrations folder
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
## Database ORM (mainly Sequelize's) models
|
# Sequelize model schema
|
||||||
|
|||||||
@ -8,40 +8,39 @@ const env = process.env.NODE_ENV || "development";
|
|||||||
const config = require(__dirname + "/../config/sequelize.js")[env];
|
const config = require(__dirname + "/../config/sequelize.js")[env];
|
||||||
const db = {};
|
const db = {};
|
||||||
|
|
||||||
// Create new Sequelize instance
|
|
||||||
let sequelize;
|
let sequelize;
|
||||||
if (config.use_env_variable) {
|
if (config.use_env_variable) {
|
||||||
sequelize = new Sequelize(process.env[config.use_env_variable], config);
|
sequelize = new Sequelize(process.env[config.use_env_variable], config);
|
||||||
} else {
|
} else {
|
||||||
sequelize = new Sequelize(config.database, config.username, config.password, config);
|
sequelize = new Sequelize(config.database, config.username, config.password, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
sequelize.authenticate()
|
(async () => {
|
||||||
.then(
|
try {
|
||||||
() => {
|
await sequelize.authenticate();
|
||||||
logger.info('Sequelize auth success');
|
logger.info("Sequelize auth success");
|
||||||
},
|
} catch (error) {
|
||||||
(err) => {
|
logger.error("Sequelize auth error", { err });
|
||||||
logger.error('Sequelize auth error', { err });
|
throw error;
|
||||||
}
|
}
|
||||||
)
|
})();
|
||||||
|
|
||||||
// Read model definitions from folder
|
// Read model definitions from folder
|
||||||
fs
|
fs
|
||||||
.readdirSync(__dirname)
|
.readdirSync(__dirname)
|
||||||
.filter(file => {
|
.filter(file => {
|
||||||
return (file.indexOf(".") !== 0) && (file !== basename) && (file.slice(-3) === ".js");
|
return (file.indexOf(".") !== 0) && (file !== basename) && (file.slice(-3) === ".js");
|
||||||
})
|
})
|
||||||
.forEach(file => {
|
.forEach(file => {
|
||||||
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
|
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
|
||||||
db[model.name] = model;
|
db[model.name] = model;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Setup defined associations
|
// Setup defined associations
|
||||||
Object.keys(db).forEach(modelName => {
|
Object.keys(db).forEach(modelName => {
|
||||||
if (db[modelName].associate) {
|
if (db[modelName].associate) {
|
||||||
db[modelName].associate(db);
|
db[modelName].associate(db);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
db.sequelize = sequelize;
|
db.sequelize = sequelize;
|
||||||
|
|||||||
5402
package-lock.json
generated
5402
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
43
package.json
43
package.json
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "backend-template",
|
"name": "backend-template",
|
||||||
"version": "1.3.1",
|
"version": "2.0.0",
|
||||||
"description": "Template for back-end server using Express and Sequelize.",
|
"description": "Template for a back-end server using Express and Sequelize.",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "cross-env NODE_ENV=development nodemon --exitcrash index.js",
|
"dev": "cross-env NODE_ENV=development nodemon --exitcrash index.js",
|
||||||
@ -11,33 +11,36 @@
|
|||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://gitlab.com/ctf-tech-2023/backend-template.git"
|
"url": "git+https://github.com/20kaushik02/express-sequelize-backend-template.git"
|
||||||
},
|
},
|
||||||
"author": "Kaushik Ravishankar <rknarayan02@gmail.com>",
|
"author": "Kaushik Ravishankar <rknarayan02@gmail.com>",
|
||||||
"license": "ISC",
|
"license": "MIT",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://gitlab.com/ctf-tech-2023/backend-template/issues"
|
"url": "https://github.com/20kaushik02/express-sequelize-backend-template/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/ctf-tech-2023/backend-template#readme",
|
"homepage": "https://github.com/20kaushik02/express-sequelize-backend-template#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"archiver": "^5.3.1",
|
"archiver": "^7.0.1",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"cross-fetch": "^3.1.5",
|
"cross-fetch": "^4.0.0",
|
||||||
"dotenv-flow": "^3.2.0",
|
"dotenv-flow": "^4.1.0",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"express-validator": "^7.0.1",
|
"express-validator": "^7.2.0",
|
||||||
"fast-csv": "^4.3.6",
|
"fast-csv": "^5.0.1",
|
||||||
"helmet": "^7.0.0",
|
"helmet": "^7.1.0",
|
||||||
"jsonwebtoken": "^9.0.0",
|
"jsonwebtoken": "^9.0.2",
|
||||||
"nodemailer": "^6.9.2",
|
"nodemailer": "^6.9.14",
|
||||||
"pg": "^8.10.0",
|
"pg": "^8.12.0",
|
||||||
"qrcode": "^1.5.3",
|
"qrcode": "^1.5.4",
|
||||||
"sequelize": "^6.31.1",
|
"sequelize": "^6.37.3",
|
||||||
"winston": "^3.8.2"
|
"winston": "^3.14.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/express": "^4.17.21",
|
||||||
|
"@types/node": "^22.2.0",
|
||||||
"cross-env": "^7.0.3",
|
"cross-env": "^7.0.3",
|
||||||
"nodemon": "^2.0.20",
|
"nodemon": "^3.1.4",
|
||||||
"sequelize-cli": "^6.5.1"
|
"sequelize-cli": "^6.6.2",
|
||||||
|
"typescript": "^5.5.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
## Routes - define control flow of the API route
|
# Routes - define control flow of the API route
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
## Database (mainly Sequelize's) seeders - initial data feed, for dummy data and testing
|
# Sequelize seeder scripts - initial data feed, for dummy data and testing
|
||||||
|
|||||||
@ -6,12 +6,6 @@
|
|||||||
* @typedef {import("express").Request} Req
|
* @typedef {import("express").Request} Req
|
||||||
* @typedef {import("express").Response} Res
|
* @typedef {import("express").Response} Res
|
||||||
* @typedef {import("express").NextFunction} Next
|
* @typedef {import("express").NextFunction} Next
|
||||||
*
|
|
||||||
* @typedef {import("sequelize")} Sequelize
|
|
||||||
* @typedef {import("sequelize").Model} Model
|
|
||||||
* @typedef {import("sequelize").QueryInterface} QueryInterface
|
|
||||||
*
|
|
||||||
* @typedef {import("winston").Logger} Logger
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
exports.unused = {};
|
exports.unused = {};
|
||||||
@ -2,8 +2,8 @@ const fs = require("fs");
|
|||||||
const archiver = require('archiver');
|
const archiver = require('archiver');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {String} sourceDir: /some/folder/to/compress
|
* @param {string} sourceDir /some/folder/to/compress
|
||||||
* @param {String} outPath: /path/to/created.zip
|
* @param {string} outPath /path/to/created.zip
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
function zipDirectory(sourceDir, outPath) {
|
function zipDirectory(sourceDir, outPath) {
|
||||||
@ -24,4 +24,4 @@ function zipDirectory(sourceDir, outPath) {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
zipDirectory,
|
zipDirectory,
|
||||||
}
|
};
|
||||||
|
|||||||
@ -1,12 +1,14 @@
|
|||||||
/**
|
/**
|
||||||
* Returns a timestamp string to use for timestamped files
|
* Returns a timestamp string to use for timestamped files
|
||||||
* @returns {string} String of current datetime in YYYY.MM.DD-HH:MM:SS format
|
* @returns {string} String of current datetime in YYYYMMDDHHMMSS format
|
||||||
*/
|
*/
|
||||||
const dateForFilename = () => {
|
const dateForFilename = () => {
|
||||||
const dt = new Date();
|
return new Date().
|
||||||
return `${dt.getFullYear()}-${dt.getMonth() + 1}-${dt.getDate()}-${dt.getHours()}-${dt.getMinutes()}-${dt.getSeconds()}`;
|
toISOString().slice(-24).
|
||||||
|
replace(/\D/g, '').
|
||||||
|
slice(0, 14);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
dateForFilename,
|
dateForFilename,
|
||||||
}
|
};
|
||||||
|
|||||||
@ -1,3 +1,9 @@
|
|||||||
|
/**
|
||||||
|
* Recursively build a FormData object from a JSON object
|
||||||
|
* @param {FormData} formData
|
||||||
|
* @param {any} data
|
||||||
|
* @param {string} parentKey
|
||||||
|
*/
|
||||||
function buildFormData(formData, data, parentKey) {
|
function buildFormData(formData, data, parentKey) {
|
||||||
if (data && typeof data === 'object' && !(data instanceof Date)) {
|
if (data && typeof data === 'object' && !(data instanceof Date)) {
|
||||||
Object.keys(data).forEach(key => {
|
Object.keys(data).forEach(key => {
|
||||||
@ -10,6 +16,11 @@ function buildFormData(formData, data, parentKey) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a JSON object to a FormData object
|
||||||
|
* @param {any} data
|
||||||
|
* @returns {FormData}
|
||||||
|
*/
|
||||||
function jsonToFormData(data) {
|
function jsonToFormData(data) {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
|
|
||||||
@ -21,4 +32,4 @@ function jsonToFormData(data) {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
jsonToFormData,
|
jsonToFormData,
|
||||||
buildFormData,
|
buildFormData,
|
||||||
}
|
};
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
/**
|
/**
|
||||||
* String joins all the values of a JSON object, including nested keys
|
* Stringifies only values of a JSON object, including nested ones
|
||||||
*
|
*
|
||||||
* @param {any} obj JSON object
|
* @param {any} obj JSON object
|
||||||
* @param {string} delimiter Delimiter of final string
|
* @param {string} delimiter Delimiter of final string
|
||||||
* @returns
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
const getNestedValuesString = (obj, delimiter) => {
|
const getNestedValuesString = (obj, delimiter = ', ') => {
|
||||||
let values = [];
|
let values = [];
|
||||||
for (key in obj) {
|
for (key in obj) {
|
||||||
if (typeof obj[key] !== "object") {
|
if (typeof obj[key] !== "object") {
|
||||||
@ -15,9 +15,9 @@ const getNestedValuesString = (obj, delimiter) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return delimiter ? values.join(delimiter) : values.join();
|
return values.join(delimiter);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getNestedValuesString
|
getNestedValuesString
|
||||||
}
|
};
|
||||||
|
|||||||
@ -1,60 +1,67 @@
|
|||||||
// Whole thing is winston logger stuff, if you want to learn read the docs
|
|
||||||
|
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
const { createLogger, transports, config, format } = require("winston");
|
const { createLogger, transports, config, format } = require('winston');
|
||||||
const { combine, label, timestamp, printf } = format;
|
const { combine, label, timestamp, printf, errors } = format;
|
||||||
|
|
||||||
const typedefs = require("../typedefs");
|
const typedefs = require("../typedefs");
|
||||||
|
|
||||||
const getLabel = (callingModule) => {
|
const getLabel = (callingModule) => {
|
||||||
const parts = callingModule.filename.split(path.sep);
|
if (!callingModule.filename) return "repl";
|
||||||
return path.join(parts[parts.length - 2], parts.pop());
|
const parts = callingModule.filename?.split(path.sep);
|
||||||
|
return path.join(parts[parts.length - 2], parts.pop());
|
||||||
};
|
};
|
||||||
|
|
||||||
const logMetaReplacer = (key, value) => {
|
const allowedErrorKeys = ["name", "code", "message", "stack"];
|
||||||
if (key === "error") {
|
|
||||||
return value.name + ": " + value.message;
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
const metaFormat = (meta) => {
|
const metaFormat = (meta) => {
|
||||||
if (Object.keys(meta).length > 0)
|
if (Object.keys(meta).length > 0)
|
||||||
return "\n" + JSON.stringify(meta, logMetaReplacer) + "\n";
|
return '\n' + JSON.stringify(meta, null, "\t");
|
||||||
return "\n";
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
const logFormat = printf(({ level, message, label, timestamp, ...meta }) => {
|
const logFormat = printf(({ level, message, label, timestamp, ...meta }) => {
|
||||||
if (meta.error) {
|
if (meta.error) { // if the error was passed
|
||||||
for (const key in meta.error) {
|
for (const key in meta.error) {
|
||||||
if (typeof key !== "symbol" && key !== "message" && key !== "name") {
|
if (!allowedErrorKeys.includes(key)) {
|
||||||
delete meta.error[key]
|
delete meta.error[key];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
const { stack, ...rest } = meta.error;
|
||||||
return `${timestamp} [${label}] ${level}: ${message}${metaFormat(meta)}`;
|
return `${timestamp} [${label}] ${level}: ${message}${metaFormat(rest)}\n` +
|
||||||
|
`${stack ?? ''}`;
|
||||||
|
}
|
||||||
|
return `${timestamp} [${label}] ${level}: ${message}${metaFormat(meta)}`;
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a curried function, and call it with the module in use to get logs with filename
|
* Creates a curried function, and call it with the module in use to get logs with filename
|
||||||
* @param {typedefs.Module} callingModule The module from which the logger is called
|
* @param {typedefs.Module} callingModule The module from which the logger is called
|
||||||
* @returns {typedefs.Logger}
|
|
||||||
*/
|
*/
|
||||||
const logger = (callingModule) => {
|
const curriedLogger = (callingModule) => {
|
||||||
return createLogger({
|
let winstonLogger = createLogger({
|
||||||
levels: config.npm.levels,
|
levels: config.npm.levels,
|
||||||
format: combine(
|
format: combine(
|
||||||
label({ label: getLabel(callingModule) }),
|
errors({ stack: true }),
|
||||||
timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
|
label({ label: getLabel(callingModule) }),
|
||||||
logFormat,
|
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
|
||||||
),
|
logFormat,
|
||||||
transports: [
|
),
|
||||||
new transports.Console(),
|
transports: [
|
||||||
new transports.File({ filename: __dirname + "/../logs/common.log" }),
|
new transports.Console({ level: 'info' }),
|
||||||
new transports.File({ filename: __dirname + "/../logs/error.log", level: "error" }),
|
new transports.File({
|
||||||
]
|
filename: __dirname + '/../logs/debug.log',
|
||||||
});
|
level: 'debug',
|
||||||
|
maxsize: 10485760,
|
||||||
|
}),
|
||||||
|
new transports.File({
|
||||||
|
filename: __dirname + '/../logs/error.log',
|
||||||
|
level: 'error',
|
||||||
|
maxsize: 1048576,
|
||||||
|
}),
|
||||||
|
]
|
||||||
|
});
|
||||||
|
winstonLogger.on('error', (error) => winstonLogger.error("Error inside logger", { error }));
|
||||||
|
return winstonLogger;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = logger;
|
module.exports = curriedLogger;
|
||||||
|
|||||||
@ -1,45 +0,0 @@
|
|||||||
const mailer = require("nodemailer");
|
|
||||||
const logger = require("./logger")(module);
|
|
||||||
|
|
||||||
// Creates a mailer transporter object with authentication and base config
|
|
||||||
const transport = mailer.createTransport({
|
|
||||||
host: "smtp.gmail.com",
|
|
||||||
port: 465,
|
|
||||||
secure: true,
|
|
||||||
service: "gmail",
|
|
||||||
auth: {
|
|
||||||
user: process.env.AUTOMAILER_ID,
|
|
||||||
pass: process.env.AUTOMAILER_APP_PASSWD,
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends a mail from web user to a mail inside organization
|
|
||||||
* @param {string} mailTarget Target mail - must be within organization
|
|
||||||
* @param {string} mailSubject Mail subject
|
|
||||||
* @param {{name: string, email: string, message: string}} userData User details: name, email, and message
|
|
||||||
*/
|
|
||||||
const inboundMailer = (mailTarget, mailSubject, userData) => {
|
|
||||||
if (!mailTarget.endsWith("cegtechforum.in")) {
|
|
||||||
throw new Error("Invalid target mail domain.");
|
|
||||||
}
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
to: mailTarget,
|
|
||||||
subject: mailSubject,
|
|
||||||
html:
|
|
||||||
"<p>Name: " + userData.name + "</p><p>Email: " + userData.email + "</p><br/><p>Message:<br/>" + userData.message + "</p>"
|
|
||||||
};
|
|
||||||
|
|
||||||
transport.sendMail(message, (err, info) => {
|
|
||||||
if (err) {
|
|
||||||
logger.error("Failure: QUERY mail NOT sent", { err, userData });
|
|
||||||
} else {
|
|
||||||
logger.info("Success: QUERY mail sent", { info });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
inboundMailer
|
|
||||||
}
|
|
||||||
@ -1,58 +0,0 @@
|
|||||||
const pathLib = require("path");
|
|
||||||
const qr = require("qrcode");
|
|
||||||
const logger = require("./logger")(module);
|
|
||||||
const { getSignedJWT } = require("./token");
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generates QR code from data and writes to file in tmp folder.
|
|
||||||
* To avoid race conditions, use email or other unique attributes for id.
|
|
||||||
* @param {string|any} data String or JSON object
|
|
||||||
*/
|
|
||||||
const qrPNGFile = (id, data) => {
|
|
||||||
qr.toFile(
|
|
||||||
path = pathLib.join(__dirname, "../tmp/tmpQR-" + id + ".png"),
|
|
||||||
text = (typeof data === "object" ? JSON.stringify(data) : data),
|
|
||||||
options = { type: 'png' },
|
|
||||||
(err) => {
|
|
||||||
if (err) {
|
|
||||||
logger.error("qrPNGFile", err);
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generates QR code from data after signing and writes to file in tmp or k-qrs folder.
|
|
||||||
*
|
|
||||||
* To avoid race conditions, use email or other unique attributes for ID.
|
|
||||||
* @param {string|any} data String or JSON object
|
|
||||||
*/
|
|
||||||
const qrSignedPNGFile = (id, data, tmp = true) => {
|
|
||||||
const signedData = getSignedJWT(data);
|
|
||||||
const qrFilename = `${tmp ? 'tmpEncQR' : 'K-QR'}-${id}.png`;
|
|
||||||
const targetPath = pathLib.join(
|
|
||||||
__dirname, "..",
|
|
||||||
tmp ? "tmp" : pathLib.join("uploads", "2023", "k-qrs"),
|
|
||||||
qrFilename,
|
|
||||||
);
|
|
||||||
|
|
||||||
qr.toFile(
|
|
||||||
path = targetPath,
|
|
||||||
text = (typeof data === "object" ? JSON.stringify(signedData) : signedData),
|
|
||||||
options = { type: 'png' },
|
|
||||||
(err) => {
|
|
||||||
if (err) {
|
|
||||||
logger.error("qrSignedPNGFile", err);
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return qrFilename;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
qrPNGFile,
|
|
||||||
qrSignedPNGFile,
|
|
||||||
}
|
|
||||||
@ -1,23 +0,0 @@
|
|||||||
/* Taken from quick-encrypt package, which is not maintained anymore */
|
|
||||||
|
|
||||||
const crypto = require('crypto')
|
|
||||||
|
|
||||||
const acceptableBitSizes = [1024, 2048];
|
|
||||||
|
|
||||||
exports.generate = (sizeInBits) => {
|
|
||||||
if (!acceptableBitSizes.includes(sizeInBits))
|
|
||||||
throw Error('Error generating public and private key. Key size can only be 1024 or 2048. Example usage: ` let keys = QuickEncrypt.generate(2048); `')
|
|
||||||
return keypair({ bits: sizeInBits })
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.encrypt = (payloadString, publicKey) => {
|
|
||||||
if (typeof payloadString !== 'string' || typeof publicKey !== 'string')
|
|
||||||
throw Error("Error encrypting. Payload and Public Key should be in text format. Example usage: ` let encryptedText = QuickEncrypt.encrypt('Some secret text here!', 'the public RSA key in text format here'); ` ")
|
|
||||||
return crypto.publicEncrypt(publicKey, Buffer.from(payloadString, 'utf8')).toString('hex')
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.decrypt = (encryptedString, privateKey) => {
|
|
||||||
if (typeof encryptedString !== 'string' || typeof privateKey !== 'string')
|
|
||||||
throw Error("Error decrypting. Decrypted Text and Private Key should be in text format. Example usage: ` let decryptedText = QuickEncrypt.decrypt('asddd213d19jenacanscasn', 'the private RSA key in text format here'); ` ")
|
|
||||||
return crypto.privateDecrypt({ key: privateKey }, Buffer.from(encryptedString, 'hex')).toString()
|
|
||||||
}
|
|
||||||
@ -1,8 +1,8 @@
|
|||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const jwt = require("jsonwebtoken");
|
const jwt = require("jsonwebtoken");
|
||||||
|
|
||||||
const privateKey = fs.readFileSync(process.env.PRIVKEY);
|
const privateKey = fs.readFileSync(process.env.PRIVKEY_PATH);
|
||||||
const publicKey = fs.readFileSync(process.env.PUBKEY);
|
const publicKey = fs.readFileSync(process.env.PUBKEY_PATH);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sign data into JWT with JWT env secret
|
* Sign data into JWT with JWT env secret
|
||||||
@ -10,7 +10,7 @@ const publicKey = fs.readFileSync(process.env.PUBKEY);
|
|||||||
* @returns {jwt.JwtPayload}
|
* @returns {jwt.JwtPayload}
|
||||||
*/
|
*/
|
||||||
const getJWT = (data) => {
|
const getJWT = (data) => {
|
||||||
return jwt.sign({ id: data }, process.env.JWTSECRET, { algorithm: "HS256" }); // symmetric encryption, so simple secret with SHA
|
return jwt.sign({ id: data }, process.env.JWTSECRET, { algorithm: "HS256" }); // symmetric encryption, so simple secret with SHA
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -19,13 +19,7 @@ const getJWT = (data) => {
|
|||||||
* @returns {jwt.JwtPayload}
|
* @returns {jwt.JwtPayload}
|
||||||
*/
|
*/
|
||||||
const getSignedJWT = (data) => {
|
const getSignedJWT = (data) => {
|
||||||
return jwt.sign(
|
return jwt.sign({ id: data }, privateKey, { algorithm: "RS256" }); // asymmetric signing, so private key with RSA
|
||||||
{ id: data },
|
|
||||||
privateKey,
|
|
||||||
{
|
|
||||||
algorithm: "RS256", // asymmetric signing, so private key with RSA
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -34,7 +28,7 @@ const getSignedJWT = (data) => {
|
|||||||
* @returns {string|any}
|
* @returns {string|any}
|
||||||
*/
|
*/
|
||||||
const verifyJWT = (data) => {
|
const verifyJWT = (data) => {
|
||||||
return jwt.verify(data, process.env.JWTSECRET, { algorithms: ["HS256"] });
|
return jwt.verify(data, process.env.JWTSECRET, { algorithms: ["HS256"] });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -43,18 +37,12 @@ const verifyJWT = (data) => {
|
|||||||
* @returns {string|any}
|
* @returns {string|any}
|
||||||
*/
|
*/
|
||||||
const verifySignedJWT = (signedString) => {
|
const verifySignedJWT = (signedString) => {
|
||||||
return jwt.verify(
|
return jwt.verify(signedString, publicKey, { algorithms: ["RS256"] });
|
||||||
signedString,
|
|
||||||
publicKey,
|
|
||||||
{
|
|
||||||
algorithms: ["RS256"]
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getJWT,
|
getJWT,
|
||||||
verifyJWT,
|
verifyJWT,
|
||||||
getSignedJWT,
|
getSignedJWT,
|
||||||
verifySignedJWT,
|
verifySignedJWT,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,7 +1,9 @@
|
|||||||
const { validationResult } = require("express-validator");
|
const { validationResult } = require("express-validator");
|
||||||
|
|
||||||
const typedefs = require("../typedefs");
|
|
||||||
const { getNestedValuesString } = require("../utils/jsonTransformer");
|
const { getNestedValuesString } = require("../utils/jsonTransformer");
|
||||||
|
const logger = require("../utils/logger")(module);
|
||||||
|
|
||||||
|
const typedefs = require("../typedefs");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Refer: https://stackoverflow.com/questions/58848625/access-messages-in-express-validator
|
* Refer: https://stackoverflow.com/questions/58848625/access-messages-in-express-validator
|
||||||
@ -16,7 +18,7 @@ const validate = (req, res, next) => {
|
|||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
const extractedErrors = []
|
const extractedErrors = [];
|
||||||
errors.array().forEach(err => {
|
errors.array().forEach(err => {
|
||||||
if (err.type === 'alternative') {
|
if (err.type === 'alternative') {
|
||||||
err.nestedErrors.forEach(nestedErr => {
|
err.nestedErrors.forEach(nestedErr => {
|
||||||
@ -31,12 +33,14 @@ const validate = (req, res, next) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(400).send({
|
res.status(400).json({
|
||||||
message: getNestedValuesString(extractedErrors),
|
message: getNestedValuesString(extractedErrors),
|
||||||
errors: extractedErrors
|
errors: extractedErrors
|
||||||
})
|
});
|
||||||
|
logger.warn("invalid request", { extractedErrors });
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
validate,
|
validate
|
||||||
}
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user