Skip to content

Commit

Permalink
Game center auth (#12359)
Browse files Browse the repository at this point in the history
  • Loading branch information
pragatimodi authored May 6, 2024
1 parent 8894b50 commit fe9184f
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "xcode",
"version" : 1
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
</array>
<key>com.apple.developer.associated-domains</key>
<array/>
<key>com.apple.developer.game-center</key>
<true/>
<key>keychain-access-groups</key>
<array>
<string>$(AppIdentifierPrefix)com.google.firebase.auth.keychainGroup1</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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":
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import FirebaseAuth
import FirebaseCore
import GameKit
import UIKit

// For Account Linking with Sign in with Google.
Expand Down Expand Up @@ -93,6 +94,9 @@ class AccountLinkingViewController: UIViewController, DataSourceProviderDelegate
case .twitter, .microsoft, .gitHub, .yahoo:
performOAuthAccountLink(for: provider)

case .gameCenter:
performGameCenterAccountLink()

case .emailPassword:
performEmailPasswordAccountLink()

Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import FirebaseAuth

// [START auth_import]
import FirebaseCore
import GameKit

// For Sign in with Google
// [START google_import]
Expand Down Expand Up @@ -71,6 +72,9 @@ class AuthViewController: UIViewController, DataSourceProviderDelegate {
case .twitter, .microsoft, .gitHub, .yahoo:
performOAuthLoginFlow(for: provider)

case .gameCenter:
performGameCenterLoginFlow()

case .emailPassword:
performDemoEmailPasswordLoginFlow()

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit fe9184f

Please sign in to comment.