คุณอนุญาตให้ผู้ใช้ตรวจสอบสิทธิ์กับ Firebase โดยใช้ผู้ให้บริการ OAuth เช่น GitHub โดยผสานการเข้าสู่ระบบ OAuth ทั่วไปในแอปของคุณโดยใช้ Firebase SDK เพื่อดำเนินการขั้นตอนการลงชื่อเข้าใช้ตั้งแต่ต้นจนจบ
ก่อนจะเริ่ม
ในการลงชื่อเข้าใช้ผู้ใช้โดยใช้บัญชี GitHub คุณต้องเปิดใช้ GitHub เป็นผู้ให้บริการลงชื่อเข้าใช้สำหรับโปรเจ็กต์ Firebase ของคุณก่อน:
ใช้ Swift Package Manager เพื่อติดตั้งและจัดการการขึ้นต่อกันของ Firebase
- ใน Xcode เมื่อโปรเจ็กต์แอปของคุณเปิดอยู่ ให้ไปที่ File > Add Packages
- เมื่อได้รับแจ้ง ให้เพิ่มที่เก็บ SDK ของแพลตฟอร์ม Firebase Apple:
- เลือกไลบรารีการตรวจสอบสิทธิ์ Firebase
- เมื่อเสร็จแล้ว Xcode จะเริ่มแก้ไขและดาวน์โหลดการพึ่งพาของคุณในพื้นหลังโดยอัตโนมัติ
https://github.com/firebase/firebase-ios-sdk
ตอนนี้ ดำเนินการตามขั้นตอนการกำหนดค่า:
- ใน คอนโซล Firebase ให้เปิดส่วนการ ตรวจสอบสิทธิ์
- บนแท็บ วิธีการลงชื่อเข้า ใช้ ให้เปิดใช้งานผู้ให้บริการ GitHub
- เพิ่ม Client ID และ Client Secret จากคอนโซลนักพัฒนาซอฟต์แวร์ของผู้ให้บริการรายนั้นไปยังการกำหนดค่าผู้ให้บริการ:
- ลงทะเบียนแอปของคุณเป็นแอปพลิ เคชันนักพัฒนาซอฟต์แวร์บน GitHub และรับ OAuth 2.0 Client ID และ Client Secret ของแอป
- ตรวจสอบให้แน่ใจว่า Firebase OAuth เปลี่ยนเส้นทาง URI ของคุณ (เช่น
my-app-12345.firebaseapp.com/__/auth/handler
) ถูกตั้งค่าเป็น URL การเรียกกลับการอนุญาต ของคุณในหน้าการตั้งค่าของ แอปบน config ของแอป GitHub
- คลิก บันทึก
จัดการขั้นตอนการลงชื่อเข้าใช้ด้วย Firebase SDK
หากต้องการจัดการขั้นตอนการลงชื่อเข้าใช้ด้วย SDK แพลตฟอร์ม Firebase Apple ให้ทำตามขั้นตอนเหล่านี้:
เพิ่มรูปแบบ URL ที่กำหนดเองให้กับโปรเจ็กต์ Xcode ของคุณ:
- เปิดการกำหนดค่าโครงการของคุณ: ดับเบิลคลิกที่ชื่อโครงการในมุมมองต้นไม้ด้านซ้าย เลือกแอปของคุณจากส่วน เป้าหมาย จากนั้นเลือกแท็บ ข้อมูล แล้วขยายส่วน ประเภท URL
- คลิกปุ่ม + และเพิ่มรูปแบบ URL สำหรับรหัสไคลเอ็นต์ที่ย้อนกลับของคุณ หากต้องการค้นหาค่านี้ ให้เปิดไฟล์การกำหนดค่า
และมองหาคีย์GoogleService-Info.plist REVERSED_CLIENT_ID
คัดลอกค่าของคีย์นั้น และวางลงในกล่อง URL Schemes บนหน้าการกำหนดค่า เว้นช่องอื่นๆ ว่างไว้เมื่อเสร็จสิ้น การกำหนดค่าของคุณควรมีลักษณะคล้ายกับต่อไปนี้ (แต่ด้วยค่าเฉพาะแอปพลิเคชันของคุณ):
สร้างอินสแตนซ์ของ OAuthProvider โดยใช้ ID ผู้ให้บริการ github.com
Swift
var provider = OAuthProvider(providerID: "github.com")
วัตถุประสงค์-C
FIROAuthProvider *provider = [FIROAuthProvider providerWithProviderID:@"github.com"];
ไม่บังคับ : ระบุพารามิเตอร์ OAuth ที่กำหนดเองเพิ่มเติมที่คุณต้องการส่งด้วยคำขอ OAuth
Swift
provider.customParameters = [ "allow_signup": "false" ]
วัตถุประสงค์-C
[provider setCustomParameters:@{@"allow_signup": @"false"}];
สำหรับพารามิเตอร์ที่ GitHub รองรับ โปรดดู เอกสารประกอบ GitHub OAuth โปรดทราบว่าคุณไม่สามารถส่งผ่านพารามิเตอร์ที่ต้องใช้ Firebase ด้วย
setCustomParameters
พารามิเตอร์เหล่านี้ ได้แก่ client_id , redirect_uri , response_type , scope and stateไม่บังคับ : ระบุขอบเขต OAuth 2.0 เพิ่มเติมนอกเหนือจากโปรไฟล์พื้นฐานที่คุณต้องการขอจากผู้ให้บริการตรวจสอบสิทธิ์ หากแอปพลิเคชันของคุณต้องการเข้าถึงข้อมูลส่วนตัวผู้ใช้จาก GitHub API คุณจะต้องขอสิทธิ์เข้าถึง GitHub API ภายใต้ สิทธิ์ API ในคอนโซลนักพัฒนาซอฟต์แวร์ GitHub ขอบเขต OAuth ที่ขอต้องตรงกันทุกประการกับขอบเขตที่กำหนดค่าไว้ล่วงหน้าในการอนุญาต API ของแอป
Swift
// Request read access to a user's email addresses. // This must be preconfigured in the app's API permissions. provider.scopes = ["user:email"]
วัตถุประสงค์-C
// Request read access to a user's email addresses. // This must be preconfigured in the app's API permissions. [provider setScopes:@[@"user:email"]];
หากต้องการเรียนรู้เพิ่มเติม โปรดดู เอกสารประกอบขอบเขต GitHub
ทางเลือก : หากคุณต้องการปรับแต่งวิธีที่แอปของคุณนำเสนอ
SFSafariViewController
หรือUIWebView
เมื่อแสดง reCAPTCHA แก่ผู้ใช้ ให้สร้างคลาสที่กำหนดเองที่สอดคล้องกับโปรโตคอลFIRAuthUIDelegate
และส่งผ่านไปยังgetCredentialWithUIDelegate:completion:
ตรวจสอบสิทธิ์กับ Firebase โดยใช้วัตถุผู้ให้บริการ OAuth
Swift
provider.getCredentialWith(nil) { credential, error in if error != nil { // Handle error. } if credential != nil { Auth().signIn(with: credential) { authResult, error in if error != nil { // Handle error. } // User is signed in. // IdP data available in authResult.additionalUserInfo.profile. guard let oauthCredential = authResult.credential as? OAuthCredential else { return } // GitHub OAuth access token can also be retrieved by: // oauthCredential.accessToken // GitHub OAuth ID token can be retrieved by calling: // oauthCredential.idToken } } }
วัตถุประสงค์-C
[provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential, NSError *_Nullable error) { if (error) { // Handle error. } if (credential) { [[FIRAuth auth] signInWithCredential:credential completion:^(FIRAuthDataResult *_Nullable authResult, NSError *_Nullable error) { if (error) { // Handle error. } // User is signed in. // IdP data available in authResult.additionalUserInfo.profile. FIROAuthCredential *oauthCredential = (FIROAuthCredential *)authResult.credential; // GitHub OAuth access token can also be retrieved by: // oauthCredential.accessToken // GitHub OAuth ID token can be retrieved by calling: // oauthCredential.idToken }]; } }];
เมื่อใช้โทเค็นการเข้าถึง OAuth คุณสามารถเรียกใช้ GitHub API
ตัวอย่างเช่น หากต้องการรับข้อมูลโปรไฟล์พื้นฐาน คุณสามารถเรียกใช้ REST API โดยส่งโทเค็นการเข้าถึงในส่วนหัว
Authorization
:https://api.github.com/user
แม้ว่าตัวอย่างข้างต้นจะเน้นที่ขั้นตอนการลงชื่อเข้าใช้ แต่คุณก็สามารถลิงก์ผู้ให้บริการ GitHub กับผู้ใช้ที่มีอยู่ได้ ตัวอย่างเช่น คุณสามารถเชื่อมโยงผู้ให้บริการหลายรายกับผู้ใช้รายเดียวกันเพื่อให้ลงชื่อเข้าใช้ได้
Swift
Auth().currentUser.link(withCredential: credential) { authResult, error in if error != nil { // Handle error. } // GitHub credential is linked to the current user. // IdP data available in authResult.additionalUserInfo.profile. // GitHub OAuth access token can also be retrieved by: // authResult.credential.accessToken // GitHub OAuth ID token can be retrieved by calling: // authResult.credential.idToken }
วัตถุประสงค์-C
[[FIRAuth auth].currentUser linkWithCredential:credential completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { if (error) { // Handle error. } // GitHub credential is linked to the current user. // IdP data available in authResult.additionalUserInfo.profile. // GitHub OAuth access token is can also be retrieved by: // authResult.credential.accessToken // GitHub OAuth ID token can be retrieved by calling: // authResult.credential.idToken }];
รูปแบบเดียวกันสามารถใช้กับ
reauthenticateWithCredential
ซึ่งสามารถใช้เพื่อดึงข้อมูลประจำตัวใหม่สำหรับการดำเนินการที่มีความละเอียดอ่อนซึ่งต้องเข้าสู่ระบบล่าสุดSwift
Auth().currentUser.reauthenticateWithCredential(withCredential: credential) { authResult, error in if error != nil { // Handle error. } // User is re-authenticated with fresh tokens minted and // should be able to perform sensitive operations like account // deletion and email or password update. // IdP data available in result.additionalUserInfo.profile. // Additional OAuth access token is can also be retrieved by: // authResult.credential.accessToken // GitHub OAuth ID token can be retrieved by calling: // authResult.credential.idToken }
วัตถุประสงค์-C
[[FIRAuth auth].currentUser reauthenticateWithCredential:credential completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { if (error) { // Handle error. } // User is re-authenticated with fresh tokens minted and // should be able to perform sensitive operations like account // deletion and email or password update. // IdP data available in result.additionalUserInfo.profile. // Additional OAuth access token is can also be retrieved by: // authResult.credential.accessToken // GitHub OAuth ID token can be retrieved by calling: // authResult.credential.idToken }];
ขั้นตอนถัดไป
หลังจากที่ผู้ใช้ลงชื่อเข้าใช้เป็นครั้งแรก บัญชีผู้ใช้ใหม่จะถูกสร้างขึ้นและเชื่อมโยงกับข้อมูลประจำตัว กล่าวคือ ชื่อผู้ใช้และรหัสผ่าน หมายเลขโทรศัพท์ หรือข้อมูลผู้ให้บริการตรวจสอบสิทธิ์ ซึ่งผู้ใช้ลงชื่อเข้าใช้ บัญชีใหม่นี้จัดเก็บเป็นส่วนหนึ่งของโปรเจ็กต์ Firebase และใช้เพื่อระบุผู้ใช้ในทุกแอปในโปรเจ็กต์ของคุณ ไม่ว่าผู้ใช้จะลงชื่อเข้าใช้ด้วยวิธีใดก็ตาม
ในแอพของคุณ คุณสามารถรับข้อมูลโปรไฟล์พื้นฐานของผู้ใช้จากอ็อบเจ็กต์
FIRUser
ดู จัดการผู้ใช้ในฐานข้อมูล Firebase Realtime Database และ Cloud Storage Security Rules คุณสามารถรับ ID ผู้ใช้เฉพาะของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร
auth
และใช้เพื่อควบคุมข้อมูลที่ผู้ใช้สามารถเข้าถึงได้
คุณสามารถอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปของคุณโดยใช้ผู้ให้บริการตรวจสอบสิทธิ์หลายรายโดย เชื่อมโยงข้อมูลรับรองของผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีผู้ใช้ที่มีอยู่
หากต้องการออกจากระบบผู้ใช้ ให้โทรออกจาก signOut:
Swift
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; }
คุณอาจต้องการเพิ่มรหัสการจัดการข้อผิดพลาดสำหรับข้อผิดพลาดในการตรวจสอบสิทธิ์ทั้งหมด ดูการ จัดการข้อผิดพลาด