Skip to content

Commit

Permalink
Move auth functions to central location
Browse files Browse the repository at this point in the history
  • Loading branch information
Berkeley Martinez authored and Berkeley Martinez committed Aug 13, 2015
1 parent 49e9d07 commit a4e220e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 80 deletions.
47 changes: 5 additions & 42 deletions common/models/User-Identity.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,14 @@
import assign from 'object.assign';
import debugFactory from 'debug';

import {
setProfileFromGithub,
getFirstImageFromProfile
} 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
}
) {
return assign(
user,
{ isGithubCool: true, isMigrationGrandfathered: false },
{
name,
username: username.toLowerCase(),
location,
joinedGithubOn,
website,
picture,
githubId,
githubURL,
githubEmail,
githubProfile: githubURL
}
);
}

export default function(UserIdent) {
UserIdent.observe('before save', function(ctx, next) {
var userIdent = ctx.currentInstance || ctx.instance;
Expand Down
40 changes: 2 additions & 38 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ var uuid = require('node-uuid'),
path = require('path'),
passportProviders = require('./passport-providers');

var setProfileFromGithub = require('./utils/auth').setProfileFromGithub;
var generateKey =
require('loopback-component-passport/lib/models/utils').generateKey;
/**
* Create Express server.
*/

var app = loopback();

expressState.extend(app);
Expand Down Expand Up @@ -44,41 +43,6 @@ passportConfigurator.setupModels({
userCredentialModel: app.models.userCredential
});

// using es6 argument destructing
function setProfileFromGithub(
user,
{
profileUrl: githubURL,
username
},
{
id: githubId,
'avatar_url': picture,
email: githubEmail,
'created_at': joinedGithubOn,
blog: website,
location,
name
}
) {
return assign(
user,
{ isGithubCool: true, isMigrationGrandfathered: false },
{
name,
username: username.toLowerCase(),
location,
joinedGithubOn,
website,
picture,
githubId,
githubURL,
githubEmail,
githubProfile: githubURL
}
);
}

var passportOptions = {
emailOptional: true,
profileToUser: function(provider, profile) {
Expand Down
42 changes: 42 additions & 0 deletions server/utils/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import assign from 'object.assign';

// using es6 argument destructing
export function setProfileFromGithub(
user,
{
profileUrl: githubURL,
username
},
{
id: githubId,
'avatar_url': picture,
email: githubEmail,
'created_at': joinedGithubOn,
blog: website,
location,
name
}
) {
return assign(
user,
{ isGithubCool: true, isMigrationGrandfathered: false },
{
name,
username: username.toLowerCase(),
location,
joinedGithubOn,
website,
picture,
githubId,
githubURL,
githubEmail,
githubProfile: githubURL
}
);
}

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

0 comments on commit a4e220e

Please sign in to comment.