Skip to content

Commit

Permalink
Implemented app.utility.debug and .error functions and introduced con…
Browse files Browse the repository at this point in the history
…fig.logging object so logging can be turned on or off for debug and error messages globally.
  • Loading branch information
asbjornu committed Nov 23, 2015
1 parent 792626b commit 4e37a65
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 190 deletions.
27 changes: 21 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ var config = require('./config'),
sequelizeStore = new SequelizeStore({ db: models.sequelize });

exports.setup = function(app) {
sequelizeStore.sync({ force: dbConfig.force });

//keep reference to config
app.config = config;

Expand Down Expand Up @@ -84,11 +82,28 @@ exports.setup = function(app) {

//setup utilities
app.utility = {
sendmail: require('./util/sendmail'),
slugify: require('./util/slugify'),
workflow: require('./util/workflow')
sendmail: require('./util/sendmail'),
slugify: require('./util/slugify'),
workflow: require('./util/workflow'),
debug: function() {
if (!config.logging.debug) {
return;
}

console.debug.apply(this, arguments);
},
error: function() {
if (!config.logging.error) {
return;
}

console.error.apply(this, arguments);
}
};

dbConfig.logging = dbConfig.logging ? app.utility.debug : false;
sequelizeStore.sync(dbConfig);

return app;
};

Expand All @@ -98,5 +113,5 @@ exports.app = this.setup(express());
// listen up
exports.app.server.listen(exports.app.config.port, function() {
//and... we're live
console.log('Server is running on port ' + config.port);
exports.app.utility.debug('Server is running on port ' + config.port);
});
9 changes: 7 additions & 2 deletions config.js.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
'use strict';

exports.port = process.env.PORT || (process.env.NODE_ENV == 'test' ? 3001 : 3000);
exports.logging = {
// Set this to true if you want extensive logging.
debug: false,
error: true
};
exports.companyName = 'Acme, Inc.';
exports.projectName = 'Drywall';
exports.systemEmail = '[email protected]';
Expand Down Expand Up @@ -53,10 +58,10 @@ exports.db = {
database: '<database>',
host: '127.0.0.1',
dialect: 'postgres',
// Logging to console.log. See the 'Options' section of
// Logging to app.utility.debug(log. See the 'Options' section of
// http://docs.sequelizejs.com/en/1.7.0/docs/usage/ for
// more information.
logging: console.log,
logging: app.utility.debug(log,
force: false
},
test: {
Expand Down
292 changes: 148 additions & 144 deletions passport.js
Original file line number Diff line number Diff line change
@@ -1,155 +1,159 @@
'use strict';

exports = module.exports = function(app, passport) {
var LocalStrategy = require('passport-local').Strategy,
TwitterStrategy = require('passport-twitter').Strategy,
GitHubStrategy = require('passport-github').Strategy,
FacebookStrategy = require('passport-facebook').Strategy,
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
TumblrStrategy = require('passport-tumblr').Strategy;

passport.use(new LocalStrategy(function(username, password, done) {
console.log('Passport.LocalStrategy:', username, done);

var conditions = {
where : {
isActive: 'yes'
}
};

if (username.indexOf('@') === -1) {
conditions.where.username = username;
} else {
conditions.where.email = username.toLowerCase();
}

app.db.User.findOne(conditions).then(function(user) {
var dataValues = user ? user.dataValues : null;
console.log('Passport.LocalStrategy:FindOne:', conditions, dataValues);

if (!user) {
return done(null, false, {
message: 'Unknown user'
});
}

app.db.User.validatePassword(password, user.password, function(err, isValid) {
console.log('Passport.LocalStrategy:ValidatePassword');

if (err) {
return done(err);
}

if (!isValid) {
return done(null, false, {
message: 'Invalid password'
});
}
return done(null, user);
});
})
.catch(function(err) {
return done(err);
});
}));

if (app.config.oauth.twitter.key) {
passport.use(new TwitterStrategy({
consumerKey: app.config.oauth.twitter.key,
consumerSecret: app.config.oauth.twitter.secret
},
function(token, tokenSecret, profile, done) {
done(null, false, {
token: token,
tokenSecret: tokenSecret,
profile: profile
});
}
));
var LocalStrategy = require('passport-local').Strategy,
TwitterStrategy = require('passport-twitter').Strategy,
GitHubStrategy = require('passport-github').Strategy,
FacebookStrategy = require('passport-facebook').Strategy,
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
TumblrStrategy = require('passport-tumblr').Strategy;

passport.use(new LocalStrategy(function(username, password, done) {
app.utility.debug('passport.localStrategy:', username, done)

var conditions = {
where: {
isActive: 'yes'
}
};

if (username.indexOf('@') === -1) {
conditions.where.username = username;
} else {
conditions.where.email = username.toLowerCase();
}

if (app.config.oauth.github.key) {
passport.use(new GitHubStrategy({
clientID: app.config.oauth.github.key,
clientSecret: app.config.oauth.github.secret,
customHeaders: {
"User-Agent": app.config.projectName
}
},
function(accessToken, refreshToken, profile, done) {
done(null, false, {
accessToken: accessToken,
refreshToken: refreshToken,
profile: profile
});
}
));
}
app.db.User.findOne(conditions).then(function(user) {
var dataValues = user ? user.dataValues : null;
app.utility.debug('passport.localStrategy.findOne:', conditions, dataValues);

if (app.config.oauth.facebook.key) {
passport.use(new FacebookStrategy({
clientID: app.config.oauth.facebook.key,
clientSecret: app.config.oauth.facebook.secret
},
function(accessToken, refreshToken, profile, done) {
done(null, false, {
accessToken: accessToken,
refreshToken: refreshToken,
profile: profile
});
}
));
}
if (!user) {
return done(null, false, {
message: 'Unknown user'
});
}

if (app.config.oauth.google.key) {
passport.use(new GoogleStrategy({
clientID: app.config.oauth.google.key,
clientSecret: app.config.oauth.google.secret
},
function(accessToken, refreshToken, profile, done) {
done(null, false, {
accessToken: accessToken,
refreshToken: refreshToken,
profile: profile
});
}
));
}
app.db.User.validatePassword(password, user.password, function(err, isValid) {
app.utility.debug('passport.localStrategy.ValidatePassword');

if (app.config.oauth.tumblr.key) {
passport.use(new TumblrStrategy({
consumerKey: app.config.oauth.tumblr.key,
consumerSecret: app.config.oauth.tumblr.secret
},
function(token, tokenSecret, profile, done) {
done(null, false, {
token: token,
tokenSecret: tokenSecret,
profile: profile
});
}
));
}
if (err) {
return done(err);
}

passport.serializeUser(function(user, done) {
var dataValues = user ? user.dataValues : null;
console.log('Passport.SerializeUser:', dataValues, done);

done(null, user.id);
});

passport.deserializeUser(function(id, done) {
console.log('Passport.DeserializeUser:', id, done);

app.db.User
.findOne({ where: { id: id }})
.then(function(user) {
done(null, user);
})
.catch(function(err) {
console.log('Error de-serializing ', err);
done(err);
if (!isValid) {
return done(null, false, {
message: 'Invalid password'
});
});
};
}
return done(null, user);
});
})
.catch(function(err) {
app.utility.error('passport.localStrategy.validatePassword:', err);
return done(err);
});
}));

if (app.config.oauth.twitter.key) {
passport.use(new TwitterStrategy({
consumerKey: app.config.oauth.twitter.key,
consumerSecret: app.config.oauth.twitter.secret
},
function(token, tokenSecret, profile, done) {
done(null, false, {
token: token,
tokenSecret: tokenSecret,
profile: profile
});
}
));
}

if (app.config.oauth.github.key) {
passport.use(new GitHubStrategy({
clientID: app.config.oauth.github.key,
clientSecret: app.config.oauth.github.secret,
customHeaders: {
"User-Agent": app.config.projectName
}
},
function(accessToken, refreshToken, profile, done) {
done(null, false, {
accessToken: accessToken,
refreshToken: refreshToken,
profile: profile
});
}
));
}

if (app.config.oauth.facebook.key) {
passport.use(new FacebookStrategy({
clientID: app.config.oauth.facebook.key,
clientSecret: app.config.oauth.facebook.secret
},
function(accessToken, refreshToken, profile, done) {
done(null, false, {
accessToken: accessToken,
refreshToken: refreshToken,
profile: profile
});
}
));
}

if (app.config.oauth.google.key) {
passport.use(new GoogleStrategy({
clientID: app.config.oauth.google.key,
clientSecret: app.config.oauth.google.secret
},
function(accessToken, refreshToken, profile, done) {
done(null, false, {
accessToken: accessToken,
refreshToken: refreshToken,
profile: profile
});
}
));
}

if (app.config.oauth.tumblr.key) {
passport.use(new TumblrStrategy({
consumerKey: app.config.oauth.tumblr.key,
consumerSecret: app.config.oauth.tumblr.secret
},
function(token, tokenSecret, profile, done) {
done(null, false, {
token: token,
tokenSecret: tokenSecret,
profile: profile
});
}
));
}

passport.serializeUser(function(user, done) {
var dataValues = user ? user.dataValues : null;
app.utility.debug('passport.serializeUser:', dataValues, done);

done(null, user.id);
});

passport.deserializeUser(function(id, done) {
app.utility.debug('passport.deserializeUser:', id, done);

app.db.User
.findOne({
where: {
id: id
}
})
.then(function(user) {
done(null, user);
})
.catch(function(err) {
app.utility.error('passport.deserializeUser:', err);
done(err);
});
});
};
2 changes: 1 addition & 1 deletion routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function ensureAuthenticated(req, res, next) {
if (req.session) {
req.session.returnUrl = req.originalUrl;
} else {
console.log('routes.ensureAuthenticated: No session!')
req.app.utility.error('routes.ensureAuthenticated: No session!')
}

res.redirect('/login/');
Expand Down
2 changes: 1 addition & 1 deletion util/workflow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exports = module.exports = function(req, res) {

workflow.on('response', function() {
workflow.outcome.success = !workflow.hasErrors();
console.log('Workflow.Response', workflow.outcome);
req.app.utility.debug('Workflow.Response', workflow.outcome);
res.send(workflow.outcome);
});

Expand Down
Loading

0 comments on commit 4e37a65

Please sign in to comment.