Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add reauthorizeDataAccess method to LoginManager #204

Merged
merged 3 commits into from
Feb 21, 2022
Merged
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
18 changes: 15 additions & 3 deletions RNFBSDKExample/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

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

const SHARE_LINK_CONTENT = {
contentType: 'link',
Expand All @@ -35,6 +35,15 @@ const SHARE_LINK_CONTENT = {
Settings.initializeSDK();

export default class App extends Component<{}> {
_reauthorizeDataAccess = async () => {
try {
const result = await LoginManager.reauthorizeDataAccess();
Alert.alert("Reauthorize data access result", JSON.stringify(result, null, 2));
} catch (error) {
Alert.alert("Reauthorize data access fail with error:", error);
}
};

_shareLinkWithShareDialog = async () => {
const canShow = await ShareDialog.canShow(SHARE_LINK_CONTENT);
if (canShow) {
Expand Down Expand Up @@ -63,7 +72,10 @@ export default class App extends Component<{}> {
}}
/>
<TouchableHighlight onPress={this._shareLinkWithShareDialog}>
<Text style={styles.shareText}>Share link with ShareDialog</Text>
<Text style={styles.buttonText}>Share link with ShareDialog</Text>
</TouchableHighlight>
<TouchableHighlight onPress={this._reauthorizeDataAccess}>
<Text style={styles.buttonText}>Reauthorize Data Access</Text>
</TouchableHighlight>
</View>
);
Expand All @@ -77,7 +89,7 @@ const styles = StyleSheet.create({
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
shareText: {
buttonText: {
neilco marked this conversation as resolved.
Show resolved Hide resolved
fontSize: 20,
margin: 10,
},
Expand Down
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
18 changes: 15 additions & 3 deletions ios/RCTFBSDK/login/RCTFBSDKLoginManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ + (BOOL)requiresMainQueueSetup
};
FBSDKLoginConfiguration *configuration;
FBSDKLoginTracking tracking = [loginTracking isEqualToString: @"limited"] ? FBSDKLoginTrackingLimited : FBSDKLoginTrackingEnabled;

if ( ( ![nonce isEqual:[NSNull null]] ) && ( [nonce length] != 0 ) ) {
configuration =
[[FBSDKLoginConfiguration alloc] initWithPermissions:permissions
Expand All @@ -90,14 +90,26 @@ + (BOOL)requiresMainQueueSetup
tracking: tracking
];
}



[_loginManager
logInFromViewController: nil
configuration:configuration
completion:requestHandler];
};

RCT_EXPORT_METHOD(reauthorizeDataAccess:(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 src/FBLoginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ export default {
LoginManager.setDefaultAudience(defaultAudience);
},

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

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