คุณให้ผู้ใช้ตรวจสอบสิทธิ์กับ Firebase ได้โดยใช้บัญชี Google
ก่อนจะเริ่ม
หากคุณยังไม่ได้ เพิ่ม Firebase ในโครงการ Android ของคุณ
ใช้ Firebase Android BoM ประกาศการพึ่งพาสำหรับไลบรารี Firebase Authentication Android ใน ไฟล์ Gradle ของโมดูล (ระดับแอป) (โดยปกติคือ
app/build.gradle
)นอกจากนี้ ในการตั้งค่าการตรวจสอบสิทธิ์ Firebase คุณต้องเพิ่ม SDK บริการ Google Play ในแอปของคุณ
Java
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:30.3.1') // 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'
// Also declare the dependency for the Google Play services library and specify its version implementation 'com.google.android.gms:play-services-auth:20.2.0' }เมื่อใช้ 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:21.0.7'
// Also declare the dependency for the Google Play services library and specify its version implementation 'com.google.android.gms:play-services-auth:20.2.0' }Kotlin+KTX
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:30.3.1') // 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'
// Also declare the dependency for the Google Play services library and specify its version implementation 'com.google.android.gms:play-services-auth:20.2.0' }เมื่อใช้ 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:21.0.7'
// Also declare the dependency for the Google Play services library and specify its version implementation 'com.google.android.gms:play-services-auth:20.2.0' }หากคุณยังไม่ได้ระบุลายนิ้วมือ SHA-1 ของแอป ให้ดำเนินการจาก หน้าการตั้งค่า ของคอนโซล Firebase อ้างถึงการ รับรองความถูกต้องไคลเอ็นต์ของคุณ สำหรับรายละเอียดเกี่ยวกับวิธีรับลายนิ้วมือ SHA-1 ของแอป
- เปิดใช้งาน Google เป็นวิธีลงชื่อเข้าใช้ในคอนโซล Firebase:
- ใน คอนโซล Firebase ให้เปิดส่วนการ ตรวจสอบสิทธิ์
- บนแท็บ วิธีการลงชื่อเข้า ใช้ ให้เปิดใช้งานวิธีการลงชื่อเข้าใช้ ของ Google แล้วคลิก บันทึก
ตรวจสอบสิทธิ์ด้วย Firebase
- รวมการลงชื่อเข้าใช้ Google One Tap ในแอปของคุณโดยทำตามขั้นตอนในหน้า ลงชื่อเข้าใช้ผู้ใช้ด้วยหน้าข้อมูลรับรองที่บันทึกไว้ เมื่อคุณกำหนดค่าอ็อบเจ็กต์
BeginSignInRequest
ให้เรียกsetGoogleIdTokenRequestOptions
:คุณต้องส่งรหัสไคลเอ็นต์ "เซิร์ฟเวอร์" ไปยังเมธอด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();
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()
setGoogleIdTokenRequestOptions
หากต้องการค้นหารหัสไคลเอ็นต์ OAuth 2.0:- เปิด หน้าข้อมูลรับรอง ในคอนโซล GCP
- รหัสไคลเอ็นต์ประเภท แอปพลิเคชันเว็บ คือรหัสไคลเอ็นต์ OAuth 2.0 ของเซิร์ฟเวอร์ส่วนหลังของคุณ
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; } } }
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) { // ... } } } // ... }
- ในวิธีการ
onCreate
ของกิจกรรมการลงชื่อเข้าใช้ รับอินสแตนซ์ที่ใช้ร่วมกันของวัตถุFirebaseAuth
:Java
private FirebaseAuth mAuth; // ... // Initialize Firebase Auth mAuth = FirebaseAuth.getInstance();
Kotlin+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); }
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) }
- ในตัวจัดการ
onActivityResult()
ของคุณ (ดูขั้นตอนที่ 1) รับโทเค็น Google ID ของผู้ใช้ แลกเปลี่ยนเป็นข้อมูลรับรอง Firebase และรับรองความถูกต้องกับ Firebase โดยใช้ข้อมูลรับรอง Firebase:หากการเรียก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); } } }); }
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!") } }
signInWithCredential
สำเร็จ คุณสามารถใช้เมธอดgetCurrentUser
เพื่อรับข้อมูลบัญชีของผู้ใช้
ขั้นตอนถัดไป
หลังจากที่ผู้ใช้ลงชื่อเข้าใช้เป็นครั้งแรก บัญชีผู้ใช้ใหม่จะถูกสร้างขึ้นและเชื่อมโยงกับข้อมูลประจำตัว กล่าวคือ ชื่อผู้ใช้และรหัสผ่าน หมายเลขโทรศัพท์ หรือข้อมูลผู้ให้บริการตรวจสอบสิทธิ์ ซึ่งผู้ใช้ลงชื่อเข้าใช้ บัญชีใหม่นี้จัดเก็บเป็นส่วนหนึ่งของโปรเจ็กต์ Firebase และใช้เพื่อระบุผู้ใช้ในทุกแอปในโปรเจ็กต์ของคุณ ไม่ว่าผู้ใช้จะลงชื่อเข้าใช้ด้วยวิธีใดก็ตาม
ในแอปของคุณ คุณสามารถรับข้อมูลโปรไฟล์พื้นฐานของผู้ใช้จากอ็อบเจ็กต์
FirebaseUser
ดู จัดการผู้ใช้ในฐานข้อมูล Firebase Realtime Database และ Cloud Storage Security Rules คุณสามารถรับ ID ผู้ใช้เฉพาะของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร
auth
และใช้เพื่อควบคุมข้อมูลที่ผู้ใช้สามารถเข้าถึงได้
คุณสามารถอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปของคุณโดยใช้ผู้ให้บริการตรวจสอบสิทธิ์หลายรายโดย เชื่อมโยงข้อมูลรับรองของผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีผู้ใช้ที่มีอยู่
หากต้องการออกจากระบบผู้ใช้ โทร signOut
:
Java
FirebaseAuth.getInstance().signOut();
Kotlin+KTX
Firebase.auth.signOut()