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

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

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

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