Skip to content

Commit

Permalink
Minor changes before switching to new branch for multiple room sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
tameeshB committed Dec 12, 2017
1 parent 34c30a1 commit 8aa7d13
Show file tree
Hide file tree
Showing 12 changed files with 318 additions and 323 deletions.
5 changes: 5 additions & 0 deletions config/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ module.exports = {
'clientID' : '',
'clientSecret' : '',
'callbackURL' : 'http://localhost:8080/auth/google/callback'
},

'emailCreds' : {
'email' : '',
'pass' : ''
}

};
168 changes: 79 additions & 89 deletions config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,115 +5,103 @@ var FacebookStrategy = require('passport-facebook').Strategy;
var User = require('../models/user');
var configAuth = require('./auth');

module.exports = function(passport, app){
passport.serializeUser(function(user, done) {
module.exports = function (passport, app) {
passport.serializeUser(function (user, done) {
done(null, user.id);
});

passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
//local
//local
passport.use('local-signup', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
},
function(req, email, password, done) {

// asynchronous
// User.findOne wont fire unless data is sent back
process.nextTick(function() {

// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({$or: [
{ 'local.email' : email },
{ 'username' : req.body.uname }
]}, function(err, user) {
// if there are any errors, return the error
if (err)
return done(err);

// check to see if theres already a user with that email
if (user) {
return done(null, false, req.flash('signupMessage', 'That email/username is already taken.'));
} else {

// if there is no user with that email
// create the user
var newUser = new User();

// set the user's local credentials
newUser.local.email = email;
newUser.local.password = newUser.generateHash(password);
newUser.username = req.body.uname;
newUser.name = req.body.name;

// save the user
newUser.save(function(err) {
function (req, email, password, done) {
process.nextTick(function () {
User.findOne({
$or: [
{ 'local.email': email },
{ 'username': req.body.uname }
]
}, function (err, user) {
if (err)
throw err;
app.mailer.send('email', {
to: '[email protected]',
subject: 'New SignUp :' + email + ' : ' + req.body.uname,
}, function (err) {
if (err) {
// handle error
console.log(err);
req.flash('emailMsg','There was an error sending the email');
return;
}
req.flash('emailMsg','Email Sent');
});
req.flash('loginMessage', 'You are registered! Login here to continue!');
return done(null, newUser);
});
}
return done(err);
//check dupes
if (user) {
return done(null, false, req.flash('signupMessage', 'That email/username is already taken.'));
} else {
var newUser = new User();

});
newUser.local.email = email;
newUser.local.password = newUser.generateHash(password);
newUser.hash = newUser.generateAccessHash(req.body.uname, password);
newUser.username = req.body.uname;
newUser.name = req.body.name;

});
// save the user
newUser.save(function (err) {
if (err)
throw err;
app.mailer.send('email', {
to: '[email protected]',
subject: 'New SignUp :' + email + ' : ' + req.body.uname,
}, function (err) {
if (err) {
// handle error
console.log(err);
req.flash('emailMsg', 'There was an error sending the email');
return;
}
req.flash('emailMsg', 'Email Sent');
});
req.flash('loginMessage', 'You are registered! Login here to continue!');

return done(null, newUser);
});
}

});

}));
});

}));


//local login
passport.use('local-login', new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true
},
function(req, email, password, done) { // callback with email and password from our form

// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({ 'local.email' : email }, function(err, user) {
// if there are any errors, return the error before anything else
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
},
function (req, email, password, done) {
User.findOne({
'local.email': email
}, function (err, user) {
if (err)
return done(err);

// if no user is found, return the message
if (!user)
return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash

// if the user is found but the password is wrong
return done(null, false, req.flash('loginMessage', 'No user found.'));
if (!user.validPassword(password))
return done(null, false, req.flash('loginMessage', 'Wrong password.'));
console.log('login successful');
console.log(user);
console.log(req.user);
console.log('login successful1.5');

req.user = user;
return done(null, false, req.flash('loginMessage', 'Wrong password.'));
var ssn = req.session;
if (user.rooms){
ssn.rooms = user.rooms;
roomsGL_id = user.rooms;
userGL = user;
}
else{
ssn.rooms = [];
roomsGL_id = {};
userGL = user;
}
req.user = user;
ssn.user = user;
console.log(req.session.user);
console.log('login successful2');
return done(null, user);
return done(null, user);

});

Expand All @@ -133,14 +121,16 @@ module.exports = function(passport, app){
process.nextTick(function () {

// find the user in the database based on their facebook id
User.findOne({ 'facebook.id': profile.id }, function (err, user) {
User.findOne({
'facebook.id': profile.id
}, function (err, user) {
if (err)
return done(err);
if (user) {
//login
var ssn = req.session;
ssn.user = user;
return done(null, user);
return done(null, user);
} else {
var newUser = new User();
newUser.facebook.id = profile.id;
Expand Down
3 changes: 3 additions & 0 deletions globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global.roomsGL_id = {};
global.roomsGL_name = {};
global.userGL = {};
8 changes: 7 additions & 1 deletion models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ var bcrypt = require('bcrypt-nodejs');

var userSchema = mongoose.Schema({
username: String,
name : String,
name : String,
hash: String,
rooms: [Number],
local : {
email : String,
password : String,
Expand Down Expand Up @@ -34,6 +36,10 @@ userSchema.methods.generateHash = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};

userSchema.methods.generateAccessHash = function (username, password) {
return bcrypt.hashSync(username + password, bcrypt.genSaltSync(8), null);
};

userSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.local.password);
};
Expand Down
Binary file removed public/.DS_Store
Binary file not shown.
Binary file added public/images/bg/gif.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 24 additions & 6 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@

require('../globals');
module.exports = function(app, passport, db) {

app.get('/:type(|about)',function(req,res){
// res.sendFile(__dirname+'/index.html');
console.log(req);
// console.log(req);
if (req.isAuthenticated() && req.url!='/about'){

res.redirect('/app');
}else{
res.render('index',{messages:null,user:req.session.user});
}

});

// app.get('/test/:a/:b',function(req,res){

// db.users.findOne({ 'username': req.params.a, 'name': req.params.b }, function (err, doc) {
// if (err) {
// res.send(err);
// }
// if(doc)
// res.send('a'+doc);
// if(!doc)
// res.send('b' + doc);
// });
// });

app.get('/app', isLoggedIn,function (req, res) {

var rooms = [];
db.rooms.find({
roomid: { $elemMatch: req.session.rooms }
},function(err,docs){
if(err)
console.log(err);
else
rooms = docs;
})
db.messages.find(function (err, docs) {
res.render('chat', { messages: docs, user: req.user });
res.render('chat', { messages: docs, user: req.user, rooms: rooms });
});
});

Expand Down
Loading

0 comments on commit 8aa7d13

Please sign in to comment.