Skip to content

Commit

Permalink
add pull in user image on signup when available
Browse files Browse the repository at this point in the history
  • Loading branch information
Berkeley Martinez authored and Berkeley Martinez committed Jun 11, 2015
1 parent 4d8ab0a commit fe5cbec
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
61 changes: 34 additions & 27 deletions common/models/User-Identity.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
// var debug = require('debug')('freecc:models:userIdent');
//
// module.exports = function(UserIdent) {
//
// UserIdent.observe('before save', function(ctx, next) {
//
// var userIdent = ctx.instance;
// userIdent.user(function(err, user) {
// if (err) { return next(err); }
// debug('got user', user.username);
//
// // check if user has picture
// // set user.picture from twitter
// if (!user.picture) {
// debug('use has no pic');
// user.picture = userIdent.profile.photos[0].value;
// user.save(function(err) {
// if (err) { return next(err); }
// next();
// });
// } else {
// debug('exiting after user ident');
// next();
// }
// });
// });
// };
var debug = require('debug')('freecc:models:userIdent');

var defaultProfileImage =
require('../utils/constantStrings.json').defaultProfileImage;

module.exports = function(UserIdent) {

UserIdent.observe('before save', function(ctx, next) {
var userIdent = ctx.currentInstance;
// treat userIdent as immutable
userIdent.user(function(err, user) {
if (err) { return next(err); }
debug('got user', user.username);

var picture = userIdent.profile && userIdent.profile[0] ?
userIdent.profile[0].value :
null;

// check if user has picture
// set user.picture from twitter
if (picture && !user.picture || user.picture === defaultProfileImage) {
debug('use has no pic');
user.picture = userIdent.profile.photos[0].value;
user.save(function(err) {
if (err) { return next(err); }
next();
});
} else {
debug('exiting after user ident');
next();
}
});
});
};
3 changes: 3 additions & 0 deletions common/utils/constantStrings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"defaultProfileImage": "https://s3.amazonaws.com/freecodecamp/camper-image-placeholder.png"
}
5 changes: 3 additions & 2 deletions server/boot/home.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var defaultProfileImage =
require('../../common/utils/constantStrings.json').defaultProfileImage;
var message =
'Learn to Code JavaScript and get a Coding Job by Helping Nonprofits';

Expand All @@ -9,8 +11,7 @@ module.exports = function(app) {

function index(req, res, next) {
if (req.user && !req.user.picture) {
req.user.picture =
'https://s3.amazonaws.com/freecodecamp/camper-image-placeholder.png';
req.user.picture = defaultProfileImage;

req.user.save(function(err) {
if (err) { return next(err); }
Expand Down

0 comments on commit fe5cbec

Please sign in to comment.