Skip to content

Commit

Permalink
Add checker in order to start authentication when configured (#9)
Browse files Browse the repository at this point in the history
In this commit was added the capability of run this server
when the user doesn't configured the facebook oauth service
and this should be acessible without anykind of authentication.

NOTE: This should be used only in dev mode
  • Loading branch information
Victor Fernandes committed Mar 15, 2020
1 parent 15541d8 commit d563207
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
12 changes: 9 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ dotenv.config();

const logger = require('./utils/logger');
const { apiErrorHandler, resourceNotFoundHandler, isAuthenticated } = require('./utils/express-middleware');
const { hasAuthentication } = require('./utils/common');

const api = require('./api');
const auth = require('./auth');

const app = express();
const serverLogger = logger('server');

app.use(session({
secret: process.env.APP_SESSION_SALT,
Expand All @@ -22,7 +23,12 @@ app.use(session({
saveUninitialized: true
}));

auth(app);
if (hasAuthentication()) {
const auth = require('./auth');

serverLogger.info('Server started with authentication configured');
auth(app);
}

// Add some basic security
app.use(helmet());
Expand All @@ -46,4 +52,4 @@ const port = process.env.APP_SERVER_PORT || 3000; // set our port
// Start the server
app.listen(port);

logger('server').info(`trackovid-19 server started on port ${port}`);
serverLogger.info(`trackovid-19 server started on port ${port}`);
13 changes: 13 additions & 0 deletions utils/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function hasFacebookCredentials () {
return process.env.FB_APP_ID !== undefined &&
process.env.FB_APP_SECRET !== undefined &&
process.env.FB_CALLBACK_URL !== undefined;
}

function hasAuthentication () {
return hasFacebookCredentials();
}

module.exports = {
hasAuthentication
};
3 changes: 2 additions & 1 deletion utils/express-middleware.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const logger = require('./logger');
const HTTP_STATUS_CODE = require('../enums/http-status-code');
const API_ERRORS = require('../enums/api-errors');
const { hasAuthentication } = require('../utils/common');

/**
* Handles the unexpected API errors
Expand Down Expand Up @@ -50,7 +51,7 @@ function resourceNotFoundHandler (req, res, next) {
* with the unauthorized status code.
*/
function isAuthenticated (req, res, next) {
if (req.isAuthenticated() || process.env.MOCK_SERVICES === "true") {
if (req.isAuthenticated() || !hasAuthentication() || process.env.MOCK_SERVICES === "true") {
next();
} else {
logger('express-middleware-auth').info(
Expand Down

0 comments on commit d563207

Please sign in to comment.