Skip to content

Mongoose fixes per version and Added functionalities(priority tag with sorting and checkbox) and styling #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
45 changes: 26 additions & 19 deletions config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,38 @@ 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) }
if (isMatch) {
return done(null, user)
passport.use(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.` })
}
return done(null, false, { msg: 'Invalid email or password.' })
})
})
}))
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) }
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(error);
}
})
}
26 changes: 14 additions & 12 deletions controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const User = require('../models/User')
})
}

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.' })
if (!validator.isLength(req.body.password, { min: 8 })) validationErrors.push({ msg: 'Password must be at least 8 characters long' })
Expand All @@ -74,23 +74,25 @@ const User = require('../models/User')
password: req.body.password
})

User.findOne({$or: [
{email: req.body.email},
{userName: req.body.userName}
]}, (err, existingUser) => {
if (err) { return next(err) }
try{
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.' })
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) => {
await user.save();
req.logIn(user, (err) => {
if (err) {
return next(err)
}
res.redirect('/todos')
})
})
})
} catch (err) {
return next(err);
}
}
40 changes: 24 additions & 16 deletions controllers/todos.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ module.exports = {
getTodos: async (req,res)=>{
console.log(req.user)
try{
const todoItems = await Todo.find({userId:req.user.id})
const todoItems = await Todo.find({userId:req.user.id}).lean() // find todo item associated with userId
const itemsLeft = await Todo.countDocuments({userId:req.user.id,completed: false})
res.render('todos.ejs', {todos: todoItems, left: itemsLeft, user: req.user})
const todosPriority = todoItems.filter(todo => todo.priority)
const todos = todoItems.filter(todo => !todo.priority)
res.render('todos.ejs', {
todosPriority: todosPriority, todos: todos, left: itemsLeft, user: req.user
})
}catch(err){
console.log(err)
}
},
createTodo: async (req, res)=>{
try{
await Todo.create({todo: req.body.todoItem, completed: false, userId: req.user.id})
await Todo.create({todo: req.body.todoItem, completed: false, userId: req.user.id, priority: false}) // userId added to created todo item
console.log('Todo has been added!')
res.redirect('/todos')
}catch(err){
Expand All @@ -23,25 +27,15 @@ module.exports = {
markComplete: async (req, res)=>{
try{
await Todo.findOneAndUpdate({_id:req.body.todoIdFromJSFile},{
completed: true
completed: req.body.completed
})
console.log('Marked Complete')
res.json('Marked Complete')
}catch(err){
console.log(err)
}
},
markIncomplete: async (req, res)=>{
try{
await Todo.findOneAndUpdate({_id:req.body.todoIdFromJSFile},{
completed: false
})
console.log('Marked Incomplete')
res.json('Marked Incomplete')
}catch(err){
console.log(err)
}
},

deleteTodo: async (req, res)=>{
console.log(req.body.todoIdFromJSFile)
try{
Expand All @@ -51,5 +45,19 @@ module.exports = {
}catch(err){
console.log(err)
}
}
},
markPriority: async (req, res)=>{
try{
const todo = await Todo.findById(req.body.todoIdFromJSFile)
await Todo.findOneAndUpdate(
{ _id: req.body.todoIdFromJSFile },
{ priority: !todo.priority }
)
console.log('Toggled Priority')
res.json({ status: 'success', priority: !todo.priority })
}catch(err){
console.log(err)
}
},

}
6 changes: 5 additions & 1 deletion models/Todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ const TodoSchema = new mongoose.Schema({
userId: {
type: String,
required: true
}
},
priority: {
type: Boolean,
required: true,
},
})

module.exports = mongoose.model('Todo', TodoSchema)
Loading