Skip to content

Commit

Permalink
Merge pull request #35 from drewuse/myChatApp
Browse files Browse the repository at this point in the history
Improved chat interface and usability
  • Loading branch information
KaranErry authored Apr 16, 2019
2 parents 4b71e15 + 561d59e commit 0899838
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 115 deletions.
28 changes: 21 additions & 7 deletions routes/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var bodyParser = require('body-parser')
var $ = require("jquery");
var Long = require('mongodb').Long;
var current_millies = new Date().getTime();
var querystring = require('querystring');



Expand All @@ -20,7 +21,8 @@ router.use(bodyParser.urlencoded({ extended: false }))


/* GET home page. */
router.get('/' , function(req, res, next) {
router.get('/', checkAuthentication, function(req, res, next) {
preselectedThread = req.query.preselectedThread ? req.query.preselectedThread : '';
chatData.find({users:
{$elemMatch :
{
Expand All @@ -29,11 +31,11 @@ router.get('/' , function(req, res, next) {
}
}
).sort({ dateChatCreated: -1 }).then(function(doc) {
res.render('chat', { title: 'DrewUse', currentSession: req.session, chats:doc});
res.render('chat', { title: 'DrewUse', currentSession: req.session, chats:doc, preselectedThread:preselectedThread});
});
});

router.post('/newMessage', function(req,res){
router.post('/newMessage', checkAuthentication, function(req,res){
var current_timestamp = Long.fromNumber(current_millies);
var date = new Date(current_millies);
var dateReadable = date.toString();
Expand All @@ -47,12 +49,13 @@ router.post('/newMessage', function(req,res){
selectedPersonSelling: req.body.selectedPersonSelling
}
var data = new chatData(chat);
data.save();
res.redirect('/chat');
data.save((err, doc) => {
res.redirect('/chat?' + querystring.stringify({'preselectedThread':doc.id}));
});
});


router.post('/messages/getInfo', (req, res) => {
router.post('/messages/getInfo', checkAuthentication, (req, res) => {
console.log(req.body.threadId);
chatData.find({_id: req.body.threadId})
.then(function(doc) {
Expand All @@ -63,7 +66,7 @@ router.post('/messages/getInfo', (req, res) => {
});


router.post('/messages',async (req, res) => {
router.post('/messages', checkAuthentication, async (req, res) => {
var current_timestamp = Long.fromNumber(current_millies);
console.log("post messages route");
console.log(req.body);
Expand All @@ -72,5 +75,16 @@ router.post('/messages',async (req, res) => {

})

//authenticate a user is logged in
function checkAuthentication(req,res,next){
if(req.isAuthenticated()){
//req.isAuthenticated() will return true if user is logged in
next();
} else{
req.session.authorigin = 'chat';
res.redirect('/auth/google/callback')
}
}


module.exports = router;
Loading

0 comments on commit 0899838

Please sign in to comment.