Skip to content

Commit

Permalink
Merge branch 'staging' of github.com:FreeCodeCamp/freecodecamp into s…
Browse files Browse the repository at this point in the history
…taging
  • Loading branch information
Quincy Larson committed Aug 13, 2015
2 parents 99d0a58 + e76b5c8 commit bf2db5f
Show file tree
Hide file tree
Showing 249 changed files with 318 additions and 42,024 deletions.
244 changes: 165 additions & 79 deletions common/models/User-Identity.js
Original file line number Diff line number Diff line change
@@ -1,99 +1,185 @@
import assign from 'object.assign';
import loopback from 'loopback';
import debugFactory from 'debug';

import {
setProfileFromGithub,
getFirstImageFromProfile,
getSocialProvider
} from '../../server/utils/auth';

const debug = debugFactory('freecc:models:userIdent');

const { defaultProfileImage } = require('../utils/constantStrings.json');

function getFirstImageFromProfile(profile) {
return profile && profile.photos && profile.photos[0] ?
profile.photos[0].value :
null;
}

// using es6 argument destructing
function setProfileFromGithub(
user,
{
profileUrl: githubURL,
username
},
{
id: githubId,
'avatar_url': picture,
email: githubEmail,
'created_at': joinedGithubOn,
blog: website,
location,
name
function createAccessToken(user, ttl, cb) {
if (arguments.length === 2 && typeof ttl === 'function') {
cb = ttl;
ttl = 0;
}
) {
return assign(
user,
{ isGithubCool: true, isMigrationGrandfathered: false },
{
name,
username: username.toLowerCase(),
location,
joinedGithubOn,
website,
picture,
githubId,
githubURL,
githubEmail,
githubProfile: githubURL
}
);
user.accessTokens.create({
created: new Date(),
ttl: Math.min(ttl || user.constructor.settings.ttl,
user.constructor.settings.maxTTL)
}, cb);
}

export default function(UserIdent) {
UserIdent.observe('before save', function(ctx, next) {
var userIdent = ctx.currentInstance || ctx.instance;
if (!userIdent) {
debug('no user identity instance found');
return next();
}
userIdent.user(function(err, user) {
let userChanged = false;
if (err) { return next(err); }
if (!user) {
debug('no user attached to identity!');
return next();
// original source
// github.com/strongloop/loopback-component-passport
UserIdent.login = function(
provider,
authScheme,
profile,
credentials,
options,
cb
) {
options = options || {};
if (typeof options === 'function' && !cb) {
cb = options;
options = {};
}
var autoLogin = options.autoLogin || !options.autoLogin;
var userIdentityModel = UserIdent;
profile.id = profile.id || profile.openid;
userIdentityModel.findOne({
where: {
provider: getSocialProvider(provider),
externalId: profile.id
}
}, function(err, identity) {
if (err) {
return cb(err);
}
if (identity) {
identity.credentials = credentials;
return identity.updateAttributes({
profile: profile,
credentials: credentials,
modified: new Date()
}, function(err) {
if (err) {
return cb(err);
}
// Find the user for the given identity
return identity.user(function(err, user) {
// Create access token if the autoLogin flag is set to true
if (!err && user && autoLogin) {
return (options.createAccessToken || createAccessToken)(
user,
function(err, token) {
cb(err, user, identity, token);
}
);
}
cb(err, user, identity);
});
});
}
// Find the user model
var userModel = userIdentityModel.relations.user &&
userIdentityModel.relations.user.modelTo ||
loopback.getModelByType(loopback.User);

const { profile } = userIdent;
const picture = getFirstImageFromProfile(profile);
var userObj = options.profileToUser(provider, profile, options);

debug('picture', picture, user.picture);
// check if picture was found
// check if user has no picture
// check if user has default picture
// set user.picture from oauth provider
if (
picture &&
(!user.picture || user.picture === defaultProfileImage)
) {
debug('setting user picture');
user.picture = picture;
userChanged = true;
}
if (!userObj.email && !options.emailOptional) {
process.nextTick(function() {
return cb('email is missing from the user profile');
});
}

var query;
if (userObj.email) {
query = { or: [
{ username: userObj.username },
{ email: userObj.email }
]};
} else {
query = { username: userObj.username };
}
userModel.findOrCreate({ where: query }, userObj, function(err, user) {
if (err) {
return cb(err);
}
var date = new Date();
userIdentityModel.create({
provider: getSocialProvider(provider),
externalId: profile.id,
authScheme: authScheme,
profile: profile,
credentials: credentials,
userId: user.id,
created: date,
modified: date
}, function(err, identity) {
if (!err && user && autoLogin) {
return (options.createAccessToken || createAccessToken)(
user,
function(err, token) {
cb(err, user, identity, token);
}
);
}
cb(err, user, identity);
});
});
});
};

// if user signed in with github refresh their info
if (/github/.test(userIdent.provider)) {
debug("user isn't github cool or username from github is different");
setProfileFromGithub(user, profile, profile._json);
userChanged = true;
UserIdent.observe('before save', function(ctx, next) {
var userIdent = ctx.currentInstance || ctx.instance;
if (!userIdent) {
debug('no user identity instance found');
return next();
}
userIdent.user(function(err, user) {
let userChanged = false;
if (err) { return next(err); }
if (!user) {
debug('no user attached to identity!');
return next();
}

const { profile, provider } = userIdent;
const picture = getFirstImageFromProfile(profile);

if (userChanged) {
return user.save(function(err) {
if (err) { return next(err); }
next();
});
}
debug('exiting after user identity before save');
next();
debug('picture', picture, user.picture);
// check if picture was found
// check if user has no picture
// check if user has default picture
// set user.picture from oauth provider
if (
picture &&
(!user.picture || user.picture === defaultProfileImage)
) {
debug('setting user picture');
user.picture = picture;
userChanged = true;
}

if (!(/github/).test(provider)) {
debug('setting social', provider, (/github/g).test(provider));
debug('profile username', profile.username);
user[provider] = profile.username;
}

// if user signed in with github refresh their info
if (/github/.test(provider)) {
debug("user isn't github cool or username from github is different");
setProfileFromGithub(user, profile, profile._json);
userChanged = true;
}


if (userChanged) {
return user.save(function(err) {
if (err) { return next(err); }
next();
});
}
debug('exiting after user identity before save');
next();
});
});
}
25 changes: 10 additions & 15 deletions common/models/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,20 @@
"type": "string",
"default": ""
},
"linkedinProfile": {
"type": "string",
"default": ""
"linkedin": {
"type": "string"
},
"githubProfile": {
"type": "string",
"default": ""
"codepen": {
"type": "string"
},
"codepenProfile": {
"type": "string",
"default": ""
"twitter": {
"type": "string"
},
"twitterHandle": {
"type": "string",
"default": ""
"facebook": {
"type": "string"
},
"facebookProfile": {
"type": "string",
"default": ""
"google": {
"type": "string"
},
"completedBonfires": {
"type": [
Expand Down
78 changes: 0 additions & 78 deletions public/js/lib/codemirror/demo/activeline.html

This file was deleted.

Loading

0 comments on commit bf2db5f

Please sign in to comment.