Skip to content

Commit 4aa7ff6

Browse files
authored
feat: Add reauthorizeDataAccess method to LoginManager (#204)
1 parent 9506ac6 commit 4aa7ff6

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

RNFBSDKExample/App.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import React, {Component} from 'react';
2525
import {Alert, StyleSheet, Text, TouchableHighlight, View} from 'react-native';
26-
import {LoginButton, Settings, ShareDialog} from 'react-native-fbsdk-next';
26+
import {LoginButton, LoginManager, Settings, ShareDialog} from 'react-native-fbsdk-next';
2727

2828
const SHARE_LINK_CONTENT = {
2929
contentType: 'link',
@@ -35,6 +35,15 @@ const SHARE_LINK_CONTENT = {
3535
Settings.initializeSDK();
3636

3737
export default class App extends Component<{}> {
38+
_reauthorizeDataAccess = async () => {
39+
try {
40+
const result = await LoginManager.reauthorizeDataAccess();
41+
Alert.alert("Reauthorize data access result", JSON.stringify(result, null, 2));
42+
} catch (error) {
43+
Alert.alert("Reauthorize data access fail with error:", error);
44+
}
45+
};
46+
3847
_shareLinkWithShareDialog = async () => {
3948
const canShow = await ShareDialog.canShow(SHARE_LINK_CONTENT);
4049
if (canShow) {
@@ -63,7 +72,10 @@ export default class App extends Component<{}> {
6372
}}
6473
/>
6574
<TouchableHighlight onPress={this._shareLinkWithShareDialog}>
66-
<Text style={styles.shareText}>Share link with ShareDialog</Text>
75+
<Text style={styles.buttonText}>Share link with ShareDialog</Text>
76+
</TouchableHighlight>
77+
<TouchableHighlight onPress={this._reauthorizeDataAccess}>
78+
<Text style={styles.buttonText}>Reauthorize Data Access</Text>
6779
</TouchableHighlight>
6880
</View>
6981
);
@@ -77,7 +89,7 @@ const styles = StyleSheet.create({
7789
alignItems: 'center',
7890
backgroundColor: '#F5FCFF',
7991
},
80-
shareText: {
92+
buttonText: {
8193
fontSize: 20,
8294
margin: 10,
8395
},

android/src/main/java/com/facebook/reactnative/androidsdk/FBLoginManagerModule.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ public void logInWithPermissions(ReadableArray permissions, final Promise promis
146146
}
147147
}
148148

149+
/**
150+
* Attempts a re-authorization to regain data access.
151+
* @param promise Use promise to pass re-authorization result to JS after re-authorization finish.
152+
*/
153+
@ReactMethod
154+
public void reauthorizeDataAccess(final Promise promise) {
155+
final LoginManager loginManager = LoginManager.getInstance();
156+
loginManager.registerCallback(getCallbackManager(), new LoginManagerCallback(promise));
157+
Activity activity = getCurrentActivity();
158+
if (activity != null) {
159+
loginManager.reauthorizeDataAccess(activity);
160+
}
161+
}
162+
149163
private WritableArray setToWritableArray(Set<String> set) {
150164
WritableArray array = Arguments.createArray();
151165
for (String e: set) {

ios/RCTFBSDK/login/RCTFBSDKLoginManager.m

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ + (BOOL)requiresMainQueueSetup
7777
};
7878
FBSDKLoginConfiguration *configuration;
7979
FBSDKLoginTracking tracking = [loginTracking isEqualToString: @"limited"] ? FBSDKLoginTrackingLimited : FBSDKLoginTrackingEnabled;
80-
80+
8181
if ( ( ![nonce isEqual:[NSNull null]] ) && ( [nonce length] != 0 ) ) {
8282
configuration =
8383
[[FBSDKLoginConfiguration alloc] initWithPermissions:permissions
@@ -90,14 +90,26 @@ + (BOOL)requiresMainQueueSetup
9090
tracking: tracking
9191
];
9292
}
93-
94-
93+
9594
[_loginManager
9695
logInFromViewController: nil
9796
configuration:configuration
9897
completion:requestHandler];
9998
};
10099

100+
RCT_EXPORT_METHOD(reauthorizeDataAccess:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
101+
{
102+
FBSDKLoginManagerLoginResultBlock requestHandler = ^(FBSDKLoginManagerLoginResult *result, NSError *error) {
103+
if (error) {
104+
reject(@"FacebookSDK", @"Reauthorization Failed", error);
105+
} else {
106+
resolve(RCTBuildResultDictionary(result));
107+
}
108+
};
109+
110+
[_loginManager reauthorizeDataAccess:nil handler:requestHandler];
111+
};
112+
101113
RCT_EXPORT_METHOD(logOut)
102114
{
103115
[_loginManager logOut];

src/FBLoginManager.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ export default {
117117
LoginManager.setDefaultAudience(defaultAudience);
118118
},
119119

120+
/**
121+
* Re-authorizes the user to update data access permissions.
122+
*/
123+
reauthorizeDataAccess(): Promise<LoginResult> {
124+
return LoginManager.reauthorizeDataAccess();
125+
},
126+
120127
/**
121128
* Logs out the user.
122129
*/

0 commit comments

Comments
 (0)