serverpreconfigured 1.2.0 → 2.1.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
+ }
@@ -1 +1,3 @@
1
1
  export declare function userIsLogged(req: any): boolean;
2
+ export declare function setUserLogged(req: any, email: string): void;
3
+ export declare function logoutUser(req: any): void;
package/dist/auth/auth.js CHANGED
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.userIsLogged = void 0;
3
+ exports.logoutUser = exports.setUserLogged = exports.userIsLogged = void 0;
4
4
  const secureget_1 = require("../sessions/secureget");
5
5
  const config_1 = require("./config");
6
+ const server_1 = require("../server");
7
+ const server_2 = require("../server");
6
8
  function userIsLogged(req) {
7
9
  let user = (0, secureget_1.getSessionValue)(req, config_1.SESSION_LOGGED_DATA);
8
10
  if (user)
@@ -10,3 +12,11 @@ function userIsLogged(req) {
10
12
  return false;
11
13
  }
12
14
  exports.userIsLogged = userIsLogged;
15
+ function setUserLogged(req, email) {
16
+ (0, server_1.setSessionValue)(req, config_1.SESSION_LOGGED_DATA, email);
17
+ }
18
+ exports.setUserLogged = setUserLogged;
19
+ function logoutUser(req) {
20
+ (0, server_2.deleteSessionValue)(req, config_1.SESSION_LOGGED_DATA);
21
+ }
22
+ exports.logoutUser = logoutUser;
@@ -13,8 +13,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const express_1 = __importDefault(require("express"));
16
- const secureset_1 = require("../sessions/secureset");
17
- const config_1 = require("../auth/config");
18
16
  const response_1 = require("../utils/response");
19
17
  const auth_1 = require("../auth/auth");
20
18
  const meta_sanitizer_1 = __importDefault(require("meta-sanitizer"));
@@ -22,6 +20,8 @@ const users_1 = require("../users/users");
22
20
  const users_2 = require("../users/users");
23
21
  const auth_2 = require("../middlewares/auth");
24
22
  const env_1 = __importDefault(require("../settings/env"));
23
+ const auth_3 = require("../auth/auth");
24
+ const auth_4 = require("../auth/auth");
25
25
  const DEBUG = env_1.default.NODE_ENV === 'development' ? true : false;
26
26
  var LoginErrorCode;
27
27
  (function (LoginErrorCode) {
@@ -41,7 +41,7 @@ const router = express_1.default.Router();
41
41
  router.post('/logout', (req, res) => {
42
42
  let is_ok = false;
43
43
  if ((0, auth_1.userIsLogged)(req)) {
44
- (0, secureset_1.deleteSessionValue)(req, config_1.SESSION_LOGGED_DATA);
44
+ (0, auth_4.logoutUser)(req);
45
45
  is_ok = true;
46
46
  }
47
47
  res.send((0, response_1.JSONResponse)(is_ok, 0, is_ok ? "" : "User Must be logged", {}));
@@ -62,7 +62,7 @@ router.post('/login', (req, res) => __awaiter(void 0, void 0, void 0, function*
62
62
  try {
63
63
  const checkPass = yield (0, users_1.checkUserPassword)(email, password);
64
64
  if (checkPass) {
65
- (0, secureset_1.setSessionValue)(req, config_1.SESSION_LOGGED_DATA, email);
65
+ (0, auth_3.setUserLogged)(req, email);
66
66
  return res.send((0, response_1.JSONResponse)(true, LoginErrorCode.NoError, "Login Ok"));
67
67
  }
68
68
  return res.send((0, response_1.JSONResponse)(false, LoginErrorCode.InvalidPassword, "Invalid Password"));
@@ -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,7 +5,7 @@ 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
  };
@@ -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,7 +20,7 @@ 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
  },
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.2.0",
3
+ "version": "2.1.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
+ }
package/src/auth/auth.ts CHANGED
@@ -1,8 +1,16 @@
1
1
  import { getSessionValue } from "../sessions/secureget";
2
2
  import { SESSION_LOGGED_DATA } from "./config";
3
+ import { setSessionValue } from "../server";
4
+ import { deleteSessionValue } from "../server";
3
5
  export function userIsLogged(req:any):boolean{
4
6
  let user=getSessionValue(req,SESSION_LOGGED_DATA);
5
7
  if(user)
6
8
  return true;
7
9
  return false;
10
+ }
11
+ export function setUserLogged(req:any,email:string){
12
+ setSessionValue(req,SESSION_LOGGED_DATA,email);
13
+ }
14
+ export function logoutUser(req:any){
15
+ deleteSessionValue(req,SESSION_LOGGED_DATA);
8
16
  }
@@ -8,6 +8,8 @@ import { checkUserPassword } from "../users/users";
8
8
  import { createUser } from "../users/users";
9
9
  import { setUserDataMiddleware } from "../middlewares/auth";
10
10
  import ENV from "../settings/env";
11
+ import { setUserLogged } from "../auth/auth";
12
+ import { logoutUser } from "../auth/auth";
11
13
 
12
14
  const DEBUG=ENV.NODE_ENV==='development'?true:false;
13
15
  enum LoginErrorCode{
@@ -27,7 +29,7 @@ const router=express.Router();
27
29
  router.post('/logout',(req,res)=>{
28
30
  let is_ok=false;
29
31
  if(userIsLogged(req)){
30
- deleteSessionValue(req,SESSION_LOGGED_DATA);
32
+ logoutUser(req)
31
33
  is_ok=true;
32
34
  }
33
35
  res.send(JSONResponse(is_ok,0,is_ok?"":"User Must be logged",{}));
@@ -47,7 +49,7 @@ router.post('/login',async (req,res)=>{
47
49
  try{
48
50
  const checkPass=await checkUserPassword(email,password);
49
51
  if(checkPass){
50
- setSessionValue(req,SESSION_LOGGED_DATA,email);
52
+ setUserLogged(req,email);
51
53
  return res.send(JSONResponse(true,LoginErrorCode.NoError,"Login Ok"));
52
54
  }
53
55
  return res.send(JSONResponse(false,LoginErrorCode.InvalidPassword,"Invalid Password"));
@@ -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,7 +14,7 @@ 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
  },