Reputation: 79
I'm working in with react-native-fbsdk. I want to get username and email to show up to the user when they're registered to the app in order to create a profile for the app, but no data is shown. How can I get these data and how can I show it in a different screen.
This is my code
import FBSDK from 'react-native-fbsdk'
import { LoginButton,AccessToken,GraphRequest,GraphRequestManager} from 'react-native-fbsdk';
const {
LoginManager,
} = FBSDK;
<LoginButton
readPermissions={['public_profile']}
onLoginFinished={
(error, result) => {
if (error) {
alert("login has error: " + result.error);
} else if (result.isCancelled) {
alert("login is cancelled.");
} else {
AccessToken.getCurrentAccessToken().then(
(data) => {
const infoRequest = new GraphRequest(
'/me?fields=name,picture',
null,
this._responseInfoCallback
);
// Start the graph request.
new GraphRequestManager().addRequest(infoRequest).start();
}
)
}
}
}
onLogoutFinished={() => alert("logout.")}/>
</View>
);
}
_responseInfoCallback = (error, result) => {
if (error) {
alert('Error fetching data: ' + error.toString());
} else {
alert('Result Name: ' + result.name);
}
}
}
Thanks friends
Upvotes: 0
Views: 4646
Reputation: 727
If you don't want to use firebase,
const infoRequest = new GraphRequest(
'/me?fields=name,email,picture.type(large)',
null,
this._responseInfoCallback
);
new GraphRequestManager().addRequest(infoRequest).start();
_responseInfoCallback = (error, result) => {
if (error) {
alert('Error fetching data: ' + error.toString());
} else {
this.setState({ userName: result.name, userEmail: result.email ,userPic:result.picture.data.url});
AsyncStorage.setItem("UserName",this.state.userName);
AsyncStorage.setItem("Email", this.state.userEmail);
AsyncStorage.setItem("UserPic", this.state.userPic);
// SharedPreferences.setItem("UserName", this.state.userName);
// SharedPreferences.setItem("Email", this.state.userEmail);
// SharedPreferences.setItem("UserPic", this.state.userPic);
console.log("Picture"+ this.state.userPic + "Name" + this.state.userName + "Email" + this.state.userEmail);
}
}
Upvotes: 0
Reputation: 344
I retrieve facebook login while I using firebase.
import this file:
import firebase from "react-native-firebase";
import { AccessToken, LoginManager } from "react-native-fbsdk";
Firebase login code work when I click button:
export const fbLogin = () => {
LoginManager.logInWithReadPermissions(["public_profile", "email"])
.then(result => {
if (result.isCancelled) {
console.log("Login was cancelled");
}
return AccessToken.getCurrentAccessToken();
})
.then(data => {
const credential = firebase.auth.FacebookAuthProvider.credential(
data.accessToken
);
firebase
.auth()
.signInWithCredential(credential)
.then(result => {
Toast.show({
text: "Sucessfully",
position: "top"
});
console.log("Successfully Login", result);
})
.catch(error => {
console.log("Failed", error);
});
})
.catch(err => {
console.log("fail", err);
});
};
I retrive my facebook login data
console.log("Successfully Login", result);
result contains username, email, picture and ..ect credentials
Upvotes: 1