generated from Technigo/express-api-starter
-
Notifications
You must be signed in to change notification settings - Fork 31
Happy Thoughts API - Malin #21
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
Open
MalLunBar
wants to merge
28
commits into
Technigo:master
Choose a base branch
from
MalLunBar:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
fa48a58
install packages
MalLunBar b11e599
creating endpoint to find specific thought
MalLunBar 094078a
create an endpoint to serch for different amounts of likes
MalLunBar a09e7b8
cleanup
MalLunBar ba24a8e
install mongoose and start using
MalLunBar 0e33a1a
new import
MalLunBar 53f890d
add a delete thought endpoint
MalLunBar 03dc5a7
add a post
MalLunBar b1b437f
add default number to hearts
MalLunBar aa05d01
Add func to patch
MalLunBar 4ba6d9a
text
MalLunBar 0358632
test
MalLunBar 92a04bc
functinality for edit
MalLunBar 22a4b94
fix bug in edit
MalLunBar f3b7814
Add correct endpoint
MalLunBar 89ebfd3
correcting error
MalLunBar dcfdfa8
trying to text bug
MalLunBar b1fa723
add authentication for user
MalLunBar 41db274
break out and structure the code
MalLunBar dfa7571
modify login. Delete name as required
MalLunBar 438de94
connect specific message to specific user + find and fix bug
MalLunBar 6e11864
up the security
MalLunBar 68aafd1
Fix bug
MalLunBar fd99adf
make get function to get liked messages
MalLunBar 00c0807
fix the count of likes
MalLunBar cab7ae0
fix show liked messages bug
MalLunBar 68bcbd2
delete console.logs
MalLunBar 8d8e1ce
clean
MalLunBar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { User } from '../models/User.js' | ||
|
|
||
|
|
||
| export const authenticateUser = async (req, res, next) => { | ||
| try { | ||
| const accessToken = req.header("Authorization") | ||
| const user = await User.findOne({ accessToken: accessToken }) | ||
| if (user) { | ||
| req.user = user | ||
| next() | ||
| } else { | ||
| res.status(401).json({ | ||
| message: "Authentication missing or invalid.", | ||
| loggedOut: true | ||
| }) | ||
| } | ||
| } catch (error) { | ||
| res.status(500).json({ | ||
| message: "Internal server error", | ||
| error: error.message | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| export const authenticateUserLike = async (req, res, next) => { | ||
| try { | ||
| const accessToken = req.header("Authorization") | ||
| if (accessToken) { | ||
| const user = await User.findOne({ accessToken: accessToken }) | ||
| if (user) { | ||
| req.user = user | ||
| } else { | ||
| return res.status(401).json({ | ||
| message: "Authentication missing or invalid.", | ||
| loggedOut: true | ||
| }) | ||
| } | ||
| } | ||
| next() | ||
| } catch (error) { | ||
| res.status(500).json({ | ||
| message: "Internal server error", | ||
| error: error.message | ||
| }) | ||
| } | ||
| } |
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤟👾✅ |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import mongoose from "mongoose" | ||
|
|
||
| const likeSchema = new mongoose.Schema({ | ||
| user: { | ||
| type: mongoose.Schema.Types.ObjectId, | ||
| ref: "User", | ||
| required: true | ||
| }, | ||
| thought: { | ||
| type: mongoose.Schema.Types.ObjectId, | ||
| ref: "Thought", | ||
| required: true | ||
| }, | ||
| createdAt: { | ||
| type: Date, | ||
| default: Date.now | ||
| } | ||
| }) | ||
|
|
||
| // will make sure a user can only like a thought one time | ||
| // | ||
| likeSchema.index({ user: 1, thought: 1 }, { unique: true }) | ||
|
|
||
| export const Like = mongoose.model("Like", likeSchema) |
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤟👾✅ |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import mongoose from "mongoose" | ||
|
|
||
| const thoughtSchema = new mongoose.Schema({ | ||
|
|
||
| message: { | ||
| type: String, | ||
| required: true, | ||
| minLength: 5, | ||
| maxLength: 140 | ||
| }, | ||
| hearts: { | ||
| type: Number, | ||
| default: 0 | ||
| }, | ||
| createdAt: { | ||
| type: Date, | ||
| default: Date.now | ||
| }, | ||
| user: { | ||
| type: mongoose.Schema.Types.ObjectId, | ||
| ref: "User", | ||
| required: true | ||
| }, | ||
|
|
||
| }) | ||
|
|
||
| export const Thought = mongoose.model("Thought", thoughtSchema) |
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤟👾✅ |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import mongoose from "mongoose" | ||
| import crypto from "crypto" | ||
|
|
||
|
|
||
| // Create a schema for the users | ||
| const userSchema = new mongoose.Schema({ | ||
| name: { | ||
| type: String, | ||
| required: true, | ||
|
|
||
| }, | ||
| email: { | ||
| type: String, | ||
| required: true, | ||
| unique: true | ||
| }, | ||
| password: { | ||
| type: String, | ||
| required: true | ||
| }, // Passwords should be hashed in a real application | ||
| accessToken: { | ||
| type: String, | ||
| default: crypto.randomBytes(128).toString("hex") | ||
| } // Generate a random access token | ||
|
|
||
| }) | ||
|
|
||
| export const User = mongoose.model("User", userSchema) |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
clear code! This is a speculation, im not really sure how one could do that, but the two authentications functions seems similar i wonder if there is a way to write one function, but maybe would make the code more complicated idk just thinking