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

Commit

Permalink
Add method to listen for changes to the access token (#666)
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-oakes authored and janicduplessis committed Nov 7, 2019
1 parent 5d016be commit 26278c9
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package com.facebook.reactnative.androidsdk;

import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.FacebookException;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.NativeModule;
Expand All @@ -31,6 +32,7 @@
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.modules.core.DeviceEventManagerModule;

/**
* This is a {@link NativeModule} that allows JS to use AcessToken in Facebook Android SDK.
Expand All @@ -39,9 +41,21 @@
public class FBAccessTokenModule extends ReactContextBaseJavaModule {

public static final String NAME = "FBAccessToken";
public static final String CHANGE_EVENT_NAME = "fbsdk.accessTokenDidChange";

private final ReactApplicationContext mReactContext;
private final AccessTokenTracker accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
mReactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(CHANGE_EVENT_NAME, currentAccessToken == null ? null : Utility.accessTokenToReactMap(currentAccessToken));
}
};

public FBAccessTokenModule(ReactApplicationContext reactContext) {
super(reactContext);
mReactContext = reactContext;
}

public String getName() {
Expand Down
4 changes: 2 additions & 2 deletions ios/RCTFBSDK/core/RCTFBSDKAccessToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>

#import <FBSDKCoreKit/FBSDKCoreKit.h>

@interface RCTFBSDKAccessToken : NSObject <RCTBridgeModule>
@interface RCTFBSDKAccessToken : RCTEventEmitter
@end
38 changes: 37 additions & 1 deletion ios/RCTFBSDK/core/RCTFBSDKAccessToken.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@

#import "RCTConvert+FBSDKAccessToken.h"

@implementation RCTFBSDKAccessToken
static NSString *const kFBSDKAccessTokenDidChangeEvent = @"fbsdk.accessTokenDidChange";

@implementation RCTFBSDKAccessToken {
BOOL _isObserving;
}

RCT_EXPORT_MODULE(FBAccessToken);

Expand Down Expand Up @@ -56,6 +60,38 @@ - (dispatch_queue_t)methodQueue
}];
}

#pragma mark - Event Methods

- (NSArray *)supportedEvents
{
return @[kFBSDKAccessTokenDidChangeEvent];
}

- (void)startObserving
{
_isObserving = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(accessTokenDidChange:)
name:FBSDKAccessTokenDidChangeNotification
object:nil];
}

- (void)stopObserving
{
_isObserving = NO;
[[NSNotificationCenter defaultCenter] removeObserver:self
name:FBSDKAccessTokenDidChangeNotification
object:nil];
}

- (void)accessTokenDidChange:(NSNotification*)notification
{
if ([notification.name isEqualToString:FBSDKAccessTokenDidChangeNotification] && _isObserving) {
NSDictionary *body = RCTBuildAccessTokenDict([FBSDKAccessToken currentAccessToken]);
[self sendEventWithName:kFBSDKAccessTokenDidChangeEvent body:body ?: [NSNull null]];
}
}

#pragma mark - Helper Functions

static NSDictionary *RCTBuildAccessTokenDict(FBSDKAccessToken *token)
Expand Down
23 changes: 23 additions & 0 deletions js/FBAccessToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

const AccessToken = require('react-native').NativeModules.FBAccessToken;

const NativeEventEmitter = require('react-native').NativeEventEmitter;
const eventEmitter = new NativeEventEmitter(AccessToken);

type AccessTokenMap = {
accessToken: string,
permissions: Array<string>,
Expand Down Expand Up @@ -140,6 +143,26 @@ class FBAccessToken {
return AccessToken.refreshCurrentAccessTokenAsync();
}

/**
* Adds a listener for when the access token changes. Returns a functions which removes the
* listener when called.
*/
static addListener(
listener: (accessToken: ?FBAccessToken) => void,
): () => void {
const subscription = eventEmitter.addListener(
'fbsdk.accessTokenDidChange',
(tokenMap: AccessTokenMap) => {
if (tokenMap) {
listener(new FBAccessToken(tokenMap));
} else {
listener(null);
}
},
);
return () => subscription.remove();
}

/**
* Gets the date at which the access token expires. The value is the number of
* milliseconds since Jan. 1, 1970, midnight GMT.
Expand Down

0 comments on commit 26278c9

Please sign in to comment.