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

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

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

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

ตรวจสอบว่ารหัสชุดที่คุณระบุใน 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.
    }];
  }
}];

ขั้นตอนถัดไป

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

คุณจะได้รับรหัสผู้ใช้ที่ไม่ซ้ำของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร auth และใช้รหัสดังกล่าวเพื่อควบคุมข้อมูลที่ผู้ใช้เข้าถึงได้ในกฎการรักษาความปลอดภัยของ Firebase และ Cloud Storage

หากต้องการรับข้อมูลผู้เล่น 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;
}