Move auth functions to central location
This commit is contained in:
@ -1,51 +1,14 @@
|
|||||||
import assign from 'object.assign';
|
|
||||||
import debugFactory from 'debug';
|
import debugFactory from 'debug';
|
||||||
|
|
||||||
|
import {
|
||||||
|
setProfileFromGithub,
|
||||||
|
getFirstImageFromProfile
|
||||||
|
} from '../../server/utils/auth';
|
||||||
|
|
||||||
const debug = debugFactory('freecc:models:userIdent');
|
const debug = debugFactory('freecc:models:userIdent');
|
||||||
|
|
||||||
const { defaultProfileImage } = require('../utils/constantStrings.json');
|
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) {
|
export default function(UserIdent) {
|
||||||
UserIdent.observe('before save', function(ctx, next) {
|
UserIdent.observe('before save', function(ctx, next) {
|
||||||
var userIdent = ctx.currentInstance || ctx.instance;
|
var userIdent = ctx.currentInstance || ctx.instance;
|
||||||
|
@ -10,11 +10,10 @@ var uuid = require('node-uuid'),
|
|||||||
path = require('path'),
|
path = require('path'),
|
||||||
passportProviders = require('./passport-providers');
|
passportProviders = require('./passport-providers');
|
||||||
|
|
||||||
|
var setProfileFromGithub = require('./utils/auth').setProfileFromGithub;
|
||||||
var generateKey =
|
var generateKey =
|
||||||
require('loopback-component-passport/lib/models/utils').generateKey;
|
require('loopback-component-passport/lib/models/utils').generateKey;
|
||||||
/**
|
|
||||||
* Create Express server.
|
|
||||||
*/
|
|
||||||
var app = loopback();
|
var app = loopback();
|
||||||
|
|
||||||
expressState.extend(app);
|
expressState.extend(app);
|
||||||
@ -44,41 +43,6 @@ passportConfigurator.setupModels({
|
|||||||
userCredentialModel: app.models.userCredential
|
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 = {
|
var passportOptions = {
|
||||||
emailOptional: true,
|
emailOptional: true,
|
||||||
profileToUser: function(provider, profile) {
|
profileToUser: function(provider, profile) {
|
||||||
|
42
server/utils/auth.js
Normal file
42
server/utils/auth.js
Normal file
@ -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;
|
||||||
|
}
|
Reference in New Issue
Block a user