Guest User

Untitled

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