Skip to content

Instantly share code, notes, and snippets.

@onmyway133
Created August 20, 2018 17:30
Show Gist options
  • Save onmyway133/f142311025735a5001dc67a135effc09 to your computer and use it in GitHub Desktop.
Save onmyway133/f142311025735a5001dc67a135effc09 to your computer and use it in GitHub Desktop.
import React from 'react'
import FBSDK from 'react-native-fbsdk'
const { LoginButton, AccessToken, GraphRequest, GraphRequestManager } = FBSDK
class FacebookService {
constructor() {
this.requestManager = new GraphRequestManager()
}
makeLoginButton(callback) {
return (
<LoginButton
readPermissions={["public_profile"]}
onLoginFinished={(error, result) => {
if (error) {
} else if (result.isCancelled) {
} else {
AccessToken.getCurrentAccessToken()
.then((data) => {
callback(data.accessToken)
})
.catch(error => {
console.log(error)
})
}
}} />
)
}
makeLogoutButton(callback) {
return (
<LoginButton onLogoutFinished={() => {
callback()
}} />
)
}
async fetchProfile(callback) {
return new Promise((resolve, reject) => {
const request = new GraphRequest(
'/me',
null,
(error, result) => {
if (result) {
const profile = result
profile.avatar = `https://graph.facebook.com/${result.id}/picture`
resolve(profile)
} else {
reject(error)
}
}
)
this.requestManager.addRequest(request).start()
})
}
}
export const facebookService = new FacebookService()
@mikehardy
Copy link

@onmyway133 this is fantastically useful to me right now but I think this callback argument is unnecessary: https://gist.github.com/onmyway133/f142311025735a5001dc67a135effc09#file-a-js-L41

@onmyway133
Copy link
Author

@mikehardy Hi, thanks for thee feedback. Ja with async we don't need callback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment