use ENV variables

This commit is contained in:
Bilal Catic
2019-05-24 15:42:45 +02:00
parent aa3a4d8720
commit 304de9bd32
7 changed files with 41 additions and 13 deletions

2
.gitignore vendored
View File

@@ -2,3 +2,5 @@ node_modules
npm-debug.log npm-debug.log
.idea .idea
.env

View File

@@ -14,6 +14,9 @@ NOTE : If migration fails, try executing migrations manually with `npm run migra
If everything works correctly, it is possible to connect to database with : If everything works correctly, it is possible to connect to database with :
`psql -d CrmIntegration -p 5432 -U docker -h localhost` `psql -d CrmIntegration -p 5432 -U docker -h localhost`
2. Copy `environment.env` file to `.env` file and make appropriate changes.
`BASIC_AUTH_USERNAME` and `BASIC_AUTH_PASSWORD` are required for functional app.
3. Start server : `npm run start-server` 3. Start server : `npm run start-server`
4. Start client : `npm run start-client` 4. Start client : `npm run start-client`

2
environment.env Normal file
View File

@@ -0,0 +1,2 @@
BASIC_AUTH_USERNAME=username
BASIC_AUTH_PASSWORD=password

22
helpers/auth.js Normal file
View File

@@ -0,0 +1,22 @@
'use strict';
const basicAuth = require('express-basic-auth');
function myAuthorizer(username, password) {
if (!process.env.BASIC_AUTH_USERNAME || !process.env.BASIC_AUTH_PASSWORD){
return false;
}
const userMatches = basicAuth.safeCompare(username, process.env.BASIC_AUTH_USERNAME);
const passwordMatches = basicAuth.safeCompare(password, process.env.BASIC_AUTH_PASSWORD);
return userMatches & passwordMatches
}
function getUnauthorizedResponse(req) {
return 'Forbidden';
}
module.exports = {
myAuthorizer,
getUnauthorizedResponse,
};

5
package-lock.json generated
View File

@@ -664,6 +664,11 @@
"is-obj": "^1.0.0" "is-obj": "^1.0.0"
} }
}, },
"dotenv": {
"version": "8.0.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.0.0.tgz",
"integrity": "sha512-30xVGqjLjiUOArT4+M5q9sYdvuR4riM6yK9wMcas9Vbp6zZa+ocC9dp6QoftuhTPhFAiLK/0C5Ni2nou/Bk8lg=="
},
"dottie": { "dottie": {
"version": "2.0.1", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/dottie/-/dottie-2.0.1.tgz", "resolved": "https://registry.npmjs.org/dottie/-/dottie-2.0.1.tgz",

View File

@@ -31,6 +31,7 @@
}, },
"homepage": "https://gitlab.com/saburly/psihologija#README", "homepage": "https://gitlab.com/saburly/psihologija#README",
"dependencies": { "dependencies": {
"dotenv": "^8.0.0",
"express": "^4.17.0", "express": "^4.17.0",
"express-basic-auth": "^1.2.0", "express-basic-auth": "^1.2.0",
"pg": "^7.11.0", "pg": "^7.11.0",

View File

@@ -1,34 +1,26 @@
'use strict'; 'use strict';
require('dotenv').config();
const express = require("express"); const express = require("express");
const basicAuth = require('express-basic-auth'); const basicAuth = require('express-basic-auth');
const path = require('path'); const path = require('path');
const routes = require('./routes'); const routes = require('./routes');
const { myAuthorizer, getUnauthorizedResponse } = require('./helpers/auth');
const app = express(); const app = express();
const port = process.env.PORT || 5000; const port = process.env.PORT || 5000;
function myAuthorizer(username, password) {
const userMatches = basicAuth.safeCompare(username, 'senadU');
const passwordMatches = basicAuth.safeCompare(password, 'Tulipan*123*');
return userMatches & passwordMatches
}
function getUnauthorizedResponse(req) {
return 'Forbidden';
}
app.use('/api', routes); app.use('/api', routes);
app.use(basicAuth({ app.use(basicAuth({
authorizer: myAuthorizer, authorizer: myAuthorizer,
challenge: true, challenge: true,
unauthorizedResponse: getUnauthorizedResponse unauthorizedResponse: getUnauthorizedResponse
})); }));
//Static file declaration //Static file declaration
app.use(express.static(path.join(__dirname, 'client/build'))); app.use(express.static(path.join(__dirname, 'client/build')));
@@ -44,4 +36,5 @@ app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/client/public/index.html')); res.sendFile(path.join(__dirname + '/client/public/index.html'));
}); });
app.listen(port, () => console.log(`App running on port ${port}!`)); app.listen(port, () => console.log(`App running on port ${port}!`));