Skip to content

Commit

Permalink
style: adjust code style of middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
eowfenth committed May 13, 2020
1 parent ea4d19a commit 2d4ac3d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 49 deletions.
44 changes: 22 additions & 22 deletions src/session/middleware.js
Original file line number Diff line number Diff line change
@@ -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 };
56 changes: 29 additions & 27 deletions src/user/middleware.js
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit 2d4ac3d

Please sign in to comment.