Skip to content

Commit b07bd08

Browse files
Local Strategy passport
1 parent bcbd58b commit b07bd08

18 files changed

Lines changed: 680 additions & 67 deletions

File tree

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"topcoat": "~0.8.0",
77
"angular-route": "~1.2.15",
88
"fontawesome": "~4.0.3",
9-
"flat-ui-official": "~2.1.3"
9+
"flat-ui": "latest"
1010
}
1111
}

config/auth.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
/**
4+
* Route middleware to ensure user is authenticated.
5+
*/
6+
exports.ensureAuthenticated = function ensureAuthenticated(req, res, next) {
7+
if (req.isAuthenticated()) { return next(); }
8+
res.send(401);
9+
}
10+
11+
/**
12+
* Blog authorizations routing middleware
13+
*/
14+
exports.blog = {
15+
hasAuthorization: function(req, res, next) {
16+
if (req.blog.creator._id.toString() !== req.user._id.toString()) {
17+
return res.send(403);
18+
}
19+
next();
20+
}
21+
};

config/database.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Created by sreekanth on 1/3/15.
3+
*/
4+
// config/database.js
5+
module.exports = {
6+
7+
'url': 'mongodb://localhost/autherization' // looks like mongodb://<user>:<pass>@mongo.onmodulus.net:27017/Mikha4ot
8+
9+
};

config/pass.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// config/auth.js
2+
3+
// expose our config directly to our application using module.exports
4+
module.exports = {
5+
6+
'facebookAuth' : {
7+
'clientID' : '1511163462453285', // your App ID
8+
'clientSecret' : 'a469ebd9b3ee882cd1578d26ee91b491', // your App Secret
9+
'callbackURL' : 'http://localhost:8080/auth/facebook/callback'
10+
},
11+
12+
'twitterAuth' : {
13+
'consumerKey' : 'I9YLv8c0FJIYPACU5eYGRbcGW',
14+
'consumerSecret' : 'j9330GuivKIuwC3c8r3RfRNLycrDyZ2OfHFQEGW4h2zrLkdElY',
15+
'callbackURL' : 'http://localhost:8080/auth/twitter/callback'
16+
},
17+
18+
'googleAuth' : {
19+
'clientID' : '233449258545-tura73svarjsatjmc13v4q6oojqknhbg.apps.googleusercontent.com',
20+
'clientSecret' : 'Gmt7k6MzSWJ3ZSANiqU7OCAG',
21+
'callbackURL' : 'http://localhost:8080/auth/google/callback'
22+
}
23+
24+
};

config/passport.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* Created by sreekanth on 1/3/15.
3+
*/
4+
// load all the things we need
5+
var LocalStrategy = require('passport-local').Strategy;
6+
// load up the user model
7+
var User = require('../models/user');
8+
9+
// load the auth variables
10+
var configAuth = require('./auth'); // use this one for testing
11+
12+
module.exports = function(passport) {
13+
14+
// =========================================================================
15+
// LOCAL LOGIN =============================================================
16+
// =========================================================================
17+
passport.use('local-login', new LocalStrategy({
18+
// by default, local strategy uses username and password, we will override with email
19+
usernameField : 'email',
20+
passwordField : 'password',
21+
passReqToCallback : true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
22+
},
23+
function(req, email, password, done) {
24+
if (email)
25+
email = email.toLowerCase(); // Use lower-case e-mails to avoid case-sensitive e-mail matching
26+
27+
// asynchronous
28+
process.nextTick(function() {
29+
User.findOne({ 'local.email' : email }, function(err, user) {
30+
// if there are any errors, return the error
31+
if (err)
32+
return done(err);
33+
34+
// if no user is found, return the message
35+
if (!user)
36+
return done(null, false, req.flash('loginMessage', 'No user found.'));
37+
38+
if (!user.validPassword(password))
39+
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.'));
40+
41+
// all is well, return user
42+
else
43+
return done(null, user);
44+
});
45+
});
46+
47+
}));
48+
49+
// =========================================================================
50+
// LOCAL SIGNUP ============================================================
51+
// =========================================================================
52+
passport.use('local-signup', new LocalStrategy({
53+
// by default, local strategy uses username and password, we will override with email
54+
usernameField : 'email',
55+
passwordField : 'password',
56+
passReqToCallback : true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
57+
},
58+
function(req, email, password, done) {
59+
if (email)
60+
email = email.toLowerCase(); // Use lower-case e-mails to avoid case-sensitive e-mail matching
61+
62+
// asynchronous
63+
process.nextTick(function() {
64+
// if the user is not already logged in:
65+
if (!req.user) {
66+
User.findOne({ 'local.email' : email }, function(err, user) {
67+
// if there are any errors, return the error
68+
if (err)
69+
return done(err);
70+
71+
// check to see if theres already a user with that email
72+
if (user) {
73+
return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
74+
} else {
75+
76+
// create the user
77+
var newUser = new User();
78+
79+
newUser.local.email = email;
80+
newUser.local.password = newUser.generateHash(password);
81+
82+
newUser.save(function(err) {
83+
if (err)
84+
throw err;
85+
86+
return done(null, newUser);
87+
});
88+
}
89+
90+
});
91+
// if the user is logged in but has no local account...
92+
} else if ( !req.user.local.email ) {
93+
// ...presumably they're trying to connect a local account
94+
var user = req.user;
95+
user.local.email = email;
96+
user.local.password = user.generateHash(password);
97+
user.save(function(err) {
98+
if (err)
99+
throw err;
100+
return done(null, user);
101+
});
102+
} else {
103+
// user is logged in and already has a local account. Ignore signup. (You should log out before trying to create a new account, user!)
104+
return done(null, req.user);
105+
}
106+
107+
});
108+
109+
}));
110+
};
111+

controllers/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
*/
55

66
exports.index = function(req, res){
7-
res.render('index', { title: 'Express' });
7+
res.render('index.html', { title: 'Express' });
88
};

controllers/workspaces.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var fs = require('fs'),
44
_ = require('lodash'),
55
spawn = require('child_process').spawn;
66

7+
8+
79
var respondInvalidWorkspace = function(res) {
810
res.status(400);
911
res.json({msg: "Invalid workspace name"});
@@ -53,20 +55,26 @@ exports.create = function(req, res) {
5355
* GET workspaces listing.
5456
*/
5557
exports.list = function(req, res){
58+
59+
console.log("User : " + req.user);
5660
fs.readdir(__dirname + '/../workspaces/' + req.user, function(err, files) {
57-
if(err) {
58-
res.status(500);
59-
res.json({error: err});
60-
} else {
61+
//console.log("Error in listing workspaces. Workspace Directory : " + __dirname + '/../workspaces/');
62+
6163
var workspaces = [];
62-
for(var i=0; i< files.length; i++) {
63-
// Skip hidden files
64-
if(files[i][0] === '.') continue;
6564

66-
workspaces.push({name: files[i]})
65+
if (files != null || files != undefined) {
66+
for (var i = 0; i < files.length; i++) {
67+
// Skip hidden files
68+
if (files[i][0] === '.') continue;
69+
70+
workspaces.push({name: files[i]})
71+
}
72+
}
73+
else {
74+
workspaces.push({name: files[0]})
6775
}
6876
res.json({workspaces: workspaces});
69-
}
77+
7078
});
7179
};
7280

@@ -146,7 +154,7 @@ exports.destroy = function(req, res) {
146154

147155
if(typeof req.app.get('runningWorkspaces')[req.user + '/' + workspaceName] === 'undefined'){
148156
getNextAvailablePort(function(nextFreePort){
149-
console.log("Starting " + __dirname + '/../../c9/bin/cloud9.sh for workspace ' + workspaceName + " on port " + nextFreePort);
157+
console.log("Starting " + __dirname + ' together /../../c9/server.js for workspace ' + workspaceName + " on port " + nextFreePort);
150158

151159
var workspace = spawn(__dirname + '/../../c9/bin/cloud9.sh', ['-w', __dirname + '/../workspaces/' + req.user + '/' + workspaceName, '-l', '0.0.0.0', '-p', nextFreePort], {detached: true});
152160
workspace.stderr.on('data', function (data) {

models/user.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// load the things we need
2+
var mongoose = require('mongoose');
3+
var bcrypt = require('bcrypt-nodejs');
4+
5+
// define the schema for our user model
6+
var userSchema = mongoose.Schema({
7+
8+
local : {
9+
email : String,
10+
password : String,
11+
},
12+
facebook : {
13+
id : String,
14+
token : String,
15+
email : String,
16+
name : String
17+
},
18+
twitter : {
19+
id : String,
20+
token : String,
21+
displayName : String,
22+
username : String
23+
},
24+
google : {
25+
id : String,
26+
token : String,
27+
email : String,
28+
name : String
29+
}
30+
31+
});
32+
33+
// generating a hash
34+
userSchema.methods.generateHash = function(password) {
35+
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
36+
};
37+
38+
// checking if password is valid
39+
userSchema.methods.validPassword = function(password) {
40+
return bcrypt.compareSync(password, this.local.password);
41+
};
42+
43+
// create the model for users and expose it to our app
44+
module.exports = mongoose.model('User', userSchema);

package.json

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,31 @@
2424
"rimraf": "2.1.4",
2525
"stylus": "*",
2626
"swig": "^1.3.2",
27-
"view-helpers": "^0.1.4"
27+
"view-helpers": "^0.1.4",
28+
"mongoose": "~3.5.5",
29+
"ejs": "~0.8.4",
30+
"underscore": "~1.5.2",
31+
"mongoose" : "~3.8.1",
32+
"passport" : "0.1.17",
33+
"passport-local" : "~0.1.6",
34+
"passport-github" : "0.1.5",
35+
"connect-flash" : "~0.1.1",
36+
"bcrypt-nodejs" : "latest",
37+
"morgan": "~1.0.0",
38+
"body-parser": "~1.0.0",
39+
"cookie-parser": "~1.0.0",
40+
"method-override": "~1.0.0",
41+
"express-session": "~1.0.0",
42+
"ejs-locals": "*",
43+
"angular" : "*",
44+
"foundation" : "*",
45+
"jquery" : "*",
46+
"bootstrap" : "*",
47+
"consolidate": "^0.10.0",
48+
"forever": "~0.10.11",
49+
"jade": "*",
50+
"lodash": "^2.4.1",
51+
"connect-mongo": "~0.4.0"
52+
2853
}
2954
}

public/images/pcb-small.jpg

14.3 KB
Loading

0 commit comments

Comments
 (0)