ตรวจสอบสิทธิ์โดยใช้ Game Center

คุณสามารถใช้ Game Center เพื่อให้ผู้เล่นลงชื่อเข้าใช้เกมบนแพลตฟอร์ม Apple ที่สร้างขึ้นบน Firebase ได้ หากต้องการใช้การลงชื่อเข้าใช้ Game Center กับ Firebase ก่อนอื่นให้ตรวจสอบว่าผู้เล่นในพื้นที่ลงชื่อเข้าใช้ด้วย Game Center แล้ว จากนั้นใช้ออบเจ็กต์ GameCenterAuthProvider เพื่อสร้างข้อมูลเข้าสู่ระบบ Firebase ซึ่งคุณสามารถใช้เพื่อตรวจสอบสิทธิ์กับ Firebase ได้

ก่อนเริ่มต้น

ใช้ Swift Package Manager เพื่อติดตั้งและจัดการทรัพยากร Dependency ของ 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 จะเริ่มจับคู่ข้อมูลและดาวน์โหลด ทรัพยากร Dependency ในเบื้องหลังโดยอัตโนมัติ

จากนั้นทำตามขั้นตอนการกำหนดค่าบางอย่างดังนี้

  1. ตรวจสอบว่าคุณได้ลงทะเบียนแอป Apple กับ Firebase แล้ว ซึ่งหมายถึงการป้อนรหัสชุดของแอปในส่วนการลงทะเบียนพร้อมกับข้อมูลเพิ่มเติมที่ไม่บังคับ เช่น รหัส App Store และรหัสทีม เป็นต้น ระบบจะกำหนดให้ระบุข้อมูลนี้เพื่อยืนยันกลุ่มเป้าหมายของข้อมูลเข้าสู่ระบบ Game Center ของผู้ใช้ก่อนที่จะลงชื่อเข้าใช้ให้เสร็จสมบูรณ์อย่างปลอดภัย
  2. เปิดใช้ Game Center เป็นผู้ให้บริการการลงชื่อเข้าใช้สำหรับโปรเจ็กต์ Firebase โดยทำดังนี้
    1. ในคอนโซล Firebase ให้ไปที่ ความปลอดภัย > การตรวจสอบสิทธิ์
    2. ในแท็บวิธีการลงชื่อเข้าใช้ ให้เปิดใช้ผู้ให้บริการการลงชื่อเข้าใช้Game Center

ผสานรวมการลงชื่อเข้าใช้ Game Center เข้ากับเกม

ก่อนอื่น หากเกมของคุณยังไม่ได้ใช้ Game Center ให้ทำตามวิธีการในหัวข้อ Incorporating Game Center into Your Game และ Authenticating a Local Player on the Device ในเว็บไซต์ของนักพัฒนาแอปเปิล

ตรวจสอบว่ารหัสชุดที่ระบุใน iTunes Connect ตรงกับรหัสชุดที่ใช้เมื่อเชื่อมต่อแอปเข้ากับโปรเจ็กต์ Firebase

คุณกำหนดตัวแฮนเดิลการตรวจสอบสิทธิ์ที่จะเรียกใช้ในหลายจุดในกระบวนการตรวจสอบสิทธิ์ Game Center ซึ่งเป็นส่วนหนึ่งของการผสานรวม Game Center ในตัวแฮนเดิลนี้ ให้ตรวจสอบว่าผู้เล่นลงชื่อเข้าใช้ด้วย 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 และ 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;
}