Conversation
bwreid
left a comment
There was a problem hiding this comment.
This looks excellent, Charlotte. As mentioned in the comments, my biggest piece of advice to you would be to try and reduce the depth of your code. Let me know if you have any questions.
| const e = new Error('invalid login credentials') | ||
| e.status = 400 | ||
| next(e) | ||
| } |
There was a problem hiding this comment.
With try/catch, we could do something like throw new Error('invalid login credentials'). This would move the error to your .catch() and shorten the "depth" of your code.
| try { | ||
| const token = req.headers.authorization.split('Bearer ')[1] | ||
| const payload = jsonwebtoken.verify(token, SECRET_KEY) | ||
| if (payload) { //if payload (token verification) returns true, cont. |
There was a problem hiding this comment.
To make your code a bit easier to read, you can also build "guards" into your code. This means flipping the statement so that instead of looking for the path you want to go down, you try and kick the user out for any errors first. For example:
if (!payload) throw new Error(...)This can make your code much shorter.
| changeUser.admin = true | ||
| await changeUser.save() | ||
|
|
||
| res.json({ status, changeUser }) |
There was a problem hiding this comment.
Remember that if you use object shorthand, the key will match. So in this case, your response will have a key of "changeUser." I don't think this will make much sense to the end user as opposed to just "user."
No description provided.