คุณสามารถให้ผู้ใช้ตรวจสอบสิทธิ์กับ Firebase โดยใช้บัญชี Facebook ของพวกเขาได้โดยรวมการเข้าสู่ระบบ Facebook เข้ากับแอปของคุณ
ก่อนที่คุณจะเริ่ม
เพิ่ม Firebase ในโปรเจ็กต์ Android ของคุณ หากยังไม่ได้ ทำ
- ในเว็บไซต์ Facebook for Developers รับ รหัสแอป และ รหัส ลับ ของแอปสำหรับแอปของคุณ
- เปิดใช้งานการเข้าสู่ระบบ Facebook:
- ใน คอนโซล Firebase ให้เปิดส่วน Auth
- บนแท็บ วิธีการลงชื่อเข้า ใช้ให้เปิดใช้งานวิธีการลงชื่อเข้าใช้ Facebook และระบุ รหัส แอป และ ความลับของแอปที่ คุณได้รับจาก Facebook
- จากนั้นตรวจสอบให้แน่ใจว่า URI การเปลี่ยนเส้นทาง OAuth ของคุณ (เช่น
my-app-12345.firebaseapp.com/__/auth/handler
) อยู่ในรายการ URI การเปลี่ยนเส้นทาง OAuth ของคุณในหน้าการตั้งค่าของแอป Facebook บนไซต์ Facebook สำหรับนักพัฒนา ใน ผลิตภัณฑ์ การตั้งค่า> การกำหนดค่าการเข้าสู่ระบบ Facebook
ใช้ Firebase Android BoM ประกาศการอ้างอิงสำหรับไลบรารี Android ของ Firebase Authentication ใน โมดูล ของคุณ (ระดับแอป) ไฟล์ Gradle (โดยทั่วไปคือ
app/build.gradle
)Java
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:26.6.0') // Declare the dependency for the Firebase Authentication library // When using the BoM, you don't specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-auth' }
เมื่อใช้ Firebase Android BoM แอปของคุณจะใช้ไลบรารี Firebase Android เวอร์ชันที่เข้ากันได้เสมอ
(ทางเลือก) ประกาศการอ้างอิงไลบรารี Firebase โดยไม่ต้อง ใช้ BoM
หากคุณเลือกที่จะไม่ใช้ Firebase BoM คุณต้องระบุไลบรารี Firebase แต่ละเวอร์ชันในบรรทัดการอ้างอิง
โปรดทราบว่าหากคุณใช้ไลบรารี Firebase หลาย ไลบรารีในแอปของคุณเราขอแนะนำอย่างยิ่งให้ใช้ BoM เพื่อจัดการเวอร์ชันไลบรารีซึ่งจะช่วยให้มั่นใจได้ว่าทุกเวอร์ชันจะเข้ากันได้
dependencies { // Declare the dependency for the Firebase Authentication library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-auth:20.0.3' }
โคตรลิน + KTX
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:26.6.0') // Declare the dependency for the Firebase Authentication library // When using the BoM, you don't specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-auth-ktx' }
เมื่อใช้ Firebase Android BoM แอปของคุณจะใช้ไลบรารี Firebase Android เวอร์ชันที่เข้ากันได้เสมอ
(ทางเลือก) ประกาศการอ้างอิงไลบรารี Firebase โดยไม่ต้อง ใช้ BoM
หากคุณเลือกที่จะไม่ใช้ Firebase BoM คุณต้องระบุไลบรารี Firebase แต่ละเวอร์ชันในบรรทัดการอ้างอิง
โปรดทราบว่าหากคุณใช้ไลบรารี Firebase หลาย ไลบรารีในแอปของคุณเราขอแนะนำอย่างยิ่งให้ใช้ BoM เพื่อจัดการเวอร์ชันไลบรารีซึ่งจะช่วยให้มั่นใจได้ว่าทุกเวอร์ชันจะเข้ากันได้
dependencies { // Declare the dependency for the Firebase Authentication library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-auth-ktx:20.0.3' }
ตรวจสอบสิทธิ์ด้วย Firebase
- รวมการเข้าสู่ระบบ Facebook เข้ากับแอพของคุณโดยทำตาม เอกสารของนักพัฒนา เมื่อคุณกำหนดค่า
LoginButton
หรือLoginManager
วัตถุขอpublic_profile
และemail
สิทธิ์ หากคุณรวมการเข้าสู่ระบบ Facebook โดยใช้LoginButton
กิจกรรมการลงชื่อเข้าใช้ของคุณจะมีรหัสคล้ายกับสิ่งต่อไปนี้:Java
// Initialize Facebook Login button mCallbackManager = CallbackManager.Factory.create(); LoginButton loginButton = mBinding.buttonFacebookLogin; loginButton.setReadPermissions("email", "public_profile"); loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { Log.d(TAG, "facebook:onSuccess:" + loginResult); handleFacebookAccessToken(loginResult.getAccessToken()); } @Override public void onCancel() { Log.d(TAG, "facebook:onCancel"); // ... } @Override public void onError(FacebookException error) { Log.d(TAG, "facebook:onError", error); // ... } }); // ... @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Pass the activity result back to the Facebook SDK mCallbackManager.onActivityResult(requestCode, resultCode, data); }
โคตรลิน + KTX
// Initialize Facebook Login button callbackManager = CallbackManager.Factory.create() binding.buttonFacebookLogin.setReadPermissions("email", "public_profile") binding.buttonFacebookLogin.registerCallback(callbackManager, object : FacebookCallback<LoginResult> { override fun onSuccess(loginResult: LoginResult) { Log.d(TAG, "facebook:onSuccess:$loginResult") handleFacebookAccessToken(loginResult.accessToken) } override fun onCancel() { Log.d(TAG, "facebook:onCancel") // ... } override fun onError(error: FacebookException) { Log.d(TAG, "facebook:onError", error) // ... } }) // ... override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) // Pass the activity result back to the Facebook SDK callbackManager.onActivityResult(requestCode, resultCode, data) }
- ใน
onCreate
ของกิจกรรมการลงชื่อเข้าใช้รับอินสแตนซ์ที่แชร์ของออบเจ็กต์FirebaseAuth
:Java
private FirebaseAuth mAuth; // ... // Initialize Firebase Auth mAuth = FirebaseAuth.getInstance();
โคตรลิน + KTX
private lateinit var auth: FirebaseAuth // ... // Initialize Firebase Auth auth = Firebase.auth
- เมื่อเริ่มต้นกิจกรรมของคุณให้ตรวจสอบว่าผู้ใช้ลงชื่อเข้าใช้อยู่หรือไม่:
Java
@Override public void onStart() { super.onStart(); // Check if user is signed in (non-null) and update UI accordingly. FirebaseUser currentUser = mAuth.getCurrentUser(); updateUI(currentUser); }
โคตรลิน + KTX
public override fun onStart() { super.onStart() // Check if user is signed in (non-null) and update UI accordingly. val currentUser = auth.currentUser updateUI(currentUser) }
- หลังจากที่ผู้ใช้ประสบความสำเร็จในสัญญาณใน
LoginButton
'sonSuccess
วิธีการโทรกลับได้รับการเข้าถึง token สำหรับลงชื่อเข้าใช้แลกเปลี่ยนเป็นข้อมูลประจำตัว Firebase และตรวจสอบกับ Firebase ใช้ข้อมูลประจำตัว Firebase:หากการเรียกเพื่อJava
private void handleFacebookAccessToken(AccessToken token) { Log.d(TAG, "handleFacebookAccessToken:" + token); AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken()); mAuth.signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInWithCredential:success"); FirebaseUser user = mAuth.getCurrentUser(); updateUI(user); } else { // If sign in fails, display a message to the user. Log.w(TAG, "signInWithCredential:failure", task.getException()); Toast.makeText(FacebookLoginActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); updateUI(null); } // ... } }); }
โคตรลิน + KTX
private fun handleFacebookAccessToken(token: AccessToken) { Log.d(TAG, "handleFacebookAccessToken:$token") val credential = FacebookAuthProvider.getCredential(token.token) auth.signInWithCredential(credential) .addOnCompleteListener(this) { task -> if (task.isSuccessful) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInWithCredential:success") val user = auth.currentUser updateUI(user) } else { // If sign in fails, display a message to the user. Log.w(TAG, "signInWithCredential:failure", task.exception) Toast.makeText(baseContext, "Authentication failed.", Toast.LENGTH_SHORT).show() updateUI(null) } // ... } }
signInWithCredential
สำเร็จคุณสามารถใช้เมธอดgetCurrentUser
เพื่อรับข้อมูลบัญชีของผู้ใช้
ขั้นตอนถัดไป
หลังจากผู้ใช้ลงชื่อเข้าใช้เป็นครั้งแรกบัญชีผู้ใช้ใหม่จะถูกสร้างขึ้นและเชื่อมโยงกับข้อมูลรับรองนั่นคือชื่อผู้ใช้และรหัสผ่านหมายเลขโทรศัพท์หรือข้อมูลผู้ให้บริการรับรองความถูกต้องซึ่งผู้ใช้ลงชื่อเข้าใช้ด้วย บัญชีใหม่นี้จัดเก็บเป็นส่วนหนึ่งของโปรเจ็กต์ Firebase ของคุณและสามารถใช้เพื่อระบุผู้ใช้ในทุกแอปในโปรเจ็กต์ของคุณได้ไม่ว่าผู้ใช้จะลงชื่อเข้าใช้อย่างไร
ในแอปของคุณคุณสามารถรับข้อมูลโปรไฟล์พื้นฐานของผู้ใช้จากออบเจ็กต์
FirebaseUser
ดู จัดการผู้ใช้ในฐานข้อมูลเรียลไทม์ Firebase และ กฎความปลอดภัยของที่ เก็บข้อมูลบนคลาวด์คุณสามารถรับ ID ผู้ใช้เฉพาะของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร
auth
และใช้เพื่อควบคุมข้อมูลที่ผู้ใช้เข้าถึงได้
คุณสามารถอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปของคุณโดยใช้ผู้ให้บริการการตรวจสอบสิทธิ์หลายรายโดยการ เชื่อมโยงข้อมูลรับรองของผู้ให้บริการรับรองความถูกต้องกับบัญชีผู้ใช้ที่มีอยู่
ในการออกจากระบบผู้ใช้โทร signOut
:
Java
FirebaseAuth.getInstance().signOut();
โคตรลิน + KTX
Firebase.auth.signOut()