Skip to content

Commit

Permalink
Change authenticateUser to initAuth. Use Events instead of Promise to…
Browse files Browse the repository at this point in the history
… accept multiple calls.
  • Loading branch information
Mads Rode committed Aug 7, 2020
1 parent 41ec0d7 commit 3f41331
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 26 deletions.
13 changes: 10 additions & 3 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ export default function App() {
}

if (GameCenterAuth) {
GameCenterAuth.authenticateUser()
.then((x) => setResult(x))
.catch((e) => console.log('e', JSON.stringify(e)));
const x = GameCenterAuth.onAuthenticate((i) => {
setResult(i);
});
GameCenterAuth.initAuth();

return () => {
x.remove();
};
}

return () => {};
}, []);

return (
Expand Down
1 change: 1 addition & 0 deletions ios/GameAuth-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
#import <GameKit/GameKit.h>
5 changes: 3 additions & 2 deletions ios/GameAuth.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
#import <GameKit/GameKit.h>

@interface RCT_EXTERN_MODULE(GameAuth, NSObject)
@interface RCT_EXTERN_MODULE(GameAuth, RCTEventEmitter)

RCT_EXTERN_METHOD(authenticateUser: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(initAuth)
RCT_EXTERN_METHOD(isAuthenticated: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(getPlayer: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(getServerAuth: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
Expand Down
26 changes: 14 additions & 12 deletions ios/GameAuth.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import GameKit

@objc(GameAuth)
class GameAuth: NSObject {
@objc(authenticateUser:rejecter:)
func authenticateUser(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock ) -> Void {
class GameAuth: RCTEventEmitter {
let C_OnAuthenticated: String = "OnAuthenticate";

open override func supportedEvents() -> [String] {
return [self.C_OnAuthenticated];
}

@objc
func initAuth() {
let ui = UIApplication.shared.keyWindow?.rootViewController;

let player = GKLocalPlayer.local
Expand All @@ -12,13 +18,9 @@ class GameAuth: NSObject {
ui?.present(vc!, animated: true, completion: nil);
}
else {
if(error == nil) {
resolve(player.isAuthenticated)
} else {
reject(error?.localizedDescription ?? "", "", error)
}
self.sendEvent(withName: self.C_OnAuthenticated, body: ["isAuthenticated":player.isAuthenticated, "error": error])
}
}
}
}

@objc(isAutheticated:rejecter:)
Expand Down Expand Up @@ -71,7 +73,7 @@ class GameAuth: NSObject {
}
}

static func requiresMainQueueSetup() -> Bool {
return true
}
@objc static override func requiresMainQueueSetup() -> Bool {
return true
}
}
38 changes: 29 additions & 9 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { NativeModules, DeviceEventEmitter } from 'react-native';
import {
NativeModules,
DeviceEventEmitter,
NativeEventEmitter,
EmitterSubscription,
} from 'react-native';

interface Player {
alias: String;
Expand All @@ -13,13 +18,6 @@ export interface IdentityVerificationSignature {
timestamp: Number;
}

type GameCenterAuthType = {
authenticateUser(): Promise<Boolean>;
isAuthenticated(): Promise<Boolean>;
getPlayer(): Promise<Player>;
getServerAuth(): Promise<IdentityVerificationSignature>;
};

type PlayGamesAuthType = {
signIn(): Promise<boolean>;
signInSilent(triggerUISignInIfSilentFails: boolean): Promise<boolean>;
Expand All @@ -30,7 +28,6 @@ type PlayGamesAuthType = {
AUTH_TOKEN_CHANGED_EVENT: string;
};

const GameCenterAuth: GameCenterAuthType | undefined = NativeModules.GameAuth;
const PlayGamesAuth: PlayGamesAuthType | undefined =
NativeModules.PlayGamesAuth;

Expand All @@ -54,4 +51,27 @@ if (PlayGamesAuth) {
};
}

if (NativeModules.GameAuth) {
NativeModules.GameAuth.onAuthenticate = (
callback: (isAuthenticated: boolean) => void
): EmitterSubscription => {
const e = new NativeEventEmitter(NativeModules.GameAuth);
return e.addListener('OnAuthenticate', (data: any) => {
callback(data.isAuthenticated);
});
};
}

type GameCenterAuthType = {
initAuth(): void;
isAuthenticated(): Promise<Boolean>;
getPlayer(): Promise<Player>;
getServerAuth(): Promise<IdentityVerificationSignature>;
onAuthenticate(
callback: (isAuthenticated: boolean) => void
): EmitterSubscription;
};

const GameCenterAuth: GameCenterAuthType | undefined = NativeModules.GameAuth;

export { GameCenterAuth, PlayGamesAuth };

0 comments on commit 3f41331

Please sign in to comment.