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

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

ก่อนที่คุณจะเริ่ม

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

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

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

  1. ตรวจสอบให้แน่ใจว่าคุณลงทะเบียนแอป Apple กับ Firebase นี่หมายถึงการป้อน ID ชุดของแอปของคุณในส่วนการลงทะเบียนพร้อมกับข้อมูลเสริมเพิ่มเติม เช่น App Store ID และ Team ID ฯลฯ ซึ่งจำเป็นสำหรับการตรวจสอบผู้ชมข้อมูลรับรอง 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 ต่อไปได้

สวิฟท์

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
  }
}

วัตถุประสงค์-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:) :

สวิฟท์

// 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!
  }

วัตถุประสงค์-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 ID บัญชีใหม่นี้จัดเก็บไว้เป็นส่วนหนึ่งของโปรเจ็กต์ Firebase ของคุณ และสามารถใช้เพื่อระบุผู้ใช้ในทุกแอปในโปรเจ็กต์ของคุณ

ในเกมของคุณ คุณสามารถรับ Firebase UID ของผู้ใช้ได้จากออบเจ็กต์ User :

สวิฟท์

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
}

วัตถุประสงค์-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 และกฎความปลอดภัยของ Cloud Storage คุณสามารถรับรหัสผู้ใช้เฉพาะของผู้ใช้ที่ลงชื่อเข้าใช้ได้จากตัวแปร auth และใช้เพื่อควบคุมข้อมูลที่ผู้ใช้จะเข้าถึงได้

หากต้องการรับข้อมูลผู้เล่น Game Center ของผู้ใช้หรือเข้าถึงบริการ Game Center ให้ใช้ API ที่ได้รับจาก Game Kit

หากต้องการลงชื่อผู้ใช้ออกจาก Firebase ให้โทร Auth.signOut() :

สวิฟท์

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

วัตถุประสงค์-C

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