אימות באמצעות מרכז המשחקים

אפשר להשתמש ב-Game Center כדי להחתים שחקנים במשחק לפלטפורמות של אפל שנבנה על Firebase. כדי להשתמש בכניסה לחשבון Game Center עם Firebase, קודם צריך לוודא שהשחקן המקומי מחובר לחשבון Game Center, ואז להשתמש באובייקט GameCenterAuthProvider כדי ליצור אישור Firebase, שאפשר להשתמש בו לאימות ב-Firebase.

לפני שמתחילים

משתמשים ב-Swift Package Manager כדי להתקין ולנהל יחסי תלות ב-Firebase.

  1. ב-Xcode, כשהפרויקט של האפליקציה פתוח, עוברים אל File > Add Packages (קובץ > הוספת חבילות).
  2. כשמוצגת בקשה, מוסיפים את מאגר Firebase Apple platforms SDK:
  3.   https://github.com/firebase/firebase-ios-sdk.git
  4. בוחרים את הספרייה Firebase Authentication.
  5. מוסיפים את הדגל -ObjC לקטע Other Linker Flags בהגדרות הבנייה של יעד הקישור.
  6. אחרי שתסיימו, פלטפורמת Xcode תתחיל באופן אוטומטי לטפל ביחסי התלות ולהוריד אותם ברקע.

לאחר מכן, מבצעים כמה שלבי הגדרה:

  1. חשוב לוודא שרשמתם את אפליקציית Apple ב-Firebase. כלומר, צריך להזין את מזהה החבילה של האפליקציה בקטע ההרשמה, יחד עם מידע אופציונלי נוסף כמו מזהה App Store ומזהה צוות וכו'. הפעולה הזו נדרשת כדי לאמת בצורה מאובטחת את קהל היעד של פרטי הכניסה של המשתמש ל-Game Center לפני השלמת הכניסה.
  2. מפעילים את Game Center כספק כניסה לפרויקט Firebase:
    1. במסוף Firebase, פותחים את הקטע אימות.
    2. בכרטיסייה שיטת הכניסה, מפעילים את ספק הכניסה Game Center.

שילוב של כניסה למרכז המשחקים במשחק

קודם כל, אם המשחק שלכם עדיין לא משתמש ב-Game Center, פועלים לפי ההוראות במאמרים Incorporating Game Center into Your Game ו-Authenticating a Local Player on the Device באתר למפתחים של Apple.

חשוב לוודא שמזהה החבילה שציינתם ב-iTunes Connect זהה למזהה החבילה שבו השתמשתם כשקישרתם את האפליקציה לפרויקט Firebase.

כחלק מהשילוב של Game Center, מגדירים handler לאימות שמופעל בכמה נקודות בתהליך האימות של Game Center. ב-handler הזה, בודקים אם השחקן מחובר ל-Game Center. אם כן, אפשר להמשיך להיכנס ל-Firebase.

Swift

let localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = { (gcAuthViewController?, error) in
  if let gcAuthViewController = gcAuthViewController {
    // Pause any activities that require user interaction, then present the
    // gcAuthViewController to the player.
  } else if localPlayer.isAuthenticated {
    // Player is signed in to Game Center. Get Firebase credentials from the
    // player's Game Center credentials (see below).
  } else {
    // Error
  }
}

Objective-C

__weak GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *gcAuthViewController,
                                    NSError *error) {
  if (gcAuthViewController != nil) {
    // Pause any activities that require user interaction, then present the
    // gcAuthViewController to the player.
  } else if (localPlayer.isAuthenticated) {
    // Player is signed in to Game Center. Get Firebase credentials from the
    // player's Game Center credentials (see below).
  } else {
    // Error
  }
};

אימות באמצעות Firebase

אחרי שקובעים שהשחקן המקומי נכנס באמצעות Game Center, צריך להכניס את השחקן למשחק על ידי יצירת אובייקט AuthCredential עם GameCenterAuthProvider.getCredential() והעברת האובייקט הזה אל signIn(with:):

Swift

// Get Firebase credentials from the player's Game Center credentials
GameCenterAuthProvider.getCredential() { (credential, error) in
  if let error = error {
    return
  }
  // The credential can be used to sign in, or re-auth, or link or unlink.
  Auth.auth().signIn(with:credential) { (user, error) in
    if let error = error {
      return
    }
    // Player is signed in!
  }

Objective-C

// Get Firebase credentials from the player's Game Center credentials
[FIRGameCenterAuthProvider getCredentialWithCompletion:^(FIRAuthCredential *credential,
                                                         NSError *error) {
  // The credential can be used to sign in, or re-auth, or link or unlink.
  if (error == nil) {
    [[FIRAuth auth] signInWithCredential:credential
                              completion:^(FIRUser *user, NSError *error) {
      // If error is nil, player is signed in.
    }];
  }
}];

השלבים הבאים

אחרי שהמשתמש נכנס לחשבון בפעם הראשונה, נוצר חשבון משתמש חדש שמקושר למזהה שלו ב-Game Center. החשבון החדש הזה נשמר כחלק מפרויקט Firebase, ואפשר להשתמש בו כדי לזהות משתמש בכל האפליקציות בפרויקט.

במשחק, אפשר לקבל את ה-UID של המשתמש ב-Firebase מאובייקט User:

Swift

let user = Auth.auth().currentUser
if let user = user {
  let playerName = user.displayName

  // The user's ID, unique to the Firebase project.
  // Do NOT use this value to authenticate with your backend server,
  // if you have one. Use getToken(with:) instead.
  let uid = user.uid
}

Objective-C

FIRUser *user = [FIRAuth auth].currentUser;
if (user) {
  NSString *playerName = user.displayName;

  // The user's ID, unique to the Firebase project.
  // Do NOT use this value to authenticate with your backend server,
  // if you have one. Use getTokenWithCompletion:completion: instead.
  NSString *uid = user.uid;
}

בכללי האבטחה של Firebase Realtime Database ו-Cloud Storage, אפשר לקבל את מזהה המשתמש הייחודי של המשתמש המחובר מהמשתנה auth ולהשתמש בו כדי לקבוע לאילו נתונים המשתמש יכול לגשת.

כדי לקבל מידע על שחקן ב-Game Center או לגשת לשירותים של Game Center, צריך להשתמש בממשקי ה-API שזמינים ב-Game Kit.

כדי להוציא משתמש מ-Firebase, קוראים ל-Auth.signOut():

Swift

let firebaseAuth = Auth.auth()
do {
  try firebaseAuth.signOut()
} catch let signOutError as NSError {
  print ("Error signing out: %@", signOutError)
}

Objective-C

NSError *signOutError;
BOOL status = [[FIRAuth auth] signOut:&signOutError];
if (!status) {
  NSLog(@"Error signing out: %@", signOutError);
  return;
}