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
1 change: 1 addition & 0 deletions config/database.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const mongoose = require("mongoose");

// connect to database
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.DB_STRING, {
Expand Down
3 changes: 3 additions & 0 deletions config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const LocalStrategy = require("passport-local").Strategy;
const mongoose = require("mongoose");
const User = require("../models/User");

// configure passport authentication
module.exports = function (passport) {
passport.use(
new LocalStrategy({ usernameField: "email" }, (email, password, done) => {
Expand Down Expand Up @@ -31,10 +32,12 @@ module.exports = function (passport) {
})
);

// serialize user id for session, essentially reduces what is stored in session memory to just the userid which can then be used to retrieve the full user data when needed for requests
passport.serializeUser((user, done) => {
done(null, user.id);
});

// retrieves the user data via the serialized userid data
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => done(err, user));
});
Expand Down
5 changes: 5 additions & 0 deletions controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const passport = require("passport");
const validator = require("validator");
const User = require("../models/User");

// render login page
exports.getLogin = (req, res) => {
if (req.user) {
return res.redirect("/profile");
Expand All @@ -11,6 +12,7 @@ exports.getLogin = (req, res) => {
});
};

// handle login form submission
exports.postLogin = (req, res, next) => {
const validationErrors = [];
if (!validator.isEmail(req.body.email))
Expand Down Expand Up @@ -44,6 +46,7 @@ exports.postLogin = (req, res, next) => {
})(req, res, next);
};

// log user out and destroy session
exports.logout = (req, res) => {
req.logout(() => {
console.log('User has logged out.')
Expand All @@ -56,6 +59,7 @@ exports.logout = (req, res) => {
});
};

// render signup page
exports.getSignup = (req, res) => {
if (req.user) {
return res.redirect("/profile");
Expand All @@ -65,6 +69,7 @@ exports.getSignup = (req, res) => {
});
};

// handle signup form submission
exports.postSignup = (req, res, next) => {
const validationErrors = [];
if (!validator.isEmail(req.body.email))
Expand Down
1 change: 1 addition & 0 deletions controllers/home.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
// renders home page
getIndex: (req, res) => {
res.render("index.ejs");
},
Expand Down
6 changes: 6 additions & 0 deletions controllers/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const cloudinary = require("../middleware/cloudinary");
const Post = require("../models/Post");

module.exports = {
// gets user profile with posts
getProfile: async (req, res) => {
try {
const posts = await Post.find({ user: req.user.id });
Expand All @@ -10,6 +11,7 @@ module.exports = {
console.log(err);
}
},
// get all posts for feed
getFeed: async (req, res) => {
try {
const posts = await Post.find().sort({ createdAt: "desc" }).lean();
Expand All @@ -18,6 +20,7 @@ module.exports = {
console.log(err);
}
},
// get single post
getPost: async (req, res) => {
try {
const post = await Post.findById(req.params.id);
Expand All @@ -26,6 +29,7 @@ module.exports = {
console.log(err);
}
},
// create new post with image upload
createPost: async (req, res) => {
try {
// Upload image to cloudinary
Expand All @@ -45,6 +49,7 @@ module.exports = {
console.log(err);
}
},
// increment post likes
likePost: async (req, res) => {
try {
await Post.findOneAndUpdate(
Expand All @@ -59,6 +64,7 @@ module.exports = {
console.log(err);
}
},
// delete post and image
deletePost: async (req, res) => {
try {
// Find post by id
Expand Down
2 changes: 2 additions & 0 deletions middleware/auth.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module.exports = {
// check if user is authenticated
ensureAuth: function (req, res, next) {
if (req.isAuthenticated()) {
return next();
} else {
res.redirect("/");
}
},
// check if user is not authenticated
ensureGuest: function (req, res, next) {
if (!req.isAuthenticated()) {
return next();
Expand Down
2 changes: 1 addition & 1 deletion middleware/multer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require("path");

module.exports = multer({
storage: multer.diskStorage({}),
fileFilter: (req, file, cb) => {
fileFilter: (req, file, cb) => { // check file type is a compatible image
let ext = path.extname(file.originalname);
if (ext !== ".jpg" && ext !== ".jpeg" && ext !== ".png") {
cb(new Error("File type is not supported"), false);
Expand Down
2 changes: 2 additions & 0 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const UserSchema = new mongoose.Schema({

// Password hash middleware.

// hash password before saving
UserSchema.pre("save", function save(next) {
const user = this;
if (!user.isModified("password")) {
Expand All @@ -30,6 +31,7 @@ UserSchema.pre("save", function save(next) {

// Helper method for validating user's password.

// compare password with hash
UserSchema.methods.comparePassword = function comparePassword(
candidatePassword,
cb
Expand Down