forked from OurProjectsCombined/chat-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send message endpoint (message.routes.js AND message.controller.js) A…
…ND protectRoutes.js middleware
- Loading branch information
Keagan Aromin
authored and
Keagan Aromin
committed
Jan 10, 2025
1 parent
0cb6ced
commit 64de860
Showing
5 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,44 @@ | ||
import Conversation from "../models/conversation.model.js"; | ||
import Message from "../models/message.model.js"; | ||
import { getReceiverSocketId, io } from "../socket/socket.js"; | ||
|
||
export const sendMessage = async (req, res) => { | ||
try { | ||
const { message } = req.body; | ||
const { id: receiverId } = req.params; | ||
const senderId = req.user._id; | ||
|
||
let conversation = await Conversation.findOne({ | ||
participants: { $all: [senderId, receiverId] }, | ||
}); | ||
|
||
if (!conversation) { | ||
conversation = await Conversation.create({ | ||
participants: [senderId, receiverId], | ||
}); | ||
} | ||
|
||
const newMessage = new Message({ | ||
senderId, | ||
receiverId, | ||
message, | ||
}); | ||
|
||
if (newMessage) { | ||
conversation.messages.push(newMessage._id); | ||
} | ||
|
||
// SOCKET IO FUNCTIONALITY WILL GO HERE | ||
|
||
// await conversation.save(); | ||
// await newMessage.save(); | ||
|
||
// this will run in parallel | ||
await Promise.all([conversation.save(), newMessage.save()]); | ||
|
||
res.status(201).json(newMessage); | ||
} catch (error) { | ||
console.log("Error in sendMessage controller: ", error.message); | ||
res.status(500).json({ error: "Internal server error" }); | ||
} | ||
}; |
This file contains 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,39 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import User from "../models/user.model.js"; | ||
|
||
const protectRoute = async (req, res, next) => { | ||
try { | ||
const token = req.cookies.jwt; | ||
//case where there is no token in the request | ||
if (!token) { | ||
return res.status(401).json({error: "Unauthorized - No Token Provided"}) //return http 401 error (unauthorized) | ||
} | ||
|
||
//remember, when we generate tokens in utils/generateToken.js, we sign using JWT_SECRET - the .env file will be in the server/backend upon deployment | ||
//so the server can use the secret, which is in the .env file, to verify/decode the token from the client | ||
const decoded = jwt.verify(token, process.env.JWT_SECRET); | ||
|
||
//case where the verification fails | ||
if (!decoded) { | ||
return res.status(401).json({error: "Unauthorized - Invalid Token"}) //return http 401 error (unauthorized) | ||
} | ||
|
||
//assign the .userId attribute of the decoded message to user | ||
const user = await User.findById(decoded.userId); | ||
|
||
if (!user) { | ||
return res.status(404).json({ error: "User not found" }); | ||
} | ||
|
||
//assign request user attribute to the user we decoded using mongoose model + jwt | ||
req.user = user; | ||
|
||
//this says that once everything has run, exit this function and call the next one | ||
next(); | ||
} catch (error) { | ||
console.log("Error in protectRoute middleware: ", error.message); | ||
res.status(500).json({error: "Internal server error"}); | ||
} | ||
} | ||
|
||
export default protectRoute; |
This file contains 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,10 @@ | ||
import express, { Router } from "express"; | ||
import { sendMessage } from "../controllers/message.controller"; | ||
import protectRoute from "../middleware/protectRoute"; | ||
|
||
const router = express.Router(); | ||
|
||
//when we have a post request, we first run protectRoute until the next() call is reached, then we run sendMessage | ||
router.post("/send/:id", protectRoute, sendMessage) | ||
|
||
export default router; |
Empty file.
This file contains 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