Skip to content

Commit a406a3f

Browse files
committed
Ready for Deployment
0 parents  commit a406a3f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+16460
-0
lines changed

api/auth.js

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const express = require("express");
2+
const router = express.Router();
3+
const UserModel = require("../models/UserModel");
4+
const FollowerModel = require("../models/FollowerModel");
5+
const NotificationModel = require("../models/NotificationModel");
6+
const jwt = require("jsonwebtoken");
7+
const bcrypt = require("bcryptjs");
8+
const isEmail = require("validator/lib/isEmail");
9+
const authMiddleware = require("../middleware/authMiddleware");
10+
const ChatModel = require("../models/ChatModel");
11+
12+
router.get("/", authMiddleware, async (req, res) => {
13+
const { userId } = req;
14+
15+
try {
16+
const user = await UserModel.findById(userId);
17+
const userFollowStats = await FollowerModel.findOne({ user: userId });
18+
19+
return res.status(200).json({ user, userFollowStats });
20+
} catch (error) {
21+
console.log(error);
22+
return res.status(401).send("Unauthorized");
23+
}
24+
});
25+
26+
router.post("/", async (req, res) => {
27+
const { email, password } = req.body.user;
28+
29+
if (!isEmail(email)) {
30+
return res.status(401).send("Invalid Email");
31+
}
32+
33+
if (password.length < 6) {
34+
return res
35+
.status(401)
36+
.send("Password must be more than 6 character in length");
37+
}
38+
39+
try {
40+
const user = await UserModel.findOne({ email: email.toLowerCase() }).select(
41+
"+password"
42+
);
43+
if (!user) {
44+
return res.status(401).send("User not found !! Check credentials");
45+
}
46+
47+
const isPassword = await bcrypt.compare(password, user.password);
48+
49+
if (!isPassword) {
50+
return res.status(401).send("Invalid Credentials");
51+
}
52+
53+
const chatModel = await ChatModel.findOne({
54+
user: user._id,
55+
});
56+
57+
if (!chatModel) {
58+
await new ChatModel({ user: user._id, chats: [] }).save();
59+
}
60+
61+
const payload = { userId: user._id };
62+
jwt.sign(
63+
payload,
64+
process.env.jwtSecret,
65+
{ expiresIn: "2d" },
66+
(err, token) => {
67+
if (err) throw err;
68+
res.status(200).json(token);
69+
}
70+
);
71+
} catch (error) {
72+
console.error(error);
73+
return res.status(500).send("Server error");
74+
}
75+
});
76+
77+
module.exports = router;

api/chats.js

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const express = require("express");
2+
const router = express.Router();
3+
const ChatModel = require("../models/ChatModel");
4+
const UserModel = require("../models/UserModel");
5+
const authMiddleware = require("../middleware/authMiddleware");
6+
7+
// GET ALL CHATS
8+
9+
router.get("/", authMiddleware, async (req, res) => {
10+
try {
11+
const { userId } = req;
12+
13+
const user = await ChatModel.findOne({ user: userId }).populate(
14+
"chats.messagesWith"
15+
);
16+
17+
let chatsToBeSent = [];
18+
19+
if (user.chats.length > 0) {
20+
chatsToBeSent = await user.chats.map((chat) => ({
21+
messagesWith: chat.messagesWith._id,
22+
name: chat.messagesWith.name,
23+
profilePicUrl: chat.messagesWith.profilePicUrl,
24+
lastMessage: chat.messages[chat.messages.length - 1].msg,
25+
date: chat.messages[chat.messages.length - 1].date,
26+
}));
27+
}
28+
29+
return res.json(chatsToBeSent);
30+
} catch (error) {
31+
console.error(error);
32+
return res.status(500).send("Server Error");
33+
}
34+
});
35+
36+
// GET USER INFO
37+
38+
router.get("/user/:userToFindId", authMiddleware, async (req, res) => {
39+
try {
40+
const user = await UserModel.findById(req.params.userToFindId);
41+
42+
if (!user) {
43+
return res.status(404).send("No User found");
44+
}
45+
46+
return res.json({ name: user.name, profilePicUrl: user.profilePicUrl });
47+
} catch (error) {
48+
console.error(error);
49+
return res.status(500).send("Server Error");
50+
}
51+
});
52+
53+
// Delete a chat
54+
55+
router.delete(`/:messagesWith`, authMiddleware, async (req, res) => {
56+
try {
57+
const { userId } = req;
58+
const { messagesWith } = req.params;
59+
60+
const user = await ChatModel.findOne({ user: userId });
61+
62+
const chatToDelete = user.chats.find(
63+
(chat) => chat.messagesWith.toString() === messagesWith
64+
);
65+
66+
if (!chatToDelete) {
67+
return res.status(404).send("Chat not found");
68+
}
69+
70+
const indexOf = user.chats
71+
.map((chat) => chat.messagesWith.toString())
72+
.indexOf(messagesWith);
73+
74+
user.chats.splice(indexOf, 1);
75+
76+
await user.save();
77+
78+
return res.status(200).send("Chat deleted");
79+
} catch (error) {
80+
console.error(error);
81+
return res.status(500).send("Server Error");
82+
}
83+
});
84+
85+
module.exports = router;

api/notifications.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const express = require("express");
2+
const router = express.Router();
3+
const authMiddleware = require("../middleware/authMiddleware");
4+
const NotificationModel = require("../models/NotificationModel");
5+
const UserModel = require("../models/UserModel");
6+
7+
router.get("/", authMiddleware, async (req, res) => {
8+
try {
9+
const { userId } = req;
10+
11+
const user = await NotificationModel.findOne({ user: userId })
12+
.populate("notifications.user")
13+
.populate("notifications.post");
14+
15+
return res.json(user.notifications);
16+
} catch (error) {
17+
console.error(error);
18+
return res.status(500).send("Server Error");
19+
}
20+
});
21+
22+
router.post("/", authMiddleware, async (req, res) => {
23+
try {
24+
const { userId } = req;
25+
26+
const user = await UserModel.findById(userId);
27+
28+
if (user.unreadNotification) {
29+
user.unreadNotification = false;
30+
await user.save();
31+
}
32+
return res.status(200).send("Updated");
33+
} catch (error) {
34+
console.error(error);
35+
return res.status(500).send("Server Error");
36+
}
37+
});
38+
39+
module.exports = router;

0 commit comments

Comments
 (0)