ด้วย Firebase JS SDK คุณสามารถให้ผู้ใช้ Firebase ตรวจสอบสิทธิ์โดยใช้ ผู้ให้บริการ OAuth ที่รองรับในสภาพแวดล้อม Cordova คุณสามารถผสานรวม ผู้ให้บริการ OAuth ทุกรายที่สนับสนุน โดยใช้ Firebase SDK เพื่อดำเนินการ ขั้น��อนการลงชื่อเข้าใช้ หรือโดยดำเนินการตามขั้นตอน OAuth ด้วยตนเองและส่งผ่าน OAuth ที่เป็นข้อมูลเข้าสู่ระบบ Firebase
ตั้งค่าการตรวจสอบสิทธิ์ Firebase สำหรับ Cordova
เพิ่ม Firebase ลงในโปรเจ็กต์ JavaScript เมื่อเพิ่ม ข้อมูลโค้ด Firebase ให้บันทึกตัวแปรการกำหนดค่า
authDomain
ซึ่งควรมีลักษณะเช่นนี้my-app.firebaseapp.com
หากใช้โดเมนที่กำหนดเอง แทนที่จะเป็นโดเมนfirebaseapp.com
ที่จัดสรรโดย Firebase ระบบจะกำหนดค่า ควรใช้โดเมนแทนหากต้องการตั้งค่าแอป Android ให้เพิ่มแอป Android ลงในคอนโซล Firebase และป้อนรายละเอียดแอปของคุณ คุณไม่จำเป็นต้องมี google-services.json
ในการตั้งค่าแอป iOS ให้สร้างแอปพลิเคชัน iOS แล้วเพิ่มลงใน คอนโซล Firebase คุณจะต้องใช้รหัสชุด iOS เพื่อเพิ่มในภายหลังเมื่อ การติดตั้งปลั๊กอินรูปแบบ URL ที่กำหนดเอง
เปิดใช้ลิงก์แบบไดนามิกของ Firebase:
- ในคอนโซล Firebase ให้เปิดส่วนลิงก์แบบไดนามิก
-
หากคุณยังไม่ได้ยอมรับข้อกำหนดลิงก์แบบไดนามิกและสร้างลิงก์แบบไดนามิกแล้ว โดเมน ให้ดำเนินการในขณะนี้
หากคุณสร้างโดเมนลิงก์แบบไดนามิกไว้แล้ว โปรดจดบันทึกไว้ ลิงก์แบบไดนามิก โดเมนมักจะมีลักษณะดังตัวอย่างต่อไปนี้
example.page.link
คุณจะต้องใช้ค่านี้เมื่อกำหนดค่าแอป Apple หรือ Android เป็น สกัดกั้นลิงก์ขาเข้า
เปิดใช้ Google Sign-In ในคอนโซล Firebase
- ในคอนโซล Firebase ให้เปิดส่วนการตรวจสอบสิทธิ์
- ในแท็บวิธีการลงชื่อเข้าใช้ ให้เปิดใช้วิธีการลงชื่อเข้าใช้ Google และ คลิกบันทึก
ติดตั้งปลั๊กอินที่จำเป็นใ��โปรเจ็กต์ Cordova
# Plugin to pass application build info (app name, ID, etc) to the OAuth widget. cordova plugin add cordova-plugin-buildinfo --save # Plugin to handle Universal Links (Android app link redirects) cordova plugin add cordova-universal-links-plugin-fix --save # Plugin to handle opening secure browser views on iOS/Android mobile devices cordova plugin add cordova-plugin-browsertab --save # Plugin to handle opening a browser view in older versions of iOS and Android cordova plugin add cordova-plugin-inappbrowser --save # Plugin to handle deep linking through Custom Scheme for iOS # Substitute *com.firebase.cordova* with the iOS bundle ID of your app. cordova plugin add cordova-plugin-customurlscheme --variable \ URL_SCHEME=com.firebase.cordova --save
เพิ่มการกำหนดค่าต่อไปนี้ลงในไฟล์ Cordova
config.xml
โดยที่AUTH_DOMAIN
คือโดเมนจากขั้นตอนที่ (1) และDYNAMIC_LINK_DOMAIN
คือค่า จากขั้นตอน (3c)<universal-links> <host name="DYNAMIC_LINK_DOMAIN" scheme="https" /> <host name="AUTH_DOMAIN" scheme="https"> <path url="/__/auth/callback"/> </host> </universal-links>
ตัวอย่างการกำหนดค่าอาจมีลักษณะดังนี้
<universal-links> <host name="example.page.link" scheme="https" /> <host name="example-app.firebaseapp.com" scheme="https"> <path url="/__/auth/callback"/> </host> </universal-links>
หากใช้โดเมนที่กำหนดเอง
auth.custom.domain.com
ให้ใช้แทนmy-app.firebaseapp.com
ด้วยสำหรับแอปพลิเคชัน Android
singleTask
ควรใช้สำหรับlaunchMode
<preference name="AndroidLaunchMode" value="singleTask" />
จัดการขั้นตอนการลงชื่อเข้าใช้ด้วย Firebase SDK
การตรวจสอบสิทธิ์ Firebase ขึ้นอยู่กับเหตุการณ์
deviceReady
เพื่อระบุ ของสภาพแวดล้อม Cordova ในปัจจุบันอย่างถูกต้อง ตรวจสอบอินสแตนซ์ของแอป Firebase จะเริ่มต้นขึ้นหลังจากทริกเกอร์เหตุการณ์นั้นสร้างอินสแตนซ์ของออบเจ็กต์ผู้ให้บริการ Google ดังนี้
Web
import { GoogleAuthProvider } from "firebase/auth/cordova"; const provider = new GoogleAuthProvider();
Web
var provider = new firebase.auth.GoogleAuthProvider();
ตรวจสอบสิทธิ์กับ Firebase โดยใช้ออบเจ็กต์ผู้ให้บริการของ Google ที่ใช้
signInWithRedirect
. โปรดทราบว่าsignInWithPopup
ไม่รองรับใน CordovaWeb
import { getAuth, signInWithRedirect, getRedirectResult, GoogleAuthProvider } from "firebase/auth/cordova"; const auth = getAuth(); signInWithRedirect(auth, new GoogleAuthProvider()) .then(() => { return getRedirectResult(auth); }) .then((result) => { const credential = GoogleAuthProvider.credentialFromResult(result); // This gives you a Google Access Token. // You can use it to access the Google API. const token = credential.accessToken; // The signed-in user info. const user = result.user; // ... }).catch((error) => { // Handle Errors here. const errorCode = error.code; const errorMessage = error.message; });
Web
firebase.auth().signInWithRedirect(provider).then(() => { return firebase.auth().getRedirectResult(); }).then((result) => { /** @type {firebase.auth.OAuthCredential} */ var credential = result.credential; // This gives you a Google Access Token. // You can use it to access the Google API. var token = credential.accessToken; // The signed-in user info. var user = result.user; // ... }).catch((error) => { // Handle Errors here. var errorCode = error.code; var errorMessage = error.message; });
จัดการกรณีที่กิจกรรมบนแอปถูกทำลายก่อนการลงชื่อเข้าใช้ ��ารดำเนินการเสร็จสมบูรณ์ ให้เรียกใช้
getRedirectResult
เมื่อแอปของคุณ โหลดขึ้นมาWeb
import { getAuth, getRedirectResult, GoogleAuthProvider } from "firebase/auth/cordova"; const auth = getAuth(); getRedirectResult(auth) .then((result) => { const credential = GoogleAuthProvider.credentialFromResult(result); if (credential) { // This gives you a Google Access Token. // You can use it to access the Google API. const token = credential.accessToken; // The signed-in user info. const user = result.user; // ... } }) .catch((error) => { // Handle Errors here. const errorCode = error.code; const errorMessage = error.message; });
Web
firebase.auth().getRedirectResult().then((result) => { if (result.credential) { /** @type {firebase.auth.OAuthCredential} */ var credential = result.credential; // This gives you a Google Access Token. // You can use it to access the Google API. var token = credential.accessToken; // The signed-in user info. var user = result.user; // ... } }).catch((error) => { // Handle Errors here. var errorCode = error.code; var errorMessage = error.message; });
คุณสามารถใช้กลไกเดียวกันนี้เพื่อลิงก์ผู้ให้บริการใหม่ผ่าน
linkWithRedirect
หรือตรวจสอบสิทธิ์อีกครั้งกับผู้ให้บริการที่มีอยู่ ด้วยreauthenticateWithRedirect