generated from Technigo/express-api-starter
-
Notifications
You must be signed in to change notification settings - Fork 30
Happy Thoughts API - Tavan #26
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
T-Thiry
wants to merge
58
commits into
Technigo:master
Choose a base branch
from
T-Thiry: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
58 commits
Select commit
Hold shift + click to select a range
07c4574
added dependencies, imports and js code
ee05b6a
added comment
21ba718
removed flowers data
8dbed54
added type and installed devdependencies
508cd8d
removed flowerdata import
1a6ea13
added mongoose import and connection to mongo db database
1f77e01
addded gotenv import and config
3a639bd
added seed database
f5f7fb3
changed json message
4a6899e
Added thought and user routes
f769b97
moved code to thoughtRoutes file
836630b
added user and thought models
5ca0e0f
Created thought model
14100f9
Created user model with access token
a3d98c5
Set up Express router
83f2a43
Add GET /thoughts endpoint with query filtering
b764be3
Add GET /thoughts/:id endpoint
c635810
fixed mispellings and removed unnecessary code
0e9b9f0
Add PATCH /thoughts/:id/like endpoint
3b08714
Added POST /thoughts endpoint with authentication
0cb7f84
Added PATCH /thoughts/:id endpoint with authentication
bed460a
Added DELETE /thoughts/:id endpoint with authentication and authoriza…
c9330ed
Set up Express router
4ca7048
Added POST /register endpoint with password hashing
9b7f6bb
Added POST /login endpoint with password verification
0fdcdd5
Added authentication middleware to verify accessToken
6ae4f52
Imported thought model
c9ae7d7
Added .js to imports
d7b7425
Fixed import path for thought model
b6ce6ab
Changed thought and user imports
75ccb2d
Changed imported routes
b86b25b
Moved devDependencies to dependencies
f26ade7
test relative path
37e6bbb
Changed start path
64ac6f0
Changed path
24dc313
Changed path
b2a2c4c
Changed thought import
8f48432
Use single quotations
d140a02
Test run
904f898
Rename Thought.js to thought.js
T-Thiry a56f463
Rename User.js to user.js
T-Thiry b8a4939
changed backend routes
34ed377
Changed more routes
27ad894
Changed URL
b4d4743
Replaced catch block
e381298
Added name to post
a2c4766
Removed space
cded4db
Removed req.user and moved user to req.body
f2296d5
Added brackets
5518431
removed user and extracted and extracted user from middleware
0049405
Added user to req.body and changed user.Id to user._id
ac4008c
Added user to req.body and changed user.Id to user._
4708a08
Added id to userschema
2d66c8c
Added id to thoughtschema
01aee36
Updated id definition
fff34ac
Added return on response
fc2d780
Pull user from req instead of req.body
9fb887e
Removed id from thougt schema and user schema
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 was deleted.
Oops, something went wrong.
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,26 @@ | ||
import { User } from '../models/user.js' | ||
|
||
|
||
export const authenticateUser = async (req, res, next) => { | ||
try { | ||
const user = await User.findOne({ accessToken: req.header("Authorization") }) | ||
|
||
if (user) { | ||
req.user = user | ||
next() | ||
|
||
} else { | ||
res.status(401).json({ | ||
success: false, | ||
message: "Authorization missing or invalid", | ||
loggedOut: true | ||
}) | ||
} | ||
|
||
} catch (error) { | ||
res.status(500).json({ | ||
message: "Internal server error", | ||
error: error.message | ||
}) | ||
} | ||
} |
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,26 @@ | ||
import mongoose from 'mongoose' | ||
|
||
const thoughtSchema = new mongoose.Schema({ | ||
message: { | ||
type: String, | ||
required: true, | ||
minlength: 5, | ||
maxlength: 140 | ||
}, | ||
hearts: { | ||
type: Number, | ||
default: 0 | ||
}, | ||
user: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "User", | ||
required: true | ||
}, | ||
createdAt: { | ||
type: Date, | ||
default: Date.now | ||
}, | ||
}); | ||
|
||
|
||
export const Thought = mongoose.model("Thought", thoughtSchema) |
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,26 @@ | ||
import mongoose from 'mongoose' | ||
import crypto from 'crypto' | ||
|
||
|
||
const userSchema = new mongoose.Schema({ | ||
name: { | ||
type: String, | ||
required: true, | ||
}, | ||
email: { | ||
type: String, | ||
required: true, | ||
unique: true | ||
}, | ||
password: { | ||
type: String, | ||
required: true, | ||
}, | ||
accessToken: { | ||
type: String, | ||
default: () => crypto.randomBytes(128).toString('hex') | ||
} | ||
}); | ||
|
||
|
||
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.
great connection to the user collection here