From edd995fd5f92bba8221c7b9384ad0cb7c59794e3 Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Thu, 25 Apr 2013 01:21:07 +0200 Subject: moved express config to own file. created configfile. --- config/config.js | 15 ++++++++++++++ config/express.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 config/config.js create mode 100644 config/express.js (limited to 'config') diff --git a/config/config.js b/config/config.js new file mode 100644 index 0000000..8312ddc --- /dev/null +++ b/config/config.js @@ -0,0 +1,15 @@ +var path = require('path') + , rootPath = path.normalize(__dirname + '/..'); + + +module.exports = { + development: { + db: 'mongodb://localhost/test', + root: rootPath, + app: { + name: 'Divid' + } + } +} + + diff --git a/config/express.js b/config/express.js new file mode 100644 index 0000000..4afd827 --- /dev/null +++ b/config/express.js @@ -0,0 +1,60 @@ + +/** + * Module dependencies + */ + +var express = require('express'); + + +/** + * Module + */ + +module.exports = function (app, config, passport) { + + //sets view engine and path + app.set('views', config.root + '/views'); + app.set('view engine', 'ejs'); + + app.use(express.static(config.root + '/public')); + + // don't use logger for test enc + if (process.env.NODE_ENV !== 'test') app.use(express.logger('dev')); + + + app.configure(function () { + + app.use(express.cookieParser()); //must be above sessions + + app.use(express.bodyParser()); //must be above methodOverride + + app.use(express.methodOverride()); + + app.use(express.session({ secret: 'lsdrghoi4hgqio42nqf2uqi32f3bilu23fl23b' })); + + // use passport session + app.use(passport.initialize()); + app.use(passport.session()); + + app.use(express.favicon(__dirname + '/public/faviconb.ico')); + + // use LESS for CSS + app.use(require('less-middleware')({ src: config.root + '/public' })); + + app.use(app.router); + + app.use(function(err, req, res, next) { + if (~err.message.indexOf('not fount')) return next(); // treat like 404 + + console.error(err.stack); + + res.status(500).render('500', { error: err.stack }); // render page + }); + + app.use(function(req, res, next) { + res.status(404).render('404', { url: req.originalUrl, error: 'Not found' }); // render page + }); + }); +} + + -- cgit v1.2.3 From 4dd7550b87386114ba4e86be17afbaa4d3fa11d6 Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Thu, 25 Apr 2013 02:12:34 +0200 Subject: addedpassport config file and did some other stuff --- config/config.js | 5 +++ config/passport.js | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 config/passport.js (limited to 'config') diff --git a/config/config.js b/config/config.js index 8312ddc..1eaffa1 100644 --- a/config/config.js +++ b/config/config.js @@ -8,6 +8,11 @@ module.exports = { root: rootPath, app: { name: 'Divid' + }, + facebook: { + clientID: "504825706245603", + clientSecret: "e5ea0faed85d8749cafd38732530ef35", + callbackURL: "https://divid.no/auth/facebook/callback" } } } diff --git a/config/passport.js b/config/passport.js new file mode 100644 index 0000000..9518409 --- /dev/null +++ b/config/passport.js @@ -0,0 +1,109 @@ +// dependencies +var mongoose = require('mongoose') + , LocalStrategy = require('passport-local').Strategy + , FacebookStrategy = require('passport-facebook').Strategy + , TwitterStrategy = require('passport-twitter').Strategy + , User = mongoose.model('User'); + +/** + * This is where the magic happends + */ + +module.exports = function (passport, config) { + + // serialize sessions + passport.serializeUser( function(user, done) { + done(null, user.id); + }); + passport.deserializeUser( function(id, done) { + User.findOne({ _id: id }, function(err, user) { + done(err, user); + }); + }); + + + /** + * Local strategy + */ + passport.use(new LocalStrategy({ + usernameField: 'email', + passwordField: 'password' + }, function(email, password, done) { + + // looks up the user in the database, and check if password matches + User.findOne({ email: email }, function(err, user) { + if (err) return done(err); + if (!user) return done(null, false, { message: 'Unknown user' }); + if (!user.authenticate(password)) return done(null, false, { message: 'Invalid password' }); + return done(null, user); + }); + })); + + + /** + * Facebook strategy + */ + passport.use(new FacebookStrategy({ + clientID: config.facebook.clientID + , clientSecret: config.facebook.clientSecret + , callbackURL: config.facebook.callbackURL + }, function(accessToken, refreshToken, profile, done) { + + // looks up the user in the database. Will create the user if it does not exist + User.findOne({ 'facebook.id': profile.id }, function(err, user) { + if(err) return done(err); + if (!user) { + user = new User({ + name: profile.displayName + , email: profile.emails[0].value + , username: profile.username + , provider: 'facebook' + , facebook: profile._json + }); + user.save(function(err) { + if (err) console.log(err); + return done(err, user); + }); + } else { + return done(err, user); + } + }); + })); + + + /** + * Twitter strategy + */ + passport.use(new TwitterStrategy({ + consumerKey: config.twitter.clientID + , consumerSecret: config.twitter.clientSecret + , callbackURL: config.twitter.callbackURL + }, function(token, tokenSecret, profile, done) { + + // looks up the user in the database. Will create the user if it does not exist + User.findOne({ 'twitter.id': profile.id }, function(err, user) { + if (err) return done(err); + if (!user) { + user = new User({ + name: profile.displayName + , username: profile.username + , provider: 'twitter' + , twitter: profile._json + }); + user.save(function(err) { + if (err) console.log(err); + return done(err, user); + }); + } else { + return done(err, user); + } + }); + })); + + + + + + +} + -- cgit v1.2.3 From a82fe16c44a6005546131e58c88c02474243105a Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Thu, 25 Apr 2013 02:21:53 +0200 Subject: added twitter config --- config/config.js | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'config') diff --git a/config/config.js b/config/config.js index 1eaffa1..f932387 100644 --- a/config/config.js +++ b/config/config.js @@ -14,6 +14,11 @@ module.exports = { clientSecret: "e5ea0faed85d8749cafd38732530ef35", callbackURL: "https://divid.no/auth/facebook/callback" } + twitter: { + clientID: "CONSUMER KEY", + clientSecret: "CONSUMER SECRET", + callbackURL: "https://divid.no/auth/twitter/callback" + } } } -- cgit v1.2.3 From d4b85e5e7765def9820dab5129f80d238e515591 Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Thu, 25 Apr 2013 16:34:48 +0200 Subject: fixed small bug --- config/config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'config') diff --git a/config/config.js b/config/config.js index f932387..8d120b2 100644 --- a/config/config.js +++ b/config/config.js @@ -13,7 +13,7 @@ module.exports = { clientID: "504825706245603", clientSecret: "e5ea0faed85d8749cafd38732530ef35", callbackURL: "https://divid.no/auth/facebook/callback" - } + }, twitter: { clientID: "CONSUMER KEY", clientSecret: "CONSUMER SECRET", -- cgit v1.2.3 From ff55ba7557f83a99a6e3cd0e26cf627b8890ed71 Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Fri, 26 Apr 2013 17:42:44 +0200 Subject: added twitter authentication --- config/config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'config') diff --git a/config/config.js b/config/config.js index 8d120b2..50dea23 100644 --- a/config/config.js +++ b/config/config.js @@ -15,8 +15,8 @@ module.exports = { callbackURL: "https://divid.no/auth/facebook/callback" }, twitter: { - clientID: "CONSUMER KEY", - clientSecret: "CONSUMER SECRET", + clientID: "tpCfKBUyAfogTpFxnb9w", + clientSecret: "abzInK4Nu0IFUhyXl73O2XjlFLFlzmBtLmbXk6v8", callbackURL: "https://divid.no/auth/twitter/callback" } } -- cgit v1.2.3 From fc50b5b7f18d02ffa8056dc1edbd3bd43e973493 Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Mon, 29 Apr 2013 03:39:15 +0200 Subject: been cleaning a bit, moving stuff to where it belongs. signup and signin works. --- config/email-settings.js | 13 ------------- config/express.js | 4 ++-- config/middlewares/authorization.js | 13 +++++++++++++ 3 files changed, 15 insertions(+), 15 deletions(-) delete mode 100644 config/email-settings.js create mode 100644 config/middlewares/authorization.js (limited to 'config') diff --git a/config/email-settings.js b/config/email-settings.js deleted file mode 100644 index 8bc9983..0000000 --- a/config/email-settings.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * Email settings - */ - -module.exports = { - - host: 'localhost', - user: 'divid', - password: '', - sender: 'Divid ' - -} - diff --git a/config/express.js b/config/express.js index 4afd827..eb10b20 100644 --- a/config/express.js +++ b/config/express.js @@ -48,11 +48,11 @@ module.exports = function (app, config, passport) { console.error(err.stack); - res.status(500).render('500', { error: err.stack }); // render page + res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack }); // render page }); app.use(function(req, res, next) { - res.status(404).render('404', { url: req.originalUrl, error: 'Not found' }); // render page + res.status(404).render('error', { title: '404', text: 'Fant ikke siden du så etter' }); // render page }); }); } diff --git a/config/middlewares/authorization.js b/config/middlewares/authorization.js new file mode 100644 index 0000000..440f085 --- /dev/null +++ b/config/middlewares/authorization.js @@ -0,0 +1,13 @@ + + +/* + * Generic require login routing + */ + +exports.requiresLogin = function(req, res, next) { + if (!req.isAuthenticated()) return res.redirect('/login'); + next(); +} + + + -- cgit v1.2.3