คุณสามารถให้ผู้ใช้ของคุณตรวจสอบสิทธิ์กับ Firebase โดยใช้บัญชี Google ของพวกเขา
ก่อนที่คุณจะเริ่มต้น
หากคุณยังไม่ได้ ดำเนินการ ให้เพิ่ม Firebase ในโครงการ Android ของคุณ
ใน ไฟล์ Gradle ของโมดูล (ระดับแอป) (โดยปกติคือ
<project>/<app-module>/build.gradle.kts
หรือ<project>/<app-module>/build.gradle
) เพิ่มการพึ่งพาสำหรับ Firebase Authentication ห้องสมุด Android ขอแนะนำให้ใช้ Firebase Android BoM เพื่อควบคุมการกำหนดเวอร์ชันของไลบรารีนอกจากนี้ ในการตั้งค่า Firebase Authentication คุณต้องเพิ่ม SDK บริการ Google Play ลงในแอปของคุณ
Kotlin+KTX
dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:32.3.1")) // Add 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")
// Also add the dependency for the Google Play services library and specify its version implementation("com.google.android.gms:play-services-auth:20.7.0") }เมื่อใช้ Firebase Android BoM แอปของคุณจะใช้ไลบรารี Firebase Android เวอร์ชันที่เข้ากันได้เสมอ
(ทางเลือก) เพิ่มการอ้างอิงไลบรารี Firebase โดยไม่ ใช้ BoM
หากคุณเลือกที่จะไม่ใช้ Firebase BoM คุณต้องระบุแต่ละเวอร์ชันของไลบรารี Firebase ในบรรทัดอ้างอิง
โปรดทราบว่าหากคุณใช้ไลบรารี Firebase หลาย ไลบรารีในแอป เราขอแนะนำอย่างยิ่งให้ใช้ BoM เพื่อจัดการเวอร์ชันของไลบรารี ซึ่งทำให้แน่ใจว่าเวอร์ชันทั้งหมดเข้ากันได้
dependencies { // Add 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:22.1.2")
// Also add the dependency for the Google Play services library and specify its version implementation("com.google.android.gms:play-services-auth:20.7.0") }Java
dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:32.3.1")) // Add 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")
// Also add the dependency for the Google Play services library and specify its version implementation("com.google.android.gms:play-services-auth:20.7.0") }เมื่อใช้ Firebase Android BoM แอปของคุณจะใช้ไลบรารี Firebase Android เวอร์ชันที่เข้ากันได้เสมอ
(ทางเลือก) เพิ่มการอ้างอิงไลบรารี Firebase โดยไม่ ใช้ BoM
หากคุณเลือกที่จะไม่ใช้ Firebase BoM คุณต้องระบุแต่ละเวอร์ชันของไลบรารี Firebase ในบรรทัดอ้างอิง
โปรดทราบว่าหากคุณใช้ไลบรารี Firebase หลาย ไลบรารีในแอป เราขอแนะนำอย่างยิ่งให้ใช้ BoM เพื่อจัดการเวอร์ชันของไลบรารี ซึ่งทำให้แน่ใจว่าเวอร์ชันทั้งหมดเข้ากันได้
dependencies { // Add 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:22.1.2")
// Also add the dependency for the Google Play services library and specify its version implementation("com.google.android.gms:play-services-auth:20.7.0") }หากคุณยังไม่ได้ระบุลายนิ้วมือ SHA ของแอป ให้ระบุจาก หน้าการตั้งค่า ของคอนโซล Firebase โปรดดูที่ การยืนยันตัวตนไคลเอ็นต์ของคุณ สำหรับรายละเอียดเกี่ยวกับวิธีรับลายนิ้วมือ SHA ของแอป
- เปิดใช้งาน Google เป็นวิธีการลงชื่อเข้าใช้ในคอนโซล Firebase:
- ใน คอนโซล Firebase ให้เปิดส่วน การรับรองความถูกต้อง
- ในแท็บ วิธีการลงชื่อเข้า ใช้ ให้เปิดใช้งานวิธีการลงชื่อเข้าใช้ Google แล้วคลิก บันทึก
เมื่อได้รับแจ้งในคอนโซล ให้ดาวน์โหลดไฟล์การกำหนดค่า Firebase ที่อัปเดต (
google-services.json
) ซึ่งขณะนี้มีข้อมูลไคลเอ็นต์ OAuth ที่จำเป็นสำหรับการลงชื่อเข้าใช้ Googleย้ายไฟล์กำหนดค่าที่อัปเดตนี้ไปยังโปรเจ็กต์ Android Studio แทนที่ ไฟล์กำหนดค่าที่เกี่ยวข้องซึ่งล้าสมัยแล้ว (ดู เพิ่ม Firebase ในโครงการ Android ของคุณ )
รับรองความถูกต้องด้วย Firebase
- รวมการลงชื่อเข้าใช้ Google One Tap เข้ากับแอปของคุณโดยทำตามขั้นตอนในหน้า ลงชื่อเข้าใช้ผู้ใช้ด้วยข้อมูลประจำตัวที่บันทึกไว้ เมื่อคุณกำหนดค่าออบเจ็กต์
BeginSignInRequest
ให้เรียกsetGoogleIdTokenRequestOptions
:คุณต้องส่งรหัสไคลเอ็นต์ "เซิร์ฟเวอร์" ของคุณไปยังเมธอดKotlin+KTX
signInRequest = BeginSignInRequest.builder() .setGoogleIdTokenRequestOptions( BeginSignInRequest.GoogleIdTokenRequestOptions.builder() .setSupported(true) // Your server's client ID, not your Android client ID. .setServerClientId(getString(R.string.your_web_client_id)) // Only show accounts previously used to sign in. .setFilterByAuthorizedAccounts(true) .build()) .build()
Java
signInRequest = BeginSignInRequest.builder() .setGoogleIdTokenRequestOptions(GoogleIdTokenRequestOptions.builder() .setSupported(true) // Your server's client ID, not your Android client ID. .setServerClientId(getString(R.string.default_web_client_id)) // Only show accounts previously used to sign in. .setFilterByAuthorizedAccounts(true) .build()) .build();
setGoogleIdTokenRequestOptions
หากต้องการค้นหารหัสลูกค้า OAuth 2.0:- เปิด หน้าข้อมูลประจำตัว ในคอนโซล GCP
- รหัสไคล เอนต์ประเภทเว็บแอป พลิเคชันคือรหัสไคลเอ็นต์ OAuth 2.0 ของเซิร์ฟเวอร์ส่วนหลังของคุณ
Kotlin+KTX
class YourActivity : AppCompatActivity() { // ... private val REQ_ONE_TAP = 2 // Can be any integer unique to the Activity private var showOneTapUI = true // ... override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) when (requestCode) { REQ_ONE_TAP -> { try { val credential = oneTapClient.getSignInCredentialFromIntent(data) val idToken = credential.googleIdToken when { idToken != null -> { // Got an ID token from Google. Use it to authenticate // with Firebase. Log.d(TAG, "Got ID token.") } else -> { // Shouldn't happen. Log.d(TAG, "No ID token!") } } } catch (e: ApiException) { // ... } } } // ... }
Java
public class YourActivity extends AppCompatActivity { // ... private static final int REQ_ONE_TAP = 2; // Can be any integer unique to the Activity. private boolean showOneTapUI = true; // ... @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQ_ONE_TAP: try { SignInCredential credential = oneTapClient.getSignInCredentialFromIntent(data); String idToken = credential.getGoogleIdToken(); if (idToken != null) { // Got an ID token from Google. Use it to authenticate // with Firebase. Log.d(TAG, "Got ID token."); } } catch (ApiException e) { // ... } break; } } }
- ในเมธอด
onCreate
ของกิจกรรมการลงชื่อเข้าใช้ ให้รับอินสแตนซ์ที่ใช้ร่วมกันของอ็อบเจ็กต์FirebaseAuth
:Kotlin+KTX
private lateinit var auth: FirebaseAuth // ... // Initialize Firebase Auth auth = Firebase.auth
Java
private FirebaseAuth mAuth; // ... // Initialize Firebase Auth mAuth = FirebaseAuth.getInstance();
- เมื่อเริ่มต้นกิจกรรมของคุณ ให้ตรวจดูว่าผู้ใช้ลงชื่อเข้าใช้อยู่หรือไม่:
Kotlin+KTX
override fun onStart() { super.onStart() // Check if user is signed in (non-null) and update UI accordingly. val currentUser = auth.currentUser updateUI(currentUser) }
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); }
- ในตัวจัดการ
onActivityResult()
ของคุณ (ดูขั้นตอนที่ 1) รับโทเค็น Google ID ของผู้ใช้ แลกเปลี่ยนเป็นข้อมูลรับรอง Firebase และตรวจสอบสิทธิ์กับ Firebase โดยใช้ข้อมูลรับรอง Firebase:หากการเรียกKotlin+KTX
val googleCredential = oneTapClient.getSignInCredentialFromIntent(data) val idToken = googleCredential.googleIdToken when { idToken != null -> { // Got an ID token from Google. Use it to authenticate // with Firebase. val firebaseCredential = GoogleAuthProvider.getCredential(idToken, null) auth.signInWithCredential(firebaseCredential) .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) updateUI(null) } } } else -> { // Shouldn't happen. Log.d(TAG, "No ID token!") } }
Java
SignInCredential googleCredential = oneTapClient.getSignInCredentialFromIntent(data); String idToken = googleCredential.getGoogleIdToken(); if (idToken != null) { // Got an ID token from Google. Use it to authenticate // with Firebase. AuthCredential firebaseCredential = GoogleAuthProvider.getCredential(idToken, null); mAuth.signInWithCredential(firebaseCredential) .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()); updateUI(null); } } }); }
signInWithCredential
สำเร็จ คุณสามารถใช้เมธอดgetCurrentUser
เพื่อรับข้อมูลบัญชีผู้ใช้
ขั้นตอนถัดไป
หลังจากที่ผู้ใช้ลงชื่อเข้าใช้เป็นครั้งแรก บัญชีผู้ใช้ใหม่จะถูกสร้างขึ้นและเชื่อมโยงกับข้อมูลรับรอง ซึ่งได้แก่ ชื่อผู้ใช้และรหัสผ่าน หมายเลขโทรศัพท์ หรือข้อมูลผู้ให้บริการรับรองความถูกต้อง ซึ่งผู้ใช้ลงชื่อเข้าใช้ด้วย บัญชีใหม่นี้จัดเก็บเป็นส่วนหนึ่งของโปรเจ็กต์ Firebase และสามารถใช้ระบุผู้ใช้ในทุกแอปในโครงการ ไม่ว่าผู้ใช้จะลงชื่อเข้าใช้ด้วยวิธีใดก็ตาม
ในแอปของคุณ คุณสามารถรับข้อมูลโปรไฟล์พื้นฐานของผู้ใช้ได้จากออบเจ็กต์
FirebaseUser
ดู จัดการผู้ใช้ใน กฎความปลอดภัย ของ Firebase Realtime Database และ Cloud Storage คุณสามารถรับ ID ผู้ใช้ที่ไม่ซ้ำกันของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร
auth
และใช้เพื่อควบคุมข้อมูลที่ผู้ใช้สามารถเข้าถึงได้
คุณสามารถอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปของคุณโดยใช้ผู้ให้บริการตรวจสอบสิทธิ์หลายรายโดย เชื่อมโยงข้อมูลประจำตัวของผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีผู้ใช้ที่มีอยู่
ในการออกจากระบบผู้ใช้ โทร signOut
:
Kotlin+KTX
Firebase.auth.signOut()
Java
FirebaseAuth.getInstance().signOut();