Skip to content
This repository has been archived by the owner on Apr 2, 2021. It is now read-only.

Add reauthorizeDataAccess method to LoginManager #720

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ public void logInWithPermissions(ReadableArray permissions, final Promise promis
}
}

/**
* Attempts a re-authorization to regain data access.
* @param promise Use promise to pass re-authorization result to JS after re-authorization finish.
*/
@ReactMethod
public void reauthorizeDataAccess(final Promise promise) {
final LoginManager loginManager = LoginManager.getInstance();
loginManager.registerCallback(getCallbackManager(), new LoginManagerCallback(promise));
Activity activity = getCurrentActivity();
if (activity != null) {
loginManager.reauthorizeDataAccess(activity);
}
}

private WritableArray setToWritableArray(Set<String> set) {
WritableArray array = Arguments.createArray();
for (String e: set) {
Expand Down
31 changes: 27 additions & 4 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,35 @@
*/

import React, {Component} from 'react';
import {Alert, StyleSheet, Text, TouchableHighlight, View} from 'react-native';
import {LoginButton, ShareDialog} from 'react-native-fbsdk';
import {
Alert,
Button,
StyleSheet,
Text,
TouchableHighlight,
View,
} from 'react-native';
import {LoginButton, LoginManager, ShareDialog} from 'react-native-fbsdk';

const SHARE_LINK_CONTENT = {
contentType: 'link',
contentUrl: 'https://www.facebook.com/',
};

export default class App extends Component<{}> {
_alert = (title, obj) => {
Alert.alert(title, JSON.stringify(obj, null, 2));
};

_reauthorizeDataAccess = async () => {
try {
const result = await LoginManager.reauthorizeDataAccess();
this._alert("Result", result);
} catch (error) {
this._alert("Error", error);
}
};

_shareLinkWithShareDialog = async () => {
const canShow = await ShareDialog.canShow(SHARE_LINK_CONTENT);
if (canShow) {
Expand All @@ -57,8 +77,11 @@ export default class App extends Component<{}> {
Alert.alert(JSON.stringify(error || data, null, 2));
}}
/>
<TouchableHighlight onPress={this._reauthorizeDataAccess}>
<Text style={styles.buttonText}>Reauthorize Data Access</Text>
</TouchableHighlight>
<TouchableHighlight onPress={this._shareLinkWithShareDialog}>
<Text style={styles.shareText}>Share link with ShareDialog</Text>
<Text style={styles.buttonText}>Share link with ShareDialog</Text>
</TouchableHighlight>
</View>
);
Expand All @@ -72,7 +95,7 @@ const styles = StyleSheet.create({
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
shareText: {
buttonText: {
fontSize: 20,
margin: 10,
},
Expand Down
13 changes: 13 additions & 0 deletions ios/RCTFBSDK/login/RCTFBSDKLoginManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ + (BOOL)requiresMainQueueSetup
[_loginManager logInWithPermissions:permissions fromViewController:nil handler:requestHandler];
};

RCT_REMAP_METHOD(reauthorizeDataAccess, reauthorizeDataAccess_resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
FBSDKLoginManagerLoginResultBlock requestHandler = ^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
reject(@"FacebookSDK", @"Reauthorization Failed", error);
} else {
resolve(RCTBuildResultDictionary(result));
}
};

[_loginManager reauthorizeDataAccess:nil handler:requestHandler];
};

RCT_EXPORT_METHOD(logOut)
{
[_loginManager logOut];
Expand Down
7 changes: 7 additions & 0 deletions js/FBLoginManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ module.exports = {
LoginManager.setDefaultAudience(defaultAudience);
},

/**
* Re-authorizes the user to regain data access.
*/
reauthorizeDataAccess(): Promise<LoginResult> {
return LoginManager.reauthorizeDataAccess();
},

/**
* Logs out the user.
*/
Expand Down