serverpreconfigured 1.1.4 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ const path = require('path');
2
+
3
+ module.exports = {
4
+ 'config': path.resolve(__dirname,'node_modules','serverpreconfigured','dist','settings','database','database.js'),
5
+ 'migrations-path': path.resolve(__dirname, 'migrations'),
6
+ }
@@ -9,7 +9,7 @@ const env_1 = __importDefault(require("../settings/env"));
9
9
  function initSessions(app) {
10
10
  app.set('trust proxy', 1); // trust first proxy
11
11
  app.use((0, express_session_1.default)({
12
- secret: 'keyboard cat',
12
+ secret: env_1.default.SESSION_SECRET,
13
13
  resave: false,
14
14
  saveUninitialized: true,
15
15
  cookie: { secure: env_1.default.NODE_ENV == 'development' ? false : true,
@@ -58,6 +58,7 @@ router.post('/login', (req, res) => __awaiter(void 0, void 0, void 0, function*
58
58
  }
59
59
  if (password == "" || email == "")
60
60
  return res.send((0, response_1.JSONResponse)(false, LoginErrorCode.InvalidParams, "Must have 'email' and 'password' params"));
61
+ email = email.toLocaleLowerCase();
61
62
  try {
62
63
  const checkPass = yield (0, users_1.checkUserPassword)(email, password);
63
64
  if (checkPass) {
@@ -80,6 +81,7 @@ router.post('/register', (req, res) => __awaiter(void 0, void 0, void 0, functio
80
81
  let name = meta_sanitizer_1.default.SanitizerEngine(req.body.name || '', true, false, [' ']).sanitizedData;
81
82
  if (email == "" || password == "" || name == "")
82
83
  return res.send((0, response_1.JSONResponse)(false, RegisterUserErrorCode.InvalidParams, "Invalid params"));
84
+ email = email.toLocaleLowerCase();
83
85
  yield (0, users_2.createUser)({ first_name: name, email: email, password_string: password });
84
86
  return res.send((0, response_1.JSONResponse)(true, RegisterUserErrorCode.NoError, "", "REGISTER OK"));
85
87
  }
@@ -1,6 +1,6 @@
1
1
  export const dialect: string;
2
2
  export const host: string;
3
- export const database: string;
3
+ export const database: any;
4
4
  export const username: string;
5
5
  export const password: string;
6
6
  export namespace define {
@@ -5,9 +5,10 @@ declare const ENV: {
5
5
  DATABASE: {
6
6
  dialect: string;
7
7
  host: string;
8
- database: string;
8
+ database: any;
9
9
  username: string;
10
10
  password: string;
11
11
  };
12
+ SESSION_SECRET: string;
12
13
  };
13
14
  export default ENV;
@@ -1,5 +1,18 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const fs_1 = __importDefault(require("fs"));
7
+ const path_1 = __importDefault(require("path"));
8
+ const custom_env_path = path_1.default.join(process.cwd(), "spc_envfile.json");
9
+ var custom_env = {};
10
+ try {
11
+ custom_env = JSON.parse(fs_1.default.readFileSync(custom_env_path).toString());
12
+ }
13
+ catch (e) {
14
+ custom_env = {};
15
+ }
3
16
  const ENV = {
4
17
  NODE_ENV: process.env.SERVER_ENV == 'development' ? 'development' : 'production',
5
18
  ALLOW_CORS: process.env.ALLOW_CORS == 'ALLOW' ? true : false,
@@ -7,9 +20,10 @@ const ENV = {
7
20
  DATABASE: {
8
21
  dialect: process.env.DATABASE_DIALECT ? process.env.DATABASE_DIALECT : 'postgres',
9
22
  host: process.env.DATABASE_HOST ? process.env.DATABASE_HOST : 'localhost',
10
- database: process.env.DATABASE_DATABASE ? process.env.DATABASE_DATABASE : 'postgres',
23
+ database: (custom_env.DATABASE && custom_env.DATABASE.DATABASE) || (process.env.DATABASE_DATABASE || 'postgres'),
11
24
  username: process.env.DATABASE_USERNAME ? process.env.DATABASE_USERNAME : 'postgres',
12
25
  password: process.env.DATABASE_PASSWORD ? process.env.DATABASE_PASSWORD : '',
13
- }
26
+ },
27
+ SESSION_SECRET: process.env.SESSION_SECRET ? process.env.SESSION_SECRET : "secret key session",
14
28
  };
15
29
  exports.default = ENV;
@@ -10,6 +10,6 @@ function debugMessage(isDebug, message, data = {}) {
10
10
  console.log("----DEBUG----" +
11
11
  "\nCaller: " + stack[2] +
12
12
  "\nMessage: " + message +
13
- addData ? "\naddData: " + addData : "");
13
+ (addData ? "\naddData: " + addData : ""));
14
14
  }
15
15
  exports.debugMessage = debugMessage;
package/install.js ADDED
@@ -0,0 +1,100 @@
1
+ #!/usr/bin/env node
2
+ const LIB_SEQUELIZE_FILENAME=".sequelizercproj";
3
+ const PROJECT_SEQUELIZE_FILENAME=".sequelizerc";
4
+ const MIGRATIONS_PATH="migrations";
5
+ const LIB_DIR=__dirname;
6
+ const PROJECT_DIR=process.cwd();
7
+ const CUSTOMENV_FILENAME="spc_envfile.json";
8
+ const fs=require("fs");
9
+ const path=require("path");
10
+ const color=require('colors-cli');
11
+ const PRINTWARNING=color.yellow;
12
+ const PRINTERROR=color.red;
13
+ const PRINTOK=color.green;
14
+ (async()=>{
15
+ try{
16
+ console.log(PRINTWARNING("START UPDATING MIGRATIONS FILE"));
17
+ await updateMigrations();
18
+ console.log(PRINTOK("MIGRATIONS FILE UPDATED"))
19
+ }catch(e){
20
+ console.log(PRINTERROR("UPDATE MIGRATIONS FILE ERROR"));
21
+ console.log(e);
22
+ }
23
+ try{
24
+ console.log(PRINTWARNING("START COPY SEQUELIZERC FILE IF NOT EXISTS"));
25
+ await copySequelizeRcFile();
26
+ console.log(PRINTOK("COPY SEQUELIZERCFILE OK"));
27
+ }catch(e){
28
+ console.log(PRINTERROR("COPY SEQUELIZERCFILE ERROR"));
29
+ console.log(e);
30
+ }
31
+ try{
32
+ console.log(PRINTWARNING("COPY CUSTOM ENV FILE IF NOT EXISTS"));
33
+ await copyCustomEnvFile();
34
+ console.log(PRINTOK("COPY CUSTOM ENV FILE OK"));
35
+ }catch(e){
36
+ console.log(PRINTERROR("COPY CUSTOM ENV FILE ERROR"));
37
+ console.log(e);
38
+ }
39
+
40
+ })()
41
+
42
+
43
+ async function updateMigrations(){
44
+ try{
45
+ await createPath();
46
+ await copyFiles();
47
+ }catch(e){
48
+ throw e;
49
+ }
50
+
51
+ async function createPath(){
52
+ try{
53
+ if(fs.existsSync(getProjectMigrationDirPath())){
54
+ return true;
55
+ }else{
56
+ fs.mkdirSync(getProjectMigrationDirPath());
57
+ }
58
+ }catch(e){
59
+ throw e;
60
+ }
61
+ }
62
+ async function copyFiles(){
63
+ const files=fs.readdirSync(getLibMigrationDirPath());
64
+ for(let file of files){
65
+ const projectFileName=path.join(getProjectMigrationDirPath(),file);
66
+ if(fs.existsSync(projectFileName))
67
+ continue;
68
+ const libFileName=path.join(getLibMigrationDirPath(),file);
69
+ fs.copyFileSync(libFileName,projectFileName);
70
+ }
71
+ }
72
+ function getProjectMigrationDirPath(){
73
+ return path.join(PROJECT_DIR,MIGRATIONS_PATH);
74
+ }
75
+ function getLibMigrationDirPath(){
76
+ return path.join(LIB_DIR,MIGRATIONS_PATH);
77
+ }
78
+ }
79
+ async function copySequelizeRcFile(){
80
+ try{
81
+ const proj_seq=path.join(PROJECT_DIR,PROJECT_SEQUELIZE_FILENAME);
82
+ if(fs.existsSync(proj_seq))
83
+ return;
84
+ const lib_seq=path.join(LIB_DIR,LIB_SEQUELIZE_FILENAME);
85
+ fs.copyFileSync(lib_seq,proj_seq);
86
+ }catch(e){
87
+ throw e;
88
+ }
89
+ }
90
+ async function copyCustomEnvFile(){
91
+ try{
92
+ const proj_env_custom_filename=path.join(PROJECT_DIR,CUSTOMENV_FILENAME);
93
+ if(fs.existsSync(proj_env_custom_filename))
94
+ return;
95
+ const lib_env_custom_filename=path.join(LIB_DIR,CUSTOMENV_FILENAME);
96
+ fs.copyFileSync(lib_env_custom_filename,proj_env_custom_filename);
97
+ }catch(e){
98
+ throw e;
99
+ }
100
+ }
package/package.json CHANGED
@@ -1,10 +1,18 @@
1
1
  {
2
2
  "name": "serverpreconfigured",
3
- "version": "1.1.4",
3
+ "version": "2.0.0",
4
4
  "description": "\"Pre-configured server with authentication system and database integration\"",
5
5
  "main": "dist/server.js",
6
- "keywords": ["server","pre configured","database","authentication"],
6
+ "keywords": [
7
+ "server",
8
+ "pre configured",
9
+ "database",
10
+ "authentication"
11
+ ],
7
12
  "type": "commonjs",
13
+ "bin": {
14
+ "serverpreconfigured": "./install.js"
15
+ },
8
16
  "scripts": {
9
17
  "test": "echo \"Error: no test specified\" && exit 1",
10
18
  "start": "node ./dist/server.js",
@@ -18,6 +26,7 @@
18
26
  "@types/express-session": "^1.17.4",
19
27
  "argon2": "^0.28.5",
20
28
  "body-parser": "^1.19.2",
29
+ "colors-cli": "^1.0.28",
21
30
  "cors": "^2.8.5",
22
31
  "express": "^4.17.3",
23
32
  "express-session": "^1.17.2",
@@ -0,0 +1,5 @@
1
+ {
2
+ "DATABASE":{
3
+ "DATABASE":false
4
+ }
5
+ }
@@ -3,7 +3,7 @@ import ENV from "../settings/env";
3
3
  export function initSessions(app:any){
4
4
  app.set('trust proxy', 1) // trust first proxy
5
5
  app.use(session({
6
- secret: 'keyboard cat',
6
+ secret: ENV.SESSION_SECRET,
7
7
  resave: false,
8
8
  saveUninitialized: true,
9
9
  cookie: { secure: ENV.NODE_ENV=='development'?false:true,
@@ -43,6 +43,7 @@ router.post('/login',async (req,res)=>{
43
43
  }
44
44
  if(password==""||email=="")
45
45
  return res.send(JSONResponse(false,LoginErrorCode.InvalidParams,"Must have 'email' and 'password' params"));
46
+ email=email.toLocaleLowerCase();
46
47
  try{
47
48
  const checkPass=await checkUserPassword(email,password);
48
49
  if(checkPass){
@@ -65,6 +66,7 @@ router.post('/register',async (req,res)=>{
65
66
  let name=meta_sanitizer.SanitizerEngine(req.body.name||'',true,false,[' ']).sanitizedData;
66
67
  if(email=="" || password=="" || name=="")
67
68
  return res.send(JSONResponse(false,RegisterUserErrorCode.InvalidParams,"Invalid params"));
69
+ email=email.toLocaleLowerCase();
68
70
  await createUser({first_name:name,email:email,password_string:password});
69
71
  return res.send(JSONResponse(true,RegisterUserErrorCode.NoError,"","REGISTER OK"));
70
72
  }catch(e){
@@ -1,3 +1,12 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ const custom_env_path=path.join(process.cwd(),"spc_envfile.json");
4
+ var custom_env:any={};
5
+ try{
6
+ custom_env=JSON.parse(fs.readFileSync(custom_env_path).toString());
7
+ }catch(e){
8
+ custom_env={};
9
+ }
1
10
  const ENV={
2
11
  NODE_ENV:process.env.SERVER_ENV=='development'?'development':'production',
3
12
  ALLOW_CORS:process.env.ALLOW_CORS=='ALLOW'?true:false,
@@ -5,9 +14,10 @@ const ENV={
5
14
  DATABASE:{
6
15
  dialect:process.env.DATABASE_DIALECT?process.env.DATABASE_DIALECT:'postgres',
7
16
  host:process.env.DATABASE_HOST?process.env.DATABASE_HOST:'localhost',
8
- database:process.env.DATABASE_DATABASE?process.env.DATABASE_DATABASE:'postgres',
17
+ database:(custom_env.DATABASE&&custom_env.DATABASE.DATABASE)||(process.env.DATABASE_DATABASE||'postgres'),
9
18
  username:process.env.DATABASE_USERNAME?process.env.DATABASE_USERNAME:'postgres',
10
19
  password:process.env.DATABASE_PASSWORD?process.env.DATABASE_PASSWORD:'',
11
- }
20
+ },
21
+ SESSION_SECRET:process.env.SESSION_SECRET?process.env.SESSION_SECRET:"secret key session",
12
22
  };
13
23
  export default ENV;
@@ -7,6 +7,6 @@ export function debugMessage(isDebug:boolean,message:string,data:any={}){
7
7
  console.log("----DEBUG----"+
8
8
  "\nCaller: "+stack[2]+
9
9
  "\nMessage: "+message+
10
- addData?"\naddData: "+addData:""
10
+ (addData?"\naddData: "+addData:"")
11
11
  );
12
12
  }