From 051611f96cc4485942cef6d2bae7177f18e75e4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnay=20Mert=20Karado=C4=9Fan?= Date: Wed, 11 Mar 2020 13:45:15 +0000 Subject: [PATCH] Add reauthorizeDataAccess method to LoginManager --- .../androidsdk/FBLoginManagerModule.java | 14 +++++++++ example/App.js | 31 ++++++++++++++++--- ios/RCTFBSDK/login/RCTFBSDKLoginManager.m | 13 ++++++++ js/FBLoginManager.js | 7 +++++ 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/android/src/main/java/com/facebook/reactnative/androidsdk/FBLoginManagerModule.java b/android/src/main/java/com/facebook/reactnative/androidsdk/FBLoginManagerModule.java index ce6da614..d37a2b55 100644 --- a/android/src/main/java/com/facebook/reactnative/androidsdk/FBLoginManagerModule.java +++ b/android/src/main/java/com/facebook/reactnative/androidsdk/FBLoginManagerModule.java @@ -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 set) { WritableArray array = Arguments.createArray(); for (String e: set) { diff --git a/example/App.js b/example/App.js index 2bf406cf..f43f89fa 100644 --- a/example/App.js +++ b/example/App.js @@ -22,8 +22,15 @@ */ 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', @@ -31,6 +38,19 @@ const SHARE_LINK_CONTENT = { }; 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) { @@ -57,8 +77,11 @@ export default class App extends Component<{}> { Alert.alert(JSON.stringify(error || data, null, 2)); }} /> + + Reauthorize Data Access + - Share link with ShareDialog + Share link with ShareDialog ); @@ -72,7 +95,7 @@ const styles = StyleSheet.create({ alignItems: 'center', backgroundColor: '#F5FCFF', }, - shareText: { + buttonText: { fontSize: 20, margin: 10, }, diff --git a/ios/RCTFBSDK/login/RCTFBSDKLoginManager.m b/ios/RCTFBSDK/login/RCTFBSDKLoginManager.m index d05ed3cb..addf4409 100644 --- a/ios/RCTFBSDK/login/RCTFBSDKLoginManager.m +++ b/ios/RCTFBSDK/login/RCTFBSDKLoginManager.m @@ -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]; diff --git a/js/FBLoginManager.js b/js/FBLoginManager.js index 1287b445..eb91e580 100644 --- a/js/FBLoginManager.js +++ b/js/FBLoginManager.js @@ -96,6 +96,13 @@ module.exports = { LoginManager.setDefaultAudience(defaultAudience); }, + /** + * Re-authorizes the user to regain data access. + */ + reauthorizeDataAccess(): Promise { + return LoginManager.reauthorizeDataAccess(); + }, + /** * Logs out the user. */