Issues (34)

app/routes/index.js (10 issues)

1
'use strict';
2
3
var express	 	= require('express');
4
var router 		= express.Router();
5
var passport 	= require('passport');
6
7
var User = require('../models/user');
8
var Room = require('../models/room');
9
10
// Home page
11
router.get('/', function(req, res, next) {
0 ignored issues
show
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
12
	// If user is already logged in, then redirect to rooms page
13
	if(req.isAuthenticated()){
14
		res.redirect('/rooms');
15
	}
16
	else{
17
		res.render('login', {
18
			success: req.flash('success')[0],
19
			errors: req.flash('error'), 
20
			showRegisterForm: req.flash('showRegisterForm')[0]
21
		});
22
	}
23
});
24
25
// Login
26
router.post('/login', passport.authenticate('local', { 
27
	successRedirect: '/rooms', 
28
	failureRedirect: '/',
29
	failureFlash: true
30
}));
31
32
// Register via username and password
33
router.post('/register', function(req, res, next) {
0 ignored issues
show
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
34
35
	var credentials = {'username': req.body.username, 'password': req.body.password };
36
37
	if(credentials.username === '' || credentials.password === ''){
38
		req.flash('error', 'Missing credentials');
39
		req.flash('showRegisterForm', true);
40
		res.redirect('/');
41
	}else{
42
43
		// Check if the username already exists for non-social account
44
		User.findOne({'username': new RegExp('^' + req.body.username + '$', 'i'), 'socialId': null}, function(err, user){
45
			if(err) throw err;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
46
			if(user){
47
				req.flash('error', 'Username already exists.');
48
				req.flash('showRegisterForm', true);
49
				res.redirect('/');
50
			}else{
51
				User.create(credentials, function(err, newUser){
0 ignored issues
show
The parameter newUser is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
52
					if(err) throw err;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
53
					req.flash('success', 'Your account has been created. Please log in.');
54
					res.redirect('/');
55
				});
56
			}
57
		});
58
	}
59
});
60
61
// Social Authentication routes
62
// 1. Login via Facebook
63
router.get('/auth/facebook', passport.authenticate('facebook'));
64
router.get('/auth/facebook/callback', passport.authenticate('facebook', {
65
		successRedirect: '/rooms',
66
		failureRedirect: '/',
67
		failureFlash: true
68
}));
69
70
// 2. Login via Twitter
71
router.get('/auth/twitter', passport.authenticate('twitter'));
72
router.get('/auth/twitter/callback', passport.authenticate('twitter', {
73
		successRedirect: '/rooms',
74
		failureRedirect: '/',
75
		failureFlash: true
76
}));
77
78
// Rooms
79
router.get('/rooms', [User.isAuthenticated, function(req, res, next) {
0 ignored issues
show
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
80
	Room.find(function(err, rooms){
81
		if(err) throw err;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
82
		res.render('rooms', { rooms });
83
	});
84
}]);
85
86
// Chat Room 
87
router.get('/chat/:id', [User.isAuthenticated, function(req, res, next) {
88
	var roomId = req.params.id;
89
	Room.findById(roomId, function(err, room){
90
		if(err) throw err;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
91
		if(!room){
92
			return next(); 
93
		}
94
		res.render('chatroom', { user: req.user, room: room });
0 ignored issues
show
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
95
	});
96
	
97
}]);
98
99
// Logout
100
router.get('/logout', function(req, res, next) {
0 ignored issues
show
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
101
	// remove the req.user property and clear the login session
102
	req.logout();
103
104
	// destroy session data
105
	req.session = null;
106
107
	// redirect to homepage
108
	res.redirect('/');
109
});
110
111
module.exports = router;