Skip to content

Commit

Permalink
Merge pull request #300 from yashpandav/follow-backend
Browse files Browse the repository at this point in the history
Follow user backend
  • Loading branch information
OkenHaha authored Feb 10, 2025
2 parents bf9fdb2 + b0d7f3a commit 25877f5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
53 changes: 52 additions & 1 deletion back-end/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,57 @@ const getOtherUser = async (req, res) => {
}
};

const followUser = async (req, res) => {
try {
const { userToFollowId } = req.params;

// Get current user from token
const authHeader = req.headers.authorization;
if (!authHeader) {
return res.status(401).json({ error: "No token provided." });
}
const token = authHeader.split(' ')[1];
const decoded = jwt.verify(token, secretKey);
const currentUserId = decoded.userId;

// Check if trying to follow self
if (currentUserId === userToFollowId) {
return res.status(400).json({ error: "You cannot follow yourself" });
}

// Find both users
const userToFollow = await User.findById(userToFollowId);
const currentUser = await User.findById(currentUserId);

console.log("userToFollow",userToFollow)
console.log("currentUser",currentUser)

if (!userToFollow || !currentUser) {
return res.status(404).json({ error: "User not found" });
}

// Check if already following
if (currentUser.following.includes(userToFollowId)) {
return res.status(400).json({ error: "You are already following this user" });
}

// Add to following and followers lists
currentUser.following.push(userToFollowId);
userToFollow.followers.push(currentUserId);

await currentUser.save();
await userToFollow.save();

res.status(200).json({
message: "Successfully followed user",
following: currentUser.following,
followers: userToFollow.followers
});

} catch (error) {
console.error("Error in followUser:", error);
res.status(500).json({ error: "An error occurred while following user" });
}
};

export { getProfile, registerUser, loginUser, editProfile, deleteUserAccount, resetPassword, getOtherUser };
export { getProfile, registerUser, loginUser, editProfile, deleteUserAccount, resetPassword, getOtherUser, followUser };
8 changes: 8 additions & 0 deletions back-end/models/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ const userSchema = new Schema(
achievements:[{
type:mongoose.Schema.Types.ObjectId,
ref:"Achievement"
}],
followers: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}],
following: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}]
},{timestamps: true}
);
Expand Down
5 changes: 4 additions & 1 deletion back-end/routes/user.routes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express';
import { loginUser, registerUser, getProfile, editProfile, deleteUserAccount, resetPassword, getOtherUser } from "../controllers/user.controller.js";
import { loginUser, registerUser, getProfile, editProfile, deleteUserAccount, resetPassword, getOtherUser, followUser } from "../controllers/user.controller.js";
import { genrateOtp, generateOTPForDelete, generateOTPForPassword } from '../Utils/otpgenerate.js';
import multer from 'multer'

Expand Down Expand Up @@ -33,4 +33,7 @@ userRouter.post('/reset-password', resetPassword);
// Get other user's profile
userRouter.get('/user/:userId', getOtherUser);

// Follow/Unfollow routes
userRouter.post('/follow/:userToFollowId', followUser);

export { userRouter };

0 comments on commit 25877f5

Please sign in to comment.