I'm trying to call signInWithFacebook method inside AccessToken.getCurrentAccessToken() but getting signInWithFacebook is undefined. The method works fine if I call it outside the LoginManager.logInWithReadPermissions function. I cannot seem to able to call any method inside the LoginManager.logInWithReadPermissions
I'm new to react native please help. Thanks.
const FBSDK = require('react-native-fbsdk');
const {
LoginManager,
AccessToken,
} = FBSDK;
import React from 'react';
import { Text, View, TouchableOpacity, Image } from 'react-native';
import { connect } from 'react-redux';
import { firebaseActions as auth } from "../actions";
const { signInWithFacebook } = auth;
class FacebookLogin extends React.Component {
constructor() {
super();
this.onSuccess = this.onSuccess.bind(this);
this.onError = this.onError.bind(this);
this.onSignInWithFacebook = this.onSignInWithFacebook.bind(this);
}
async onSignInWithFacebook() {
LoginManager.logInWithReadPermissions(["public_profile"]).then(
function (result) {
if (result.isCancelled) {
console.log("Login was Cancelled");
} else {
AccessToken.getCurrentAccessToken().then(
(data) => {
this.props.signInWithFacebook(data.accessToken, this.onSuccess, this.onError);
}
);
}
},
function (error) {
console.log("An error has occured" + error);
}
);
}
onSuccess({ exists, user }) {
console.log(
"Login Success"
);
}
onError(error) {
console.log(error.message);
}
render() {
return (
<View>
<TouchableOpacity
onPress={this.onSignInWithFacebook}
>
<Text>Login</Text>
</TouchableOpacity>
</View>
);
}
}
export default connect(null, { signInWithFacebook })(FacebookLogin);
for this only add one line to your code as:--
async onSignInWithFacebook() {
const _this = this;
LoginManager.logInWithReadPermissions(["public_profile"]).then(
function (result) {
if (result.isCancelled) {
console.log("Login was Cancelled");
} else {
AccessToken.getCurrentAccessToken().then(
(data) => {
_this.props.signInWithFacebook(data.accessToken, this.onSuccess, this.onError);
}
);
}
},
function (error) {
console.log("An error has occured" + error);
}
);
}
In your code LoginManager emitting a event so your this scope is changed and you got undefined signInWithFacebook.. notes:-- When attaching a function using addEventListener() the value of this is changed...