Skip to content

Commit

Permalink
Send parameter to show/not show GameCenter login on initAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
Mads Rode committed Aug 10, 2020
1 parent 90454a5 commit 826c1bc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 26 deletions.
14 changes: 8 additions & 6 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { StyleSheet, View, Text, Button } from 'react-native';
import { GameCenterAuth, PlayGamesAuth } from 'react-native-game-auth';

export default function App() {
const [result, setResult] = React.useState<Boolean | undefined>();
const [result, setResult] = React.useState<Boolean | undefined>(undefined);
const [json, setJson] = React.useState<string | undefined>();

const psAuthStateChanged = (isSignedIn: boolean): void => {
Expand All @@ -23,7 +23,7 @@ export default function App() {
const x = GameCenterAuth.onAuthenticate((i) => {
setResult(i);
});
GameCenterAuth.initAuth();
GameCenterAuth.initAuth(false);
return () => {
x.remove();
};
Expand All @@ -34,16 +34,18 @@ export default function App() {

return (
<View style={styles.container}>
<Text>{result ? 'Authenticated' : 'Not authenticated'}</Text>
<Text>{result ? 'Authenticated' : 'Not authenticated ' + result}</Text>
<Button
title="getPlayer"
onPress={() => {
GameCenterAuth?.isAuthenticated().then((x) =>
console.log('isAuthen', x)
);
GameCenterAuth?.getPlayer()
.then((x) => setJson(JSON.stringify(x)))
.catch((x) => console.warn(x));
GameCenterAuth?.initAuth(true);
GameCenterAuth?.isAuthenticated();
// GameCenterAuth?.getPlayer()
// .then((x) => setJson(JSON.stringify(x)))
// .catch((x) => console.warn(x));
}}
/>
<Button
Expand Down
2 changes: 1 addition & 1 deletion ios/GameAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@interface RCT_EXTERN_MODULE(GameAuth, RCTEventEmitter)

RCT_EXTERN_METHOD(initAuth)
RCT_EXTERN_METHOD(initAuth: (BOOL)showUIIfUnauthenticated)
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
55 changes: 37 additions & 18 deletions ios/GameAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,52 @@ import GameKit

@objc(GameAuth)
class GameAuth: RCTEventEmitter {
let C_OnAuthenticated: String = "OnAuthenticate";

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

let C_OnAuthenticated: String = "OnAuthenticate";
var loginUI: UIViewController? = nil;

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

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

let player = GKLocalPlayer.local
player.authenticateHandler = { vc, error in
if(vc != nil) {
ui?.present(vc!, animated: true, completion: nil);
if(player.authenticateHandler == nil) {
player.authenticateHandler = { vc, error in
self.loginUI = vc;
if(vc != nil) {
if(showUIIfUnauthenticated) {
ui?.present(vc!, animated: true, completion: nil);
}
else {
self.sendEvent(withName: self.C_OnAuthenticated, body: ["isAuthenticated":false, "error": error])
}
}
else {
self.sendEvent(withName: self.C_OnAuthenticated, body: ["isAuthenticated":player.isAuthenticated, "error": error])
}
}
}
else {
if(player.isAuthenticated) {
self.sendEvent(withName: self.C_OnAuthenticated, body: ["isAuthenticated":player.isAuthenticated, "error": nil])
}
else {
self.sendEvent(withName: self.C_OnAuthenticated, body: ["isAuthenticated":player.isAuthenticated, "error": error])
if(showUIIfUnauthenticated && self.loginUI != nil) {
ui?.present(self.loginUI!, animated: true, completion: nil);
}
}
}
}
}

@objc(isAuthenticated:rejecter:)
func isAuthenticated(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock ) -> Void {
let player = GKLocalPlayer.local
resolve(player.isAuthenticated)
}

@objc(getPlayer:rejecter:)
func getPlayer(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock ) -> Void {
let player = GKLocalPlayer.local
Expand All @@ -44,7 +63,7 @@ class GameAuth: RCTEventEmitter {
reject("Unautheticated", "Unautheticated", nil)
}
}

@objc(getServerAuth:rejecter:)
func getServerAuth(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock ) -> Void {
let player = GKLocalPlayer.local
Expand All @@ -63,7 +82,7 @@ class GameAuth: RCTEventEmitter {
"salt" : salt?.base64EncodedString() ?? "",
"timestamp": timestamp
]

resolve(data);
}
}
Expand All @@ -72,7 +91,7 @@ class GameAuth: RCTEventEmitter {
reject("Unauthenticated", "", nil);
}
}

@objc static override func requiresMainQueueSetup() -> Bool {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ if (NativeModules.GameAuth) {
}

type GameCenterAuthType = {
initAuth(): void;
initAuth(showUIIfUnauthenticated: boolean): void;
isAuthenticated(): Promise<Boolean>;
getPlayer(): Promise<Player>;
getServerAuth(): Promise<IdentityVerificationSignature>;
Expand Down

0 comments on commit 826c1bc

Please sign in to comment.