From 84f52cbfbb9e11c43ffe350ee0725eb0b7b7bee1 Mon Sep 17 00:00:00 2001 From: Nurdaulet Date: Wed, 24 Aug 2016 21:37:17 +0600 Subject: [PATCH] vk.com auth data manager implemented --- src/authDataManager/vkontakte.js | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/authDataManager/vkontakte.js diff --git a/src/authDataManager/vkontakte.js b/src/authDataManager/vkontakte.js new file mode 100644 index 0000000000..a6899b58c1 --- /dev/null +++ b/src/authDataManager/vkontakte.js @@ -0,0 +1,43 @@ +'use strict'; + +// Helper functions for accessing the instagram API. +var https = require('https'); +var Parse = require('parse/node').Parse; + +// Returns a promise that fulfills iff this user id is valid. +function validateAuthData(authData) { + return request("users.get?v=V&access_token=" + authData.access_token).then(function (response) { + if (response && response.response && response.response[0].uid == authData.id) { + return; + } + throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Vk auth is invalid for this user.'); + }); +} + +// Returns a promise that fulfills iff this app id is valid. +function validateAppId() { + return Promise.resolve(); +} + +// A promisey wrapper for api requests +function request(path) { + return new Promise(function (resolve, reject) { + https.get("https://api.vk.com/method/" + path, function (res) { + var data = ''; + res.on('data', function (chunk) { + data += chunk; + }); + res.on('end', function () { + data = JSON.parse(data); + resolve(data); + }); + }).on('error', function (e) { + reject('Failed to validate this access token with Vk.'); + }); + }); +} + +module.exports = { + validateAppId: validateAppId, + validateAuthData: validateAuthData +};