-
Notifications
You must be signed in to change notification settings - Fork 31
PR - Happy Thouhts API #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
| import jwt from "jsonwebtoken"; | ||
| import { User } from "../models/User.js"; | ||
|
|
||
| const JWT_SECRET = process.env.JWT_SECRET || "supersecret"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoded fallback secret can be removed for security reasons.
| const token = jwt.sign( | ||
| { userId: user._id, email: user.email }, | ||
| JWT_SECRET, | ||
| { expiresIn: "2h" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem to have 2 hour duration but next step would be refreshed token.
| }; | ||
|
|
||
| // PATCH /thoughts/:id/unlike | ||
| export const unlikeThought = async (req, res) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good safety code that thoughts cant go negative. Keeping things positive or neutral at least.
Update: While logged in I can spam my own like-button and it does to negative -1 and -2 I managed to get.
| thoughtsQuery = thoughtsQuery.sort({ hearts: 1 }); | ||
|
|
||
| const total = await Thought.countDocuments(query); | ||
| const results = await thoughtsQuery |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do coercion on both number and page instead of just number.
"const { message, minHearts, sort, page = 1, limit = 10 } = req.query;
const pageNum = Number(page);
const limitNum = Number(limit);
.skip((pageNum - 1) * limitNum)
.limit(limitNum);"
| } | ||
| }; | ||
|
|
||
| // DELETE /thoughts/:id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The user can not delete thoughts if they're not logged in - potential new feature? Connecting it to the session ID or similar?
| // connect to mongoDB then start server | ||
| connectToDatabase().then(() => { | ||
| app.listen(port, () => { | ||
| console.log(`Server running on http://localhost:${port}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catch error here?
oskarnordin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very clean and easily understandable code. Good comments and good setup. Very minor changes can be made.
JennieDalgren
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⭐
https://js-project-api-cathi.onrender.com