|
| 1 | +function initializeSdk(callback) { |
| 2 | + (function (d, s, id) { |
| 3 | + var js, fjs = d.getElementsByTagName(s)[0]; |
| 4 | + |
| 5 | + if (d.getElementById(id)) { |
| 6 | + console.log('if statement got called'); |
| 7 | + return; |
| 8 | + } |
| 9 | + js = d.createElement(s); js.id = id; |
| 10 | + js.src = '//connect.facebook.net/en_US/sdk.js'; |
| 11 | + fjs.parentNode.insertBefore(js, fjs); |
| 12 | + } (document, 'script', 'facebook-jssdk')); |
| 13 | + |
| 14 | + |
| 15 | + // sdk callback function |
| 16 | + window.fbAsyncInit = () => { |
| 17 | + console.log('fbAsyncInit called'); |
| 18 | + // eslint-disable-next-line no-undef |
| 19 | + FB.init({ |
| 20 | + appId: '355696044788303', |
| 21 | + status: true, |
| 22 | + xfbml: true, |
| 23 | + version: 'v2.8', |
| 24 | + cookie: true, |
| 25 | + }); |
| 26 | + callback(); |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +function login(callback) { |
| 31 | + // eslint-disable-next-line no-undef |
| 32 | + FB.login((response) => { |
| 33 | + // console.log('login response'); |
| 34 | + // console.log(response); |
| 35 | + if (response && !response.error) { |
| 36 | + return callback(null, response); |
| 37 | + } |
| 38 | + callback({ message: 'Unable to log in', response: response }); |
| 39 | + }); |
| 40 | +} |
| 41 | +function logout() { |
| 42 | + // eslint-disable-next-line no-undef |
| 43 | + console.log(FB); |
| 44 | + // eslint-disable-next-line no-undef |
| 45 | + FB.logout(); |
| 46 | +} |
| 47 | + |
| 48 | +function getUserImage(user_id, callback) { |
| 49 | + |
| 50 | + // eslint-disable-next-line no-undef |
| 51 | + FB.api(`${user_id}/picture?type=large`, (response) => { |
| 52 | + if (response && response.data && response.data.url) { |
| 53 | + return callback(null, response.data.url); |
| 54 | + } |
| 55 | + // console.log(response); |
| 56 | + callback({ message: 'No image found' }); |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +function getUser(callback) { |
| 61 | + // eslint-disable-next-line no-undef |
| 62 | + FB.api('/me', function (response) { |
| 63 | + // console.log('Successful login for: ' + response.name); |
| 64 | + if (response && response.name) { |
| 65 | + console.log(response); |
| 66 | + return callback(null, response); |
| 67 | + } |
| 68 | + callback({ message: 'No such user found' }); |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +export { initializeSdk, login, logout, getUser, getUserImage }; |
0 commit comments