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
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

npm-debug.log
node_modules


.DS_Store
config/database.js
.env
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"terminal.integrated.fontSize": 16
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 RC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node server.js
44 changes: 44 additions & 0 deletions app/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// load the things we need
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');

// define the schema for our user model
var userSchema = mongoose.Schema({

local : {
email : String,
password : String
},
facebook : {
id : String,
token : String,
name : String,
email : String
},
twitter : {
id : String,
token : String,
displayName : String,
username : String
},
google : {
id : String,
token : String,
email : String,
name : String
}

});

// generating a hash
userSchema.methods.generateHash = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};

// checking if password is valid
userSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.local.password);
};

// create the model for users and expose it to our app
module.exports = mongoose.model('User', userSchema);
90 changes: 90 additions & 0 deletions app/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const { ObjectId } = require('mongodb');

module.exports = function(app, passport, db) {

// Normal Routes

// show login page (home page)
app.get('/', function(req, res) {
res.render('login.ejs', { message: req.flash('loginMessage') });
});

// display signup
app.get('/signup', function(req, res) {
res.render('signup.ejs', { message: req.flash('signupMessage') });
});

// show mood on Dashboard (Main App Page)
app.get('/index', isLoggedIn, function(req, res) {
db.collection('moods').find({ userId: req.user._id }).toArray((err, result) => {
if (err) return console.log(err);
res.render('index.ejs', {
user: req.user,
moods: result
});
});
});

// Logout
app.get('/logout', function(req, res) {
req.logout(function(err) {
if (err) {
return next(err);
}
res.redirect('/');
});
});

// Mood actions

// adding a mood
app.post('/addMood', isLoggedIn, (req, res) => {
db.collection('moods').insertOne({
mood: req.body.mood,
color: req.body.color,
userId: req.user._id,
createdAt: new Date()
}, (err, result) => {
if (err) return console.log(err);
console.log('Saved new mood to database');
res.redirect('/index');
});
});

// Delete a mood
app.post('/deleteMood', isLoggedIn, (req, res) => {
const id = req.body.id;
db.collection('moods').deleteOne(
{ _id: new ObjectId(id), userId: req.user._id },
(err, result) => {
if (err) return res.status(500).send(err);
res.redirect('/index');
}
);
});

// auth routes

// Login form processing
app.post('/login', passport.authenticate('local-login', {
successRedirect: '/index',
failureRedirect: '/',
failureFlash: true
}));

// signup form processing
app.post('/signup', passport.authenticate('local-signup', {
successRedirect: '/index',
failureRedirect: '/signup',
failureFlash: true
}));

};

// mw to check if user is logged in
function isLoggedIn(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/');
}
85 changes: 85 additions & 0 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// config/passport.js

var LocalStrategy = require('passport-local').Strategy;

// load up the user model
var User = require('../app/models/user');


module.exports = function(passport) {

// =========================================================================
// PASSPORT SESSION SETUP ==================================================
// =========================================================================

// used to serialize the user for the session
passport.serializeUser(function(user, done) {
done(null, user.id);
});

// used to deserialize the user
passport.deserializeUser(async function(id, done) {
try {
const user = await User.findById(id);
done(null, user);
} catch (err) {
done(err, null);
}
});

// =========================================================================
// LOCAL SIGNUP ============================================================
// =========================================================================

passport.use('local-signup', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
},
async function(req, email, password, done) {
try {
const existingUser = await User.findOne({ 'local.email': email });

if (existingUser) {
return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
} else {
var newUser = new User();
newUser.local.email = email;
newUser.local.password = newUser.generateHash(password);

await newUser.save();
return done(null, newUser);
}
} catch (err) {
return done(err);
}
}));

// =========================================================================
// LOCAL LOGIN =============================================================
// =========================================================================

passport.use('local-login', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
},
async function(req, email, password, done) {
try {
const user = await User.findOne({ 'local.email': email });

if (!user) {
return done(null, false, req.flash('loginMessage', 'No user found.'));
}

if (!user.validPassword(password)) {
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.'));
}

return done(null, user);
} catch (err) {
return done(err);
}
}));

};
44 changes: 44 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// load the things we need
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');

// define the schema for our user model
var userSchema = mongoose.Schema({

local : {
email : String,
password : String
},
facebook : {
id : String,
token : String,
name : String,
email : String
},
twitter : {
id : String,
token : String,
displayName : String,
username : String
},
google : {
id : String,
token : String,
email : String,
name : String
}

});

// generating a hash
userSchema.methods.generateHash = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};

// checking if password is valid
userSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.local.password);
};

// create the model for users and expose it to our app
module.exports = mongoose.model('User', userSchema);
26 changes: 26 additions & 0 deletions mood.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"mood": "Happy",
"color": "#FFD700",
"userId": { "$oid": "68114174ff3abc3145e0ba5f" },
"createdAt": { "$date": "2025-04-29T15:00:00.000Z" }
},
{
"mood": "Calm",
"color": "#A3D5D3",
"userId": { "$oid": "68114174ff3abc3145e0ba5f" },
"createdAt": { "$date": "2025-04-29T15:10:00.000Z" }
},
{
"mood": "Sad",
"color": "#89CFF0",
"userId": { "$oid": "68114174ff3abc3145e0ba5f" },
"createdAt": { "$date": "2025-04-29T15:20:00.000Z" }
},
{
"mood": "Angry",
"color": "#FF6B6B",
"userId": { "$oid": "68114174ff3abc3145e0ba5f" },
"createdAt": { "$date": "2025-04-29T15:30:00.000Z" }
}
]
Loading