'use strict';

var userModel = require('../database').models.user;

var create = function (data, callback){
	var newUser = new userModel(data);
	newUser.save(callback);
};

var findOne = function (data, callback){
	userModel.findOne(data, callback);
}

var findById = function (id, callback){
	userModel.findById(id, callback);
}


/**
 * Find a user, and create one if doesn't exist already.
 * This method is used ONLY to find user accounts registered via Social Authentication.
 *
 */
var findOrCreate = function(data, callback){
	findOne({'socialId': data.id}, function(err, user){
		if(err) { return callback(err); }
		if(user){
			return callback(err, user);
		}else{
			create({
				username: data.displayName,
				socialId: data.id,
				picture: data.photos[0].value || null
			}, function(err, newUser){
				callback(err, newUser);
			});
		}
	});
}

/**
 * A middleware allows user to get access to pages ONLY if the user is already logged in.
 *
 */
var isAuthenticated = function (req, res, next) {
	if(req.isAuthenticated()){
		next();
	}else{
		res.redirect('/');
	}
}

module.exports = { 
	create, 
	findOne, 
	findById, 
	findOrCreate, 
	isAuthenticated 
};
@@ -665,24 +665,34 @@
 %0A%09%09%7D
+ 
 else
+ 
 %7B%0A%09%09%09
-create(
+var userData = 
 %7B%0A%09%09
@@ -790,16 +790,334 @@
 ull%0A%09%09%09%7D
+;%0A%0A%09%09%09// To avoid expired Facebook CDN URLs%0A%09%09%09// Request user's profile picture using user id %0A%09%09%09// @see http://stackoverflow.com/a/34593933/6649553%0A%09%09%09if(data.provider == %22facebook%22 && userData.picture)%7B%0A%09%09%09%09userData.picture = %22http://graph.facebook.com/%22 + data.id + %22/picture?type=large%22;%0A%09%09%09%7D%0A%0A%09%09%09create(userData
 , functi
@@ -1492,10 +1492,11 @@
 icated %0A
-
 %7D;
+%0A

Fix & Avoid expired Facebook CDN URLs