Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,21 @@
# Run

`npm start`

# Updates

- Updated code to work with newest version of mongoose/node.js
- Added the ability for users to make comments that record the comment, the date the comment was made, and the user who made the comment
- Added a delete button for comments that will only show up for logged-in users on comments that they wrote
- Added title and username to images in the feed
- Added username to posts
- Changed the likes button so that you can only like each post once. It's also a toggle so if you click it again, you unlike the post.
- Made the dates look a little nicer
- Cleaned up commented-out text

# To add in the future

- edit button for comments
- edit button for caption, post title?
- add a like button to the feed page?
- cloudinary & passport error messages can be seen by the user
4 changes: 0 additions & 4 deletions config/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ const mongoose = require("mongoose");
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.DB_STRING, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
});

console.log(`MongoDB Connected: ${conn.connection.host}`);
Expand Down
68 changes: 40 additions & 28 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,53 @@
// const passport = require("passport"); //new
const passport = require("passport")
const LocalStrategy = require("passport-local").Strategy;
const mongoose = require("mongoose");
const User = require("../models/User");

module.exports = function (passport) {
passport.use(
new LocalStrategy({ usernameField: "email" }, (email, password, done) => {
User.findOne({ email: email.toLowerCase() }, (err, user) => {
if (err) {
return done(err);
}
if (!user) {
return done(null, false, { msg: `Email ${email} not found.` });
}
if (!user.password) {
return done(null, false, {
msg:
"Your account was registered using a sign-in provider. To enable password login, sign in using a provider, and then set a password under your user profile.",
});
}
user.comparePassword(password, (err, isMatch) => {
if (err) {
return done(err);
new LocalStrategy(
{ usernameField: "email" },
async (email, password, done) => {
try {
// Find the user by email
const user = await User.findOne({ email: email.toLowerCase() });
if (!user) {
return done(null, false, { msg: `Email ${email} not found.` });
}
if (isMatch) {
return done(null, user);

if (!user.password) {
return done(null, false, {
msg:
"Your account was registered using a sign-in provider. To enable password login, sign in using a provider, and then set a password under your user profile.",
});
}
return done(null, false, { msg: "Invalid email or password." });
});
});
})
user.comparePassword(password, (err, isMatch) => {
if (err) {
return done(err);
}
if (isMatch) {
return done(null, user);
}
return done(null, false, { msg: "Invalid email or password." });
});
// return done(null, user); // Successful authentication
} catch (err) {
return done(err); // Handle errors
}
}
)
);

passport.serializeUser((user, done) => {
done(null, user.id);
});

passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => done(err, user));

passport.deserializeUser(async (id, done) => {
try {
const user = await User.findById(id);
done(null, user);
} catch (err) {
done(err);
}
});
};
}
111 changes: 74 additions & 37 deletions controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const passport = require("passport");
const validator = require("validator");
const User = require("../models/User");

exports.getLogin = (req, res) => {

exports.getLogin = (req, res, next) => {
if (req.user) {
return res.redirect("/profile");
}
Expand All @@ -25,15 +26,16 @@ exports.postLogin = (req, res, next) => {
req.body.email = validator.normalizeEmail(req.body.email, {
gmail_remove_dots: false,
});

passport.authenticate("local", (err, user, info) => {
if (err) {
return next(err);
}
if (!user) {
req.flash("errors", info);
return res.redirect("/login");
}
};

req.logIn(user, (err) => {
if (err) {
return next(err);
Expand All @@ -42,8 +44,11 @@ exports.postLogin = (req, res, next) => {
res.redirect(req.session.returnTo || "/profile");
});
})(req, res, next);

};



exports.logout = (req, res) => {
req.logout(() => {
console.log('User has logged out.')
Expand All @@ -56,7 +61,7 @@ exports.logout = (req, res) => {
});
};

exports.getSignup = (req, res) => {
exports.getSignup = async (req, res) => {
if (req.user) {
return res.redirect("/profile");
}
Expand All @@ -65,54 +70,86 @@ exports.getSignup = (req, res) => {
});
};

exports.postSignup = (req, res, next) => {
const validationErrors = [];
if (!validator.isEmail(req.body.email))
validationErrors.push({ msg: "Please enter a valid email address." });
if (!validator.isLength(req.body.password, { min: 8 }))
validationErrors.push({
msg: "Password must be at least 8 characters long",
exports.postSignup = async (req, res, next) => {
try {
const validationErrors = [];
if (!validator.isEmail(req.body.email))
validationErrors.push({ msg: "Please enter a valid email address." });
if (!validator.isLength(req.body.password, { min: 8 }))
validationErrors.push({
msg: "Password must be at least 8 characters long",
});
if (req.body.password !== req.body.confirmPassword)
validationErrors.push({ msg: "Passwords do not match" });

if (validationErrors.length) {
req.flash("errors", validationErrors);
return res.redirect("../signup");
}

req.body.email = validator.normalizeEmail(req.body.email, {
gmail_remove_dots: false,
});
if (req.body.password !== req.body.confirmPassword)
validationErrors.push({ msg: "Passwords do not match" });

if (validationErrors.length) {
req.flash("errors", validationErrors);
return res.redirect("../signup");
}
req.body.email = validator.normalizeEmail(req.body.email, {
gmail_remove_dots: false,
});
// Check if the user already exists
const existingUser = await User.findOne({
$or: [{ email: req.body.email }, { userName: req.body.userName }],
});

const user = new User({
userName: req.body.userName,
email: req.body.email,
password: req.body.password,
});
if (existingUser) {
req.flash("errors", {
msg: "Account with that email address or username already exists.",
});
return res.redirect("../signup");
}

User.findOne(
{ $or: [{ email: req.body.email }, { userName: req.body.userName }] },
(err, existingUser) => {
// Create and save the new user
const user = new User({
userName: req.body.userName,
email: req.body.email,
password: req.body.password,
});

await user.save();

// Log in the new user
req.logIn(user, (err) => {
if (err) {
return next(err);
}
res.redirect("/profile");
});
} catch (err) {
next(err); // Pass errors to the error handler
}
};


const handleSignup = async (req, res, next) => {
try {
// Check if a user with the same email or username already exists
const existingUser = await User.findOne({
$or: [{ email: req.body.email }, { userName: req.body.userName }],
});

if (existingUser) {
req.flash("errors", {
msg: "Account with that email address or username already exists.",
});
return res.redirect("../signup");
}
user.save((err) => {

// Save the new user
await user.save();

// Log in the new user
req.logIn(user, (err) => {
if (err) {
return next(err);
}
req.logIn(user, (err) => {
if (err) {
return next(err);
}
res.redirect("/profile");
});
res.redirect("/profile");
});
} catch (err) {
next(err); // Pass any errors to the next middleware
}
);
};
};
39 changes: 39 additions & 0 deletions controllers/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

const Comment = require("../models/Comment");
const User = require('../models/User');

module.exports = {
createComment: async (req, res) => {
try {
const user = req.user; // The logged-in user object
const username = user.userName; // Get the username from the logged-in user

await Comment.create({
comment: req.body.comment,
likes: 0,
post: req.params.id,
user: user._id,
username: username,
});
console.log("Comment has been added!");
console.log(`${user._id}`)
res.redirect("/post/"+req.params.id);
} catch (err) {
console.log(err);
}
},

deleteComment: async (req,res) => {
const commentId = req.params.id;
const postId= req.body.postId;
console.log(`Deleting comment with ID: ${commentId} for post ID: ${postId}`);
try {
const result = await Comment.findByIdAndDelete(commentId)
console.log("Deleted comment", result)
res.redirect(`/post/${postId}`);
} catch (err) {
if (err) return res.status(500).send(err)
}

},
};
Loading