Commit 0d91583 1 parent d080468 commit 0d91583 Copy full SHA for 0d91583
File tree 3 files changed +24
-4
lines changed
3 files changed +24
-4
lines changed Original file line number Diff line number Diff line change @@ -12,8 +12,8 @@ app.use(express.json({ limit: "200mb" }));
12
12
app . use ( express . urlencoded ( { extended : true , limit : "200mb" } ) ) ;
13
13
app . use ( cors ( ) ) ;
14
14
app . use ( express . urlencoded ( { extended : false } ) ) ;
15
- app . use ( errorHandler ) ;
16
15
app . use ( "/api/user" , require ( "./routes/userRoutes" ) ) ;
17
16
app . use ( "/api/bookmark" , require ( "./routes/bookmarkRoutes" ) ) ;
17
+ app . use ( errorHandler ) ;
18
18
19
19
app . listen ( 5000 , ( ) => console . log ( "Server is running on PORT 5000" ) ) ;
Original file line number Diff line number Diff line change
1
+ import { Request , Response , NextFunction } from "express" ;
2
+ import Bookmark from "../models/bookmarkModel" ;
3
+ import { IBookmark } from "../interfaces/BookmarkInterface" ;
4
+ import { ObjectId } from "mongodb" ;
5
+
6
+ export const checkBookmark = async (
7
+ req : Request ,
8
+ res : Response ,
9
+ next : NextFunction
10
+ ) => {
11
+ try {
12
+ const bookmark : IBookmark = await Bookmark . findById ( { _id : req . params . id } ) ;
13
+ if ( bookmark . user . toString ( ) !== res . locals . user . _id )
14
+ return res . status ( 404 ) . send ( "Bookmark not found!" ) ;
15
+ next ( ) ;
16
+ } catch ( error ) {
17
+ next ( error ) ;
18
+ }
19
+ } ;
Original file line number Diff line number Diff line change @@ -8,13 +8,14 @@ import {
8
8
singleBookmark ,
9
9
} from "../controllers/bookmarkController" ;
10
10
import { Protected } from "../middleware/authMiddleware" ;
11
+ import { checkBookmark } from "../middleware/bookmarkMiddleware" ;
11
12
12
13
router . get ( "/" , Protected , getBookmark ) ;
13
14
router . post ( "/add" , Protected , addBookmark ) ;
14
15
router
15
16
. route ( "/:id" )
16
- . get ( singleBookmark )
17
- . put ( Protected , updateBookmark )
18
- . delete ( Protected , deleteBookmark ) ;
17
+ . get ( Protected , checkBookmark , singleBookmark )
18
+ . put ( Protected , checkBookmark , updateBookmark )
19
+ . delete ( Protected , checkBookmark , deleteBookmark ) ;
19
20
20
21
module . exports = router ;
You can’t perform that action at this time.
0 commit comments