-
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
1 parent
4c262d5
commit 77201c5
Showing
12 changed files
with
231 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import mongoose from "mongoose"; | ||
import { Message } from "../models/message.model.js"; | ||
import { Conversation } from "../models/communication.model.js"; | ||
|
||
export const sendMsg = async (req, res) => { | ||
try { | ||
const { message } = req.body; | ||
const senderId = req.params.id; | ||
const recieverId = req.user._id; | ||
const msg = new Message({ | ||
senderId, | ||
recieverId, | ||
message, | ||
}); | ||
msg.save().then((res) => { | ||
const conv = Conversation.findOne({ | ||
participants: { $all: [senderId, recieverId] }, | ||
}); | ||
if (!conv) { | ||
const con = new Conversation({ | ||
participants: [senderId, recieverId], | ||
message: [msg._id], | ||
}); | ||
con | ||
.save() | ||
.then(() => { | ||
console.log("Conversation created"); | ||
}) | ||
.catch((err) => { | ||
console.log("problem while creating conversation"); | ||
}); | ||
} else { | ||
conv.message.push(msg._id); | ||
} | ||
}); | ||
} catch (err) { | ||
console.log("error in controller of sendMsg", err.message); | ||
res.status(500).json({ error: "error while sending message" }); | ||
} | ||
}; |
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,19 +1,23 @@ | ||
import dotenv from "dotenv"; | ||
dotenv.config(); | ||
import express from "express"; | ||
import cookieParser from "cookie-parser"; | ||
|
||
import { router } from "./routes/user.auth.js"; | ||
import authrouter from "./routes/user.auth.js"; | ||
import msgRouter from "./routes/message.route.js"; | ||
import { authConn } from "./db/user.auth.js"; | ||
|
||
const app = express(); | ||
const PORT = process.env.PORT || 5000; | ||
|
||
app.use(express.json()); | ||
app.use(cookieParser()); | ||
|
||
app.use("/auth", router); | ||
app.use("/api/auth", authrouter); | ||
app.use("/api/message", msgRouter); | ||
|
||
app.listen(PORT, () => { | ||
authConn(); | ||
console.log(`Listening to ${PORT}`); | ||
app.use("/auth", router); | ||
app.use("/api/auth", authrouter); | ||
}); |
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,26 @@ | ||
import jwt from "jsonwebtoken"; | ||
import { User } from "../models/user.auth.js"; | ||
|
||
const protectRoute = async (req, res, next) => { | ||
try { | ||
const token = req.cookies.jwt; | ||
if (!token) { | ||
res.status(401).json({ error: "Unauthorized -> No token found" }); | ||
} | ||
const decoded = jwt.verify(token, process.env.JWT_SECRET); | ||
if (!decoded) { | ||
res.status(401).json({ error: "Unauthorized->Invalid token" }); | ||
} | ||
const user = await User.findById(decoded.userId).select("-password"); | ||
if (!user) { | ||
res.status(404).json({ error: "user not found in login check" }); | ||
} | ||
req.user = user; | ||
next(); | ||
} catch (error) { | ||
console.log("Problem in protectRoute", 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,23 @@ | ||
import mongoose from "mongoose"; | ||
|
||
const Schema = new mongoose.Schema( | ||
{ | ||
participants: [ | ||
{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "User", | ||
required: true, | ||
}, | ||
], | ||
messages: [ | ||
{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "Message", | ||
default: [], | ||
}, | ||
], | ||
}, | ||
{ timestamps: true } | ||
); | ||
|
||
export const Conversation = mongoose.model("Conversation", Schema); |
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,23 @@ | ||
import mongoose from "mongoose"; | ||
|
||
const Schema = new mongoose.Schema( | ||
{ | ||
recieverId: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "User", | ||
required: true, | ||
}, | ||
senderId: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "User", | ||
required: true, | ||
}, | ||
message: { | ||
type: String, | ||
required: true, | ||
}, | ||
}, | ||
{ timestamps: true } | ||
); | ||
|
||
export const Message = mongoose.model("Message", Schema); |
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,29 +1,32 @@ | ||
import mongoose from "mongoose"; | ||
|
||
const authSchema = mongoose.Schema({ | ||
fullName: { | ||
type: String, | ||
required: true, | ||
const authSchema = mongoose.Schema( | ||
{ | ||
fullName: { | ||
type: String, | ||
required: true, | ||
}, | ||
userName: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
minLength: 3, | ||
}, | ||
password: { | ||
type: String, | ||
required: true, | ||
minLength: 4, | ||
}, | ||
gender: { | ||
type: String, | ||
enum: ["male", "female"], | ||
}, | ||
profilePic: { | ||
type: "String", | ||
default: "", | ||
}, | ||
}, | ||
userName: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
minLength: 3, | ||
}, | ||
password: { | ||
type: String, | ||
required: true, | ||
minLength: 4, | ||
}, | ||
gender: { | ||
type: String, | ||
enum: ["male", "female"], | ||
}, | ||
profilePic: { | ||
type: "String", | ||
default: "", | ||
}, | ||
}); | ||
{ timestamp: true } | ||
); | ||
|
||
export const User = mongoose.model("User", authSchema); |
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,9 @@ | ||
import express from "express"; | ||
import { sendMsg } from "../controllers/message.js"; | ||
import protectRoute from "../middleware/protectRoute.js"; | ||
|
||
const router = express.Router(); | ||
|
||
router.post("/send/:id", protectRoute, sendMsg); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import express from "express"; | ||
import { Signup, Login } from "../controllers/user.auth.js"; | ||
import { Signup, Login, Logout } from "../controllers/user.auth.js"; | ||
|
||
export const router = express.Router(); | ||
const router = express.Router(); | ||
|
||
router.post("/signup", Signup); | ||
|
||
router.post("/login", Login); | ||
|
||
router.post("/logout", Logout); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import jwt from "jsonwebtoken"; | ||
|
||
export const generateTokensAndSetCookies = (userId, res) => { | ||
const token = jwt.sign({ userId }, process.env.JWT_SECRET, { | ||
expiresIn: "15d", | ||
}); | ||
|
||
res.cookie("jwt", token, { | ||
maxAge: 15 * 24 * 60 * 60 * 1000, | ||
sameSite: "strict", | ||
httpOnly: true, | ||
strict: process.env.NODE_ENV !== "development", | ||
}); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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