Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ar ServiceConfiguration;module.import('meteor/service-configuration',{"ServiceConfiguration":function(v){ServiceConfiguration=v}});var Meteor;module.import('meteor/meteor',{"Meteor":function(v){Meteor=v}});var Accounts;module.import('meteor/accounts-base',{"Accounts":function(v){Accounts=v}});var HTTP;module.import('meteor/http',{"HTTP":function(v){HTTP=v}});var _;module.import('meteor/underscore',{"_":function(v){_=v}});
- // 2
- // 3
- // 4
- // 5
- //
- var settings = Meteor.settings && Meteor.settings.oauth && Meteor.settings.oauth.vk; // 7
- //
- var init = function init() { // 9
- if (settings) { // 10
- ServiceConfiguration.configurations.upsert({ service: "vk" }, { // 11
- $set: { // 14
- appId: settings.appId, // 15
- secret: settings.secret // 16
- } // 14
- }); // 13
- } // 20
- registerHandler(); // 21
- }; // 22
- //
- var registerHandler = function registerHandler() { // 24
- Accounts.registerLoginHandler('vk', function (params) { // 25
- console.log('registerLoginHandler vk', params) // 26
- try { // 27
- var data = params.vk; // 28
- //
- // If this isn't vk login then we don't care about it. No need to proceed. // 30
- if (!data) { // 31
- return undefined; // 32
- } // 33
- //
- // The fields we care about (same as Meteor's) // 35
- var whitelisted = ['uid', 'nickname', 'first_name', 'last_name', 'sex', 'bdate', 'timezone', 'photo', 'photo_big', 'city', 'country'];
- //
- // Get our user's identifying information. This also checks if the accessToken // 38
- // is valid. If not it will error out. // 39
- var identity = getIdentity(data.accessToken, whitelisted); // 40
- // Build our actual data object. // 41
- var serviceData = { // 42
- accessToken: data.accessToken, // 43
- expiresAt: +new Date() + 1000 * data.expirationTime // 44
- }; // 42
- var fields = Object.assign({}, serviceData, identity); // 46
- // Search for an existing user with that vk id // 47
- fields.id = identity.uid; // 48
- var existingUser = Meteor.users.findOne({ 'services.vk.uid': identity.uid }); // 49
- var userId = void 0; // 50
- var profile = { // 51
- name: identity.first_name + ' ' + identity.last_name, // 52
- firstName: identity.first_name, // 53
- lastName: identity.last_name, // 54
- picture: identity['photo_big'] // 55
- }; // 51
- if (existingUser) { // 57
- (function () { // 57
- userId = existingUser._id; // 58
- //
- // Update our data to be in line with the latest from Facebook // 60
- var prefixedData = {}; // 61
- _.each(fields, function (val, key) { // 62
- prefixedData['services.vk.' + key] = val; // 63
- }); // 64
- prefixedData['profile'] = _.extend(existingUser.profile, profile); // 65
- Meteor.users.update({ _id: userId }, { // 66
- $set: prefixedData // 67
- }); // 66
- })(); // 57
- } else { // 70
- // Create our user // 71
- console.log('create user', profile); // 72
- userId = Meteor.users.insert({ // 73
- services: { // 74
- vk: fields // 75
- }, // 74
- profile: _.extend(profile, { // 77
- approvedObservationsCount: 0, // 78
- place: -1 // 79
- }) // 77
- }); // 73
- } // 88
- //
- return { // 90
- userId: userId // 91
- }; // 90
- } catch (e) { // 93
- throw e; // 94
- } // 95
- }); // 96
- }; // 97
- //
- // Gets the identity of our user and by extension checks if // 99
- // our access token is valid. // 100
- var getIdentity = function getIdentity(accessToken, fields) { // 101
- try { // 102
- var result = HTTP.get("https://api.vk.com/method/users.get", { // 103
- params: { // 105
- access_token: accessToken, // 106
- fields: fields.join(', ') // 107
- } // 105
- }); // 104
- return result.data.response[0]; // 110
- } catch (err) { // 111
- throw _.extend(new Error("Failed to fetch identity from Vk. " + err.message), { response: err.response }); // 112
- } // 114
- }; // 115
- //
- module.export("default",exports.default=(init)); // 117
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment