Skip to content

Instantly share code, notes, and snippets.

@wendellrocha
Last active November 23, 2020 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wendellrocha/794b2154bb18ce2b81b21c5da79cc76e to your computer and use it in GitHub Desktop.
Save wendellrocha/794b2154bb18ce2b81b21c5da79cc76e to your computer and use it in GitHub Desktop.
/**
* @author Wendell Rocha
*
* This file is required to override the LoginHandler method at server for Google oAuth/Facebook Login API/Apple Login API from enhanced_meteorify package.
* https://github.com/wendellrocha/enhanced_meteorify
*/
Accounts.registerLoginHandler(function (options) {
if (options.googleLoginPlugin || options.facebookLoginPlugin || options.appleLoginPlugin) {
var user;
let userId = null;
let serviceResponse = {};
var res;
if (options.googleLoginPlugin) {
user = Meteor.users.findOne({ 'emails.address': options.email });
let authHeaders = options.authHeaders.Authorization;
res = HTTP.get('https://people.googleapis.com/v1/people/me/', {
headers: {
'Authorization': authHeaders,
'User-Agent': 'Meteor/1.11.1'
},
params: {
personFields: 'names,phoneNumbers,birthdays,photos'
}
});
if (res.error) {
throw new Meteor.Error(400, res.error);
} else {
if (res.data) {
var googleResponse = {};
for (let item in res.data) {
if (item == 'resourceName') {
googleResponse['resourceName'] = res.data[item];
}
if (item == 'etag') {
googleResponse['etag'] = res.data[item];
}
if (item == 'names') {
for (let prop in item) {
if (res.data[item][prop].familyName != null && res.data[item][prop].familyName != undefined) {
googleResponse['sobrenome'] = res.data[item][prop].familyName;
}
if (res.data[item][prop].givenName != null) {
googleResponse['nome'] = res.data[item][prop].givenName;
break;
}
}
}
if (item == 'photos') {
for (let prop in item) {
if (res.data[item][prop].url != null && res.data[item][prop].url != undefined) {
let url = res.data[item][prop].url
googleResponse['avatar'] = url.replace('=s100', '');
break;
}
}
}
if (item == 'genders') {
for (let prop in item) {
if (res.data[item][prop].value != null && res.data[item][prop].value != undefined) {
googleResponse['genero'] = res.data[item][prop].value == 'male' ? 'Masculino' : 'Feminino';
break;
}
}
}
if (item == 'birthdays') {
for (let prop in item) {
if (res.data[item][prop].date != null && res.data[item][prop].date != undefined) {
if (res.data[item][prop]['date'].year == undefined) continue;
let day = res.data[item][prop]['date'].day;
let month = res.data[item][prop]['date'].month;
let year = res.data[item][prop]['date'].year;
googleResponse['data_nascimento'] = moment(new Date(year, month, day)).subtract(1, 'month').toISOString();
break;
}
}
}
if (item == 'phoneNumbers') {
for (let prop in item) {
if (res.data[item][prop].value != null && res.data[item][prop].value != undefined) {
let username = res.data[item][prop]['value'].replace('+55', '');
googleResponse['username'] = username;
googleResponse['ddd_cel'] = username.slice(0, 2);
googleResponse['celular'] = username.slice(2);
break;
}
}
}
}
googleResponse['authHeaders'] = options.authHeaders;
googleResponse['id'] = options.userId;
if (typeof (googleResponse['email']) === 'undefined') {
googleResponse['email'] = options.email;
}
}
serviceResponse = { google: googleResponse };
}
} else if (options.facebookLoginPlugin) {
res = HTTP.get(`https://graph.facebook.com/v8.0/${options.userId}`, {
headers: {
'User-Agent': 'Meteor/1.11.1'
},
params: {
fields: 'name,picture,first_name,last_name,email',
access_token: options.token
}
});
if (res.error) {
throw new Meteor.Error(400, res.error);
} else {
if (res.data && res.data.email != null) {
var facebookResponse = {};
user = Meteor.users.findOne({ 'emails.address': res.data.email });
facebookResponse['userId'] = options.userId;
facebookResponse['token'] = options.token;
facebookResponse['nome'] = res.data.first_name;
facebookResponse['sobrenome'] = res.data.last_name;
facebookResponse['email'] = res.data.email;
facebookResponse['avatar'] = res.data.picture.data.url;
serviceResponse = { facebook: facebookResponse };
} else {
throw new Meteor.Error(400, 'Você não tem um e-mail válido no facebook. Por favor, tente o login de outra forma.');
}
}
} else if (options.appleLoginPlugin) {
var appleResponse = {};
if (options.email != null) {
user = Meteor.users.findOne({ 'emails.address': options.email });
} else {
user = Meteor.users.findOne({ 'services.apple.userId': options.userId });
}
appleResponse['userId'] = options.userId;
if (options.givenName != null) {
appleResponse['nome'] = options.givenName;
}
if (options.lastName != null) {
appleResponse['sobrenome'] = options.lastName;
}
if (options.email != null) {
appleResponse['email'] = options.email;
}
serviceResponse = { apple: appleResponse };
}
if (!user) {
if (options.googleLoginPlugin) {
let insertObject = {
createdAt: new Date(),
services: serviceResponse,
emails: [{
address: options.email,
verified: true
}],
username: googleResponse.username,
roles: ["usuario"],
};
userId = Meteor.users.insert(insertObject);
let personDoc = {
id_usuario: userId,
username: googleResponse.username,
email: googleResponse.email,
nome: googleResponse.nome,
sobrenome: googleResponse.sobrenome != null ? googleResponse.sobrenome : 'Não informado',
ddd_cel: googleResponse.ddd_cel,
celular: googleResponse.celular,
id_usuario_alteracao: userId,
avatar: googleResponse.avatar,
genero: googleResponse.genero,
data_nascimento: googleResponse.data_nascimento,
tipo_pessoa: 'usuario',
cod_ref: [{ cod: Random.id(9), ativo: true }]
};
Pessoa.insert(personDoc);
} else if (options.facebookLoginPlugin) {
let insertObject = {
createdAt: new Date(),
services: serviceResponse,
emails: [{
address: facebookResponse.email,
verified: true
}],
roles: ["usuario"],
};
userId = Meteor.users.insert(insertObject);
let personDoc = {
id_usuario: userId,
email: facebookResponse.email,
nome: facebookResponse.nome,
sobrenome: facebookResponse.sobrenome != null ? facebookResponse.sobrenome : 'Não informado',
id_usuario_alteracao: userId,
avatar: facebookResponse.avatar,
tipo_pessoa: 'usuario',
cod_ref: [{ cod: Random.id(9), ativo: true }]
};
Pessoa.insert(personDoc);
} else if (options.appleLoginPlugin) {
let insertObject = {
createdAt: new Date(),
services: serviceResponse,
emails: [{
address: options.email,
verified: true
}],
roles: ["usuario"],
};
userId = Meteor.users.insert(insertObject);
let personDoc = {
id_usuario: userId,
email: options.email,
nome: appleResponse.nome,
sobrenome: appleResponse.sobrenome != null ? appleResponse.sobrenome : 'Não informado',
id_usuario_alteracao: userId,
tipo_pessoa: 'usuario',
cod_ref: [{ cod: Random.id(9), ativo: true }]
};
Pessoa.insert(personDoc);
}
} else {
userId = user._id;
let updateQuery = {};
if (options.googleLoginPlugin) {
updateQuery = { 'services.google': serviceResponse.google };
} else if (options.facebookLoginPlugin && serviceResponse != null) {
updateQuery = { 'services.facebook': serviceResponse.facebook };
} else if (options.appleLoginPlugin) {
updateQuery = { 'services.apple': serviceResponse.apple };
}
Meteor.users.update({ _id: userId }, { $set: updateQuery });
}
if (options.googleLoginPlugin) {
Meteor.users.update({ 'emails.address': options.email }, { $set: { 'emails.$.verified': true } });
} else if (options.facebookLoginPlugin && !_.isEmpty(serviceResponse)) {
Meteor.users.update({ 'emails.address': facebookResponse.email }, { $set: { 'emails.$.verified': true } });
} else if (options.appleLoginPlugin) {
if (options.email != null) {
Meteor.users.update({ 'emails.address': options.email }, { $set: { 'emails.$.verified': true } });
}
}
return { userId: userId };
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment