Skip to content

Instantly share code, notes, and snippets.

@khanghoang
Created March 27, 2015 04:12
Show Gist options
  • Save khanghoang/1f8fe4fa29abfedb76ce to your computer and use it in GitHub Desktop.
Save khanghoang/1f8fe4fa29abfedb76ce to your computer and use it in GitHub Desktop.
Native Facebook login with React Native from http://brentvatne.ca/facebook-login-with-react-native/
#import "FacebookLoginManager.h"
#import "FBSDKCoreKit/FBSDKCoreKit.h"
#import "FBSDKLoginKit/FBSDKLoginKit.h"
@implementation FacebookLoginManager
- (void)newSession:(RCTResponseSenderBlock)callback {
RCT_EXPORT();
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"public_profile", @"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
callback(@[@"Error", [NSNull null]]);
} else if (result.isCancelled) {
callback(@[@"Canceled", [NSNull null]]);
} else {
FBSDKAccessToken *token = result.token;
NSString *tokenString = token.tokenString;
NSString *userId = token.userID;
NSDictionary *credentials = @{ @"token" : tokenString, @"userId" : userId };
callback(@[[NSNull null], credentials]);
}
}];
}
@end
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight
} = React;
var FacebookLoginManager = require('NativeModules').FacebookLoginManager;
var FacebookLogin = React.createClass({
getInitialState() {
return {
result: '...'
}
},
componentDidMount() {
var self = this;
},
login() {
FacebookLoginManager.newSession((error, info) => {
if (error) {
this.setState({result: error});
} else {
this.setState({result: info});
}
});
},
render() {
return (
<View style={styles.container}>
<TouchableHighlight onPress={this.login}>
<Text style={styles.welcome}>
Facebook Login
</Text>
</TouchableHighlight>
<Text style={styles.instructions}>
{this.state.result}
</Text>
</View>
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment