-
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.
- Loading branch information
Showing
65 changed files
with
4,141 additions
and
5 deletions.
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
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 |
---|---|---|
@@ -1,3 +1,39 @@ | ||
import Consersation from "../models/conversation.model.js"; | ||
import Message from "../models/message.model.js"; | ||
|
||
export const sendMessage = async (req, res) => { | ||
console.log("message sent", req.params.id); | ||
try { | ||
const { message } = req.body; | ||
const { id: receiverId } = req.params; | ||
const senderId = req.user._id; | ||
|
||
let conversation = await Consersation.findOne({ | ||
participants: { $all: [senderId, receiverId] }, | ||
}); | ||
|
||
if (!conversation) { | ||
conversation = await Consersation.create({ | ||
participants: [senderId, receiverId], | ||
}); | ||
} | ||
|
||
const newMessage = new Message({ | ||
senderId, | ||
receiverId, | ||
message, | ||
}); | ||
|
||
if (newMessage) { | ||
conversation.messages.push(newMessage._id); | ||
} | ||
|
||
// save conservation and new message to mongoDB | ||
await conversation.save(); | ||
await 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,35 @@ | ||
import jwt from "jsonwebtoken"; | ||
import User from "../models/user.model.js"; | ||
|
||
const protectRoute = async (req, res, next) => { | ||
try { | ||
const token = req.cookies.jwt; | ||
|
||
if (!token) { | ||
return res | ||
.status(401) | ||
.json({ error: "Unauthorized -- No Token Provided" }); | ||
} | ||
|
||
const decoded = jwt.verify(token, process.env.JWT_SECRET); | ||
|
||
if (!decoded) { | ||
return res.status(401).json({ error: "Unauthorized -- Invalid Token" }); | ||
} | ||
|
||
const user = await User.findById(decoded.userId).select("-password"); | ||
|
||
if (!user) { | ||
return res.status(404).json({ error: "User not found" }); | ||
} | ||
|
||
req.user = user; | ||
|
||
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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import express from "express"; | ||
import { sendMessage } from "../controllers/message.controller.js"; | ||
import protectRoute from "../middleware/protectRoute.js"; | ||
|
||
const router = express.Router(); | ||
|
||
router.post("/send/:id", sendMessage); | ||
router.post("/send/:id", protectRoute, sendMessage); | ||
|
||
export default router; |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.