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
7 changes: 1 addition & 6 deletions config/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ 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,
});
const conn = await mongoose.connect(process.env.DB_STRING);

console.log(`MongoDB Connected: ${conn.connection.host}`);
} catch (err) {
Expand Down
28 changes: 17 additions & 11 deletions config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ 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);
}
new LocalStrategy({ usernameField: "email" }, async (email, password, done) => {
try {
const user = await User.findOne({ email: email.toLowerCase() })

if (!user) {
return done(null, false, { msg: `Email ${email} not found.` });
}
Expand All @@ -18,24 +17,31 @@ module.exports = function (passport) {
"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);
}
if (err) return done(err);

if (isMatch) {
return done(null, user);
}
return done(null, false, { msg: "Invalid email or password." });
});
});
} catch(err){
return done(err);
}
})
);

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)
}
});
};
54 changes: 29 additions & 25 deletions controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ exports.postLogin = (req, res, next) => {
return next(err);
}
if (!user) {
req.flash("errors", info);
req.flash("errors", [{ msg: info.msg }]);
return res.redirect("/login");
}
req.logIn(user, (err) => {
Expand Down Expand Up @@ -65,7 +65,7 @@ exports.getSignup = (req, res) => {
});
};

exports.postSignup = (req, res, next) => {
exports.postSignup = async (req, res, next) => {
const validationErrors = [];
if (!validator.isEmail(req.body.email))
validationErrors.push({ msg: "Please enter a valid email address." });
Expand All @@ -80,6 +80,7 @@ exports.postSignup = (req, res, next) => {
req.flash("errors", validationErrors);
return res.redirect("../signup");
}

req.body.email = validator.normalizeEmail(req.body.email, {
gmail_remove_dots: false,
});
Expand All @@ -90,29 +91,32 @@ exports.postSignup = (req, res, next) => {
password: req.body.password,
});

User.findOne(
{ $or: [{ email: req.body.email }, { userName: req.body.userName }] },
(err, existingUser) => {
if (err) {
return next(err);
}
if (existingUser) {
req.flash("errors", {
msg: "Account with that email address or username already exists.",
});
return res.redirect("../signup");
}
user.save((err) => {
if (err) {
return next(err);
}
req.logIn(user, (err) => {
if (err) {
return next(err);
}
res.redirect("/profile");
});
try {
// Check if user 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");
}
);

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

// Passport login still uses a callback — that's OK
req.logIn(user, (err) => {
if (err) return next(err);
return res.redirect("/profile");
});

} catch (err) {
return next(err);
}
};
42 changes: 42 additions & 0 deletions controllers/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const Comment = require("../models/Comment");

module.exports = {
createComment: async (req, res) => {
try {
await Comment.create({
comment: req.body.comment,
likes: 0,
post: req.params.id,
user: req.user.id,
});
console.log("Comment has been added!");
res.redirect(`/post/${req.params.id}`);
} catch (err) {
console.log(err);
}
},
likeComment: async (req, res) => {
try {
await Comment.findOneAndUpdate(
{ _id: req.params.id },
{
$inc: { likes: 1 },
}
);
console.log("Likes +1");
res.redirect(`/post/${req.params.id}`);
} catch (err) {
console.log(err);
}
},
deleteComment: async (req, res) => {
try {
// Delete comment from db
await Comment.remove({ _id: req.params.id });
console.log("Deleted Comment");
res.redirect("/profile");
} catch (err) {
res.redirect("/profile");
}
},
};
9 changes: 7 additions & 2 deletions controllers/posts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const cloudinary = require("../middleware/cloudinary");
const Post = require("../models/Post");
const Comment = require("../models/Comment");

module.exports = {
getProfile: async (req, res) => {
Expand All @@ -21,7 +22,8 @@ module.exports = {
getPost: async (req, res) => {
try {
const post = await Post.findById(req.params.id);
res.render("post.ejs", { post: post, user: req.user });
const comments = await Comment.find({post: req.params.id}).sort({createdAt: "desc"}).lean();
res.render("post.ejs", { post: post, user: req.user, comments: comments });
} catch (err) {
console.log(err);
}
Expand Down Expand Up @@ -66,8 +68,11 @@ module.exports = {
// Delete image from cloudinary
await cloudinary.uploader.destroy(post.cloudinaryId);
// Delete post from db
await Post.remove({ _id: req.params.id });
await Post.findByIdAndDelete(req.params.id)
// Delete comments from db
await Comment.deleteMany({ post: req.params.id });
console.log("Deleted Post");

res.redirect("/profile");
} catch (err) {
res.redirect("/profile");
Expand Down
26 changes: 26 additions & 0 deletions models/Comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const mongoose = require("mongoose");

const CommentSchema = new mongoose.Schema({
comment: {
type: String,
required: true,
},
likes: {
type: Number,
required: true,
},
post: {
type: mongoose.Schema.Types.ObjectId,
ref: "Post",
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
createdAt: {
type: Date,
default: Date.now,
},
});

module.exports = mongoose.model("Comment", CommentSchema);
43 changes: 18 additions & 25 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,29 @@ const UserSchema = new mongoose.Schema({
password: String,
});

// Password hash middleware.

UserSchema.pre("save", function save(next) {
const user = this;
if (!user.isModified("password")) {
return next();
}
bcrypt.genSalt(10, (err, salt) => {
if (err) {
return next(err);
}
bcrypt.hash(user.password, salt, (err, hash) => {
if (err) {
return next(err);
}
user.password = hash;
next();
});
});
// Password hashing middleware (async/await, no next needed)
UserSchema.pre("save", async function () {
// If password wasn't modified, skip hashing
if (!this.isModified("password")) return;

const salt = await bcrypt.genSalt(10);
this.password = await bcrypt.hash(this.password, salt);
});

// Helper method for validating user's password.
// Helper method to validate password

// UserSchema.methods.comparePassword = function (candidatePassword) {
// return bcrypt.compare(candidatePassword, this.password);
// };

UserSchema.methods.comparePassword = function comparePassword(
candidatePassword,
cb
) {

UserSchema.methods.comparePassword = function (candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
cb(err, isMatch);
if (err) return cb(err);
cb(null, isMatch);
});
};



module.exports = mongoose.model("User", UserSchema);
Loading