-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style: adjust code style of middlewares
- Loading branch information
Showing
2 changed files
with
51 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |