From fe9184f9164f048786dbb50ee40611fc8536bcf0 Mon Sep 17 00:00:00 2001 From: pragatimodi <110490169+pragatimodi@users.noreply.github.com> Date: Mon, 6 May 2024 13:44:47 -0700 Subject: [PATCH] Game center auth (#12359) --- .../Auth Provider Icons/Contents.json | 6 +-- .../Game Center.imageset/Contents.json | 21 +++++++++ .../Game Center.imageset/gamecontroller.png | Bin 0 -> 764 bytes .../AuthenticationExample.entitlements | 2 + .../Models/AuthMenu.swift | 7 ++- .../AccountLinkingViewController.swift | 33 +++++++++++++++ .../ViewControllers/AuthViewController.swift | 40 ++++++++++++++++++ .../AuthenticationExampleUITests.swift | 5 --- 8 files changed, 105 insertions(+), 9 deletions(-) create mode 100644 FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Assets.xcassets/Auth Provider Icons/Game Center.imageset/Contents.json create mode 100644 FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Assets.xcassets/Auth Provider Icons/Game Center.imageset/gamecontroller.png diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Assets.xcassets/Auth Provider Icons/Contents.json b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Assets.xcassets/Auth Provider Icons/Contents.json index da4a164c918..73c00596a7f 100644 --- a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Assets.xcassets/Auth Provider Icons/Contents.json +++ b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Assets.xcassets/Auth Provider Icons/Contents.json @@ -1,6 +1,6 @@ { "info" : { - "version" : 1, - "author" : "xcode" + "author" : "xcode", + "version" : 1 } -} \ No newline at end of file +} diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Assets.xcassets/Auth Provider Icons/Game Center.imageset/Contents.json b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Assets.xcassets/Auth Provider Icons/Game Center.imageset/Contents.json new file mode 100644 index 00000000000..8cd53c205c6 --- /dev/null +++ b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Assets.xcassets/Auth Provider Icons/Game Center.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "filename" : "gamecontroller.png", + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Assets.xcassets/Auth Provider Icons/Game Center.imageset/gamecontroller.png b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Assets.xcassets/Auth Provider Icons/Game Center.imageset/gamecontroller.png new file mode 100644 index 0000000000000000000000000000000000000000..14b182a9c457c53d62bc0d28c34d4a7cda3a9a10 GIT binary patch literal 764 zcmeAS@N?(olHy`uVBq!ia0vp^vOp}v!3HFkAH8}DNHG=%xjQkeJ16rJ$Z<)H@J#dd zWzYh$IT#q*GZ|PwN`P1jh#44|7cep~18GK(*a9ZFtn312I9mv$Fh;wvfq{W3&C|s( z#DjP4)aZ`hK#}9UlDs?n3O(~?&V1&|>B?z(l-Wzu>*AE6nHsLVn*MP%8X|F1*w3zZ z37q2XbLuVY&ixA0#fvO?}=BVw{ zWN0ANc0gf8v-}5MyMxsgyyqWi*0gUbN?7-iWmlpg&snMckGi}6GKtIc99B5>O?rd8 z+)=i-HG!KgSc4xZ-C?``OLR}GSN{8TCWl=znrCicxx!Tu86jG?O`x7BP14lH>hG55 z7v=1H;jiwzEf2eSkMY`1o+nj~4=jBeUN5+J zAhlp!?FY$UCfui^%D%e(yLKqz+{6p+#q4ufo)mYp}P zP8?n;f3;3%b`fM?c!yu}RL}n{ocHK>5OR2iYxTrJLSLoHk@$ zw(JKJ&;7Dgt`vX&uWBov-deKKCS~15Lspw>d2jdorN4@Q{aJ{)TIS8QbB)yx!k&H# zn32jI(UtYfckxq8uJ@-(_ITxpn(TOyV!^$0&usPt!yk^{tD}6s<+oLCklN>Pg;{3( zU5n=LiOb!Vz5Q|RpwI8sd{$9fo6nXX+x@C0H=yQ$ZR5wf$`x-mi2nnod(=sl(`1!#c@dR6ung1ELU$6M8x5qUPl=3`X{an^LB{Ts55cElj literal 0 HcmV?d00001 diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/AuthenticationExample.entitlements b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/AuthenticationExample.entitlements index 2402d248da7..075352dfe1b 100644 --- a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/AuthenticationExample.entitlements +++ b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/AuthenticationExample.entitlements @@ -10,6 +10,8 @@ com.apple.developer.associated-domains + com.apple.developer.game-center + keychain-access-groups $(AppIdentifierPrefix)com.google.firebase.auth.keychainGroup1 diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Models/AuthMenu.swift b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Models/AuthMenu.swift index b0abf158c96..711c60a0680 100644 --- a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Models/AuthMenu.swift +++ b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Models/AuthMenu.swift @@ -24,6 +24,7 @@ enum AuthMenu: String { case gitHub = "github.com" case yahoo = "yahoo.com" case facebook = "facebook.com" + case gameCenter = "gc.apple.com" case emailPassword = "password" case passwordless = "emailLink" case phoneNumber = "phone" @@ -54,6 +55,8 @@ enum AuthMenu: String { return "Yahoo" case .facebook: return "Facebook" + case .gameCenter: + return "Game Center" case .emailPassword: return "Email & Password Login" case .passwordless: @@ -91,6 +94,8 @@ enum AuthMenu: String { self = .yahoo case "Facebook": self = .facebook + case "Game Center": + self = .gameCenter case "Email & Password Login": self = .emailPassword case "Email Link/Passwordless": @@ -114,7 +119,7 @@ enum AuthMenu: String { extension AuthMenu: DataSourceProvidable { private static var providers: [AuthMenu] { - [.google, .apple, .twitter, .microsoft, .gitHub, .yahoo, .facebook] + [.google, .apple, .twitter, .microsoft, .gitHub, .yahoo, .facebook, .gameCenter] } static var settingsSection: Section { diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/AccountLinkingViewController.swift b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/AccountLinkingViewController.swift index 05f59f0c2b8..410c7adc624 100644 --- a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/AccountLinkingViewController.swift +++ b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/AccountLinkingViewController.swift @@ -14,6 +14,7 @@ import FirebaseAuth import FirebaseCore +import GameKit import UIKit // For Account Linking with Sign in with Google. @@ -93,6 +94,9 @@ class AccountLinkingViewController: UIViewController, DataSourceProviderDelegate case .twitter, .microsoft, .gitHub, .yahoo: performOAuthAccountLink(for: provider) + case .gameCenter: + performGameCenterAccountLink() + case .emailPassword: performEmailPasswordAccountLink() @@ -233,6 +237,35 @@ class AccountLinkingViewController: UIViewController, DataSourceProviderDelegate } } + private func performGameCenterAccountLink() { + // Step 1: Ensure Game Center Authentication + guard GKLocalPlayer.local.isAuthenticated else { + print("Error: Player not authenticated with Game Center.") + return + } + + // Step 2: Get Game Center Credential for Linking + GameCenterAuthProvider.getCredential { credential, error in + if let error = error { + print("Error getting Game Center credential: \(error.localizedDescription)") + return + } + + guard let credential = credential else { + print("Error: Missing Game Center credential") + return + } + + // Step 3: Link Credential with Current Firebase User + Auth.auth().currentUser?.link(with: credential) { authResult, error in + if let error = error { + print("Error linking Game Center to Firebase: \(error.localizedDescription)") + return + } + } + } + } + // MARK: - Email & Password Login Account Linking 🔥 private func performEmailPasswordAccountLink() { diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/AuthViewController.swift b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/AuthViewController.swift index 4cc7d4597f0..20a00d3b086 100644 --- a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/AuthViewController.swift +++ b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/AuthViewController.swift @@ -18,6 +18,7 @@ import FirebaseAuth // [START auth_import] import FirebaseCore +import GameKit // For Sign in with Google // [START google_import] @@ -71,6 +72,9 @@ class AuthViewController: UIViewController, DataSourceProviderDelegate { case .twitter, .microsoft, .gitHub, .yahoo: performOAuthLoginFlow(for: provider) + case .gameCenter: + performGameCenterLoginFlow() + case .emailPassword: performDemoEmailPasswordLoginFlow() @@ -210,6 +214,42 @@ class AuthViewController: UIViewController, DataSourceProviderDelegate { } } + private func performGameCenterLoginFlow() { + // Step 1: System Game Center Login + GKLocalPlayer.local.authenticateHandler = { viewController, error in + if let error = error { + // Handle Game Center login error + print("Error logging into Game Center: \(error.localizedDescription)") + } else if let authViewController = viewController { + // Present Game Center login UI if needed + self.present(authViewController, animated: true) + } else { + // Game Center login successful, proceed to Firebase + self.linkGameCenterToFirebase() + } + } + } + + // Step 2: Link to Firebase + private func linkGameCenterToFirebase() { + GameCenterAuthProvider.getCredential { credential, error in + if let error = error { + // Handle Firebase credential retrieval error + print("Error getting Game Center credential: \(error.localizedDescription)") + } else if let credential = credential { + Auth.auth().signIn(with: credential) { authResult, error in + if let error = error { + // Handle Firebase sign-in error + print("Error signing into Firebase with Game Center: \(error.localizedDescription)") + } else { + // Firebase sign-in successful + print("Successfully linked Game Center to Firebase") + } + } + } + } + } + private func performDemoEmailPasswordLoginFlow() { let loginController = LoginController() loginController.delegate = self diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExampleUITests/AuthenticationExampleUITests.swift b/FirebaseAuth/Tests/SampleSwift/AuthenticationExampleUITests/AuthenticationExampleUITests.swift index 1dc9bddd718..c70795dc548 100644 --- a/FirebaseAuth/Tests/SampleSwift/AuthenticationExampleUITests/AuthenticationExampleUITests.swift +++ b/FirebaseAuth/Tests/SampleSwift/AuthenticationExampleUITests/AuthenticationExampleUITests.swift @@ -36,11 +36,6 @@ class AuthenticationExampleUITests: XCTestCase { XCTAssertTrue(app.navigationBars["Firebase Auth"].exists) } - func testAuthOptions() { - // There are 15 sign in methods, each with its own cell - XCTAssertEqual(app.tables.cells.count, 15) - } - func testAuthAnonymously() { app.staticTexts["Anonymous Authentication"].tap()