Skip to content
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

Login #11

Open
wants to merge 5 commits into
base: master
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
node_modules/
data/
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ It requires [node.js](http://nodejs.org/download/)
The server will run on port 3000.
You can test it in the (Chrome) browser at localhost:3000.

## User Database

Using a local MongoDB

* mongod --dbpath ./data --port 1664

You can configure the url in /config/database.js

## Author

- Pierre Chabardes
58 changes: 38 additions & 20 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,51 @@
/**
* Module dependencies.
*/
var express = require('express')
, http = require('http')
, path = require('path')
, streams = require('./app/streams.js')();

var app = express()
, server = http.createServer(app)
, io = require('socket.io').listen(server);

// all environments
app.set('port', 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon(__dirname + '/public/images/favicon.ico'));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
var express = require('express')
, http = require('http')
, path = require('path')
, mongoose = require('mongoose')
, passport = require('passport')
, flash = require('connect-flash')
, streams = require('./app/streams.js')();

var app = express()
, server = http.createServer(app)
, configDB = require('./config/database.js')
, io = require('socket.io').listen(server);

mongoose.connect(configDB.url);
require('./config/passport')(passport); // pass passport for configuration

app.configure(function() {
// all environments
app.set('port', 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon(__dirname + '/public/images/favicon.ico'));
app.use(express.logger('dev'));
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());

// required for passport
app.use(express.session({ secret: 'ilovemaplesyruppancakes' })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session

app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});


// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}

// routing
require('./app/routes.js')(app, streams);
require('./app/routes.js')(app, streams, passport);

/**
* Socket.io event handling
Expand Down
25 changes: 25 additions & 0 deletions app/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');

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

local : {
username: String,
password: String,
}
});

// methods ======================
// 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);
86 changes: 59 additions & 27 deletions app/routes.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,82 @@
module.exports = function(app, streams) {
module.exports = function(app, streams, passport) {

// GET home
// HOME PAGE (with login links)
var index = function(req, res) {
res.render('index', {
title: 'Project RTC',
header: 'WebRTC live streaming',
footer: '[email protected]',
id: req.params.id
res.render('index', {
title: 'Project RTC'
});
};

// GET crowd
var crowd = function(req, res) {
res.render('crowd', {
title: 'Project RTC',
footer: ''
// LOGIN PAGE
var login = function(req, res) {
res.render('login', {
title: 'Project RTC',
message: req.flash('loginMessage')
});
};

// GET join
var join = function(req, res) {
res.render('join', {
// LOGOUT PAGE
var logout = function(req, res) {
req.logout();
res.redirect('/');
};

// SIGNUP PAGE
var signup = function(req, res) {
res.render('signup', {
title: 'Project RTC',
message: req.flash('signupMessage')
});
};

// GET main
var main = function(req, res) {
res.render('main', {
title: 'Project RTC',
footer: ''
header: 'WebRTC live streaming',
footer: '[email protected]',
id: req.params.id,
user: req.user.local.username
});
};

// GET streams as JSON
var displayStreams = function(req, res) {
var id = req.params.id;
var streamList = streams.getStreams();
// JSON exploit to clone streamList.public
var data = (JSON.parse(JSON.stringify(streamList.public)));

/*
* if a specific id is requested, always add the stream even if private
*/
if(!!id) {
data[id] = streamList.public[id] || streamList.private[id];
}
var data = (JSON.parse(JSON.stringify(streamList)));

res.json(200, data);
};

// route middleware to make sure a user is logged in
var isLoggedIn = function(req, res, next) {

// if user is authenticated in the session, carry on
if (req.isAuthenticated())
return next();

// if they aren't redirect them to the home page
res.redirect('/');
}

app.get('/streams', displayStreams);
app.get('/streams/:id', displayStreams);
app.get('/main', isLoggedIn, main);
app.get('/login', login);
// process the login form
app.post('/login', passport.authenticate('local-login', {
successRedirect : '/main', // redirect to the secure profile section
failureRedirect : '/login', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
app.get('/logout', logout);
app.get('/signup', signup);
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/main', // redirect to the secure main section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
app.get('/', index);
app.get('/:id', index);
app.get('/:id', isLoggedIn, main);
}
4 changes: 2 additions & 2 deletions app/socketHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ module.exports = function(io, streams) {
client.on('readyToStream', function(options) {
console.log('-- ' + client.id + ' is ready to stream --');

streams.addStream(client.id, options.name, options.privacy);
streams.addStream(client.id, options.name);
});

client.on('rate', function(rating) {
streams.rate(rating.id, client.id, rating.points);
});

client.on('update', function(options) {
streams.update(client.id, options.name, options.privacy);
streams.update(client.id, options.name);
});

function leave() {
Expand Down
20 changes: 8 additions & 12 deletions app/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ module.exports = function() {
* available streams
* the id key is considered unique (provided by socket.io)
*/
var streamList = {
public: {},
private: {}
};
var streamList = {};

/**
* Stream object
Expand All @@ -21,19 +18,18 @@ module.exports = function() {
}

return {
addStream : function(id, name, isPrivate) {
addStream : function(id, name) {
var stream = new Stream(name);
isPrivate ? streamList.private[id] = stream : streamList.public[id] = stream;
streamList[id] = stream;
},

removeStream : function(id) {
delete streamList.public[id];
delete streamList.private[id];
delete streamList[id];
},

// rate function
rate : function(id, rater, rating) {
var stream = (streamList.public[id] || streamList.private[id]);
var stream = streamList[id];
if(stream.raters[rater] || stream.raters[rater] === null) {
stream.rating += rating - stream.raters[rater];
} else {
Expand All @@ -44,12 +40,12 @@ module.exports = function() {
},

// update function
update : function(id, name, isPrivate) {
var stream = streamList.public[id] || streamList.private[id];
update : function(id, name) {
var stream = streamList[id];
stream.name = name;

this.removeStream(id);
isPrivate ? streamList.private[id] = stream : streamList.public[id] = stream;
streamList[id] = stream;
},

getStreams : function() {
Expand Down
6 changes: 6 additions & 0 deletions config/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// config/database.js
module.exports = {

'url' : 'mongodb://localhost:1664'

};
Loading