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

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

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

ใช้ Swift Package Manager เพื่อติดตั้งและจัดการทรัพยากร Dependency ของ Firebase

  1. เปิดโปรเจ็กต์แอปใน Xcode แล้วไปที่ไฟล์ > เพิ่มแพ็กเกจ
  2. เมื่อได้รับข้อความแจ้ง ให้เพิ่มที่เก็บ SDK สำหรับแพลตฟอร์ม Firebase ของ Apple ดังนี้
  3.   https://github.com/firebase/firebase-ios-sdk.git
  4. เลือกไลบรารีการตรวจสอบสิทธิ์ Firebase
  5. เพิ่มแฟล็ก -ObjC ลงในส่วนแฟล็ก Linker อื่นๆ ของการตั้งค่าบิลด์ของเป้าหมาย
  6. เมื่อเสร็จแล้ว Xcode จะเริ่มแก้ปัญหาและดาวน์โหลด ทรัพยากร Dependency ในเบื้องหลัง

จากนั้น ให้ทำตามขั้นตอนการกำหนดค่าต่อไปนี้

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

ผสานรวมการลงชื่อเข้าใช้ Game Center ไว้ในเกมของคุณ

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

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

ในการผสานรวม 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 ซึ่งสามารถใช้เพื่อระบุผู้ใช้ในทุกแอปในบัญชีของคุณ

ในเกม คุณจะรับ Firebase UID ของผู้ใช้ได้จากออบเจ็กต์ 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;
}