Skip to content

code review start #142

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

fletchulence
Copy link

No description provided.

return next({ status: 401, message: 'Token required'})
}
jwt.verify(token, JWT_SECRET, (err, decode)=>{
if(err){
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesnt match above white space

/*
If the username in req.body does NOT exist in the database
status 401
{
"message": "Invalid credentials"
}
*/
const { username } = req.body
try{
const dbUsername = await User.findBy({ username })
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dbUsername actually is a collection == dbUsers

returns an array

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generalize

@@ -62,6 +93,21 @@ const validateRoleName = (req, res, next) => {
"message": "Role name can not be longer than 32 chars"
}
*/
let { role_name } = req.body
// let role_name = role_name.trim()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

take out the comments as normal --- delete it!

@@ -62,6 +93,21 @@ const validateRoleName = (req, res, next) => {
"message": "Role name can not be longer than 32 chars"
}
*/
let { role_name } = req.body
// let role_name = role_name.trim()
if( !role_name || role_name.trim() === undefined ){
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after || will never be called

next()
} else {
if ( role_name.trim() === 'admin'){
return next({ status: 422, message: 'Role name can not be admin'})
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return is not necessary if you have "else/else if" right after...
change one or the other

@@ -1,8 +1,12 @@
const router = require("express").Router();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

be consistent with semicolons !!

const { checkUsernameExists, validateRoleName } = require('./auth-middleware');
const { JWT_SECRET } = require("../secrets"); // use this secret!
const { BCRYPT_ROUNDS } = require("../secrets"); // use this secret!
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will the comment be a lifesaver here??

if yes, leave in
if not, take out

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no misleading comments

const hash = bcrypt.hashSync(password, BCRYPT_ROUNDS)
const role = req.role_name
try {
const newUser = await User.add({ username, password: hash, role_name: role })
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep lines under 100 at least ((maybe 80 if possible))

@@ -37,6 +51,20 @@ router.post("/#", checkUsernameExists, (req, res, next) => {
"role_name": "admin" // the role of the authenticated user
}
*/

try {
let user = req.user
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could have destructured

return db('users as u')
.leftJoin('roles as r', 'r.role_id', 'u.role_id')
.select('u.*', 'r.*')
.where(filter).first()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is only finding the first in the array

"knex": "^0.95.14",
"sqlite3": "^5.0.2"
"jsonwebtoken": "^8.5.1",
"knex": "^1.0.1"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding risk by doing this -- might be increasing the possibility of errs

only if you need to use the new version

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant