From 2d4ac3d639ca9466e85e379bb607a1786851d0fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=ADcolas=20=27nohaleh=27=20De=C3=A7ordi?= Date: Wed, 13 May 2020 13:49:07 -0300 Subject: [PATCH] style: adjust code style of middlewares --- src/session/middleware.js | 44 +++++++++++++++--------------- src/user/middleware.js | 56 ++++++++++++++++++++------------------- 2 files changed, 51 insertions(+), 49 deletions(-) diff --git a/src/session/middleware.js b/src/session/middleware.js index abb8cdd..a1ebdff 100644 --- a/src/session/middleware.js +++ b/src/session/middleware.js @@ -1,30 +1,30 @@ const jwt = require('jsonwebtoken'); const checkAuthorization = async (req, res, next) => { - const { authorization } = req.headers; + const { authorization } = req.headers; - if (!authorization) { - return res.status(401).json({ - error: 401, - data: { - message: 'Unauthorized', - }, - }); - } + if (!authorization) { + return res.status(401).json({ + error: 401, + data: { + message: 'Unauthorized', + }, + }); + } - const [bearer, token] = authorization.split(' '); - try { - const verification = await jwt.verify(token, process.env.JWT_SECRET); - req.auth_user_id = verification.id; - } catch (err) { - res.status(401).json({ - error: 401, - data: { - message: 'Unauthorized', - }, - }); - } - return next(); + const [bearer, token] = authorization.split(' '); + try { + const verification = await jwt.verify(token, process.env.JWT_SECRET); + req.auth_user_id = verification.id; + } catch (err) { + res.status(401).json({ + error: 401, + data: { + message: 'Unauthorized', + }, + }); + } + return next(); }; module.exports = { checkAuthorization }; diff --git a/src/user/middleware.js b/src/user/middleware.js index 6d12fef..4cc3ffc 100644 --- a/src/user/middleware.js +++ b/src/user/middleware.js @@ -1,40 +1,42 @@ +/* eslint-disable no-else-return */ + const bcrypt = require('bcryptjs'); const User = require('./model'); const encryptPassword = async (req, res, next) => { - const { password = null } = req.body; + const { password = null } = req.body; - if (password) { - const password_hash = await bcrypt.hash(password, 10); + if (password) { + const password_hash = await bcrypt.hash(password, 10); - req.body.password_hash = password_hash; - } + req.body.password_hash = password_hash; + } - return next(); + return next(); }; const checkAccountStatus = async (req, res, next) => { - const { auth_user_id } = req; - - const user = await User.getOne(auth_user_id); - - if (!user) { - return res.json({ - error: 403, - data: { - message: "Forbidden", - }, - }); - } else if (user && user.deleted_at) { - return res.json({ - error: 403, - data: { - message: 'Your user account is disabled', - }, - }); - } - - return next(); + const { auth_user_id } = req; + + const user = await User.getOne(auth_user_id); + + if (!user) { + return res.json({ + error: 403, + data: { + message: 'Forbidden', + }, + }); + } else if (user && user.deleted_at) { + return res.json({ + error: 403, + data: { + message: 'Your user account is disabled', + }, + }); + } + + return next(); }; module.exports = { checkAccountStatus, encryptPassword };