Skip to content

Commit

Permalink
refactor code to es6
Browse files Browse the repository at this point in the history
  • Loading branch information
antonybudianto committed Nov 19, 2017
1 parent 2ad3de7 commit 04a69cb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
14 changes: 6 additions & 8 deletions src/auth.middleware.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
const admin = require.main.require('firebase-admin');
import { log } from './util/logger';

const logger = require('./util/logger');
const admin = require.main.require('firebase-admin');

function firebaseAuthMiddleware(req, res, next) {
export default function firebaseAuthMiddleware(req, res, next) {
const authorization = req.header('Authorization');
if (authorization) {
let token = authorization.split(' ');
admin.auth().verifyIdToken(token[1])
.then((decodedToken) => {
logger.log(decodedToken);
log(decodedToken);
res.locals.user = decodedToken;
next();
})
.catch(err => {
logger.log(err);
log(err);
res.sendStatus(401);
});
} else {
logger.log('Authorization header is not found');
log('Authorization header is not found');
res.sendStatus(401);
}
}

module.exports = firebaseAuthMiddleware;
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const auth = require('./auth.middleware');
import auth from './auth.middleware';

module.exports = {
export {
auth
};
}
6 changes: 1 addition & 5 deletions src/util/logger.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
const debug = (process.env.APP_DEBUG === 'true');

function log() {
export function log() {
if (!debug) return;

console.log(...arguments);
}

module.exports = {
log
};

0 comments on commit 04a69cb

Please sign in to comment.