Skip to content

Instantly share code, notes, and snippets.

@josefrichter
Last active April 1, 2019 23:50
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josefrichter/524dbffc2a37d4952b8a to your computer and use it in GitHub Desktop.
Save josefrichter/524dbffc2a37d4952b8a to your computer and use it in GitHub Desktop.
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
'use strict';
const React = require('react-native');
const FBSDKCore = require('react-native-fbsdkcore');
const FBSDKLogin = require('react-native-fbsdklogin');
var Parse = require('parse/react-native');
var ParseReact = require('parse-react/react-native');
Parse.initialize(
'******',
'******'
);
const {
StyleSheet,
Text,
View,
Component,
} = React;
const {
FBSDKGraphRequest,
FBSDKAccessToken,
} = FBSDKCore;
const {
FBSDKLoginButton,
} = FBSDKLogin;
class LoginScreen extends Component {
_onPressFBLoginButton(credentials) {
// the timestamp needs to be reformatted for Parse http://stackoverflow.com/questions/12945003/format-date-as-yyyy-mm-ddthhmmss-sssz
var expdate = new Date(credentials._expirationDate);
expdate = expdate.toISOString();
// these are the data from the successful FB login we will pass to Parse.FacebookUtils.logIn instead of null
// based on https://github.com/ParsePlatform/ParseReact/issues/45#issuecomment-111063927
let authData = {
id: credentials.userID,
access_token: credentials.tokenString,
expiration_date: expdate
};
Parse.FacebookUtils.logIn(authData, {
success: function(user) {
if (!user.existed()) {
alert("User signed up and logged in through Facebook!");
} else {
alert("User logged in through Facebook!");
}
},
error: function(user, error) {
console.log(user,error);
alert("User cancelled the Facebook login or did not fully authorize.");
}
});
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Log in
</Text>
<FBSDKLoginButton
onLoginFinished={(error, result) => {
if (error) {
alert('Error logging in.');
} else {
if (result.isCancelled) {
alert('Login cancelled.');
} else {
console.log(result);
// we need to make one more request to get the token becase the token is not sent back in the login
var fetchMeRequest = new FBSDKGraphRequest((error, result) => {
if (error) {
alert('Error making request.');
} else {
// Data from request is in result
FBSDKAccessToken.getCurrentAccessToken((token) => {
//console.log(token);
this._onPressFBLoginButton(token);
});
}
}, '/me?fields=id,first_name,name,birthday,gender,email');
// Start the graph request.
fetchMeRequest.start();
}
}
}}
onLogoutFinished={() => alert('Logged out.')}
readPermissions={['public_profile', 'email']}
publishPermissions={[]}/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
});
module.exports = LoginScreen;
@atom2ueki
Copy link

this helps, thanks

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