คุณสามารถผสานรวม Firebase Authentication กับระบบการตรวจสอบสิทธิ์ที่กำหนดเองได้โดย แก้ไขเซิร์ฟเวอร์การตรวจสอบสิทธิ์เพื่อสร้างโทเค็นที่ลงนามแบบกำหนดเองเมื่อผู้ใช้ ลงชื่อเข้าใช้สำเร็จ แอปของคุณจะได้รับโทเค็นนี้และใช้เพื่อตรวจสอบสิทธิ์ กับ Firebase
ก่อนเริ่มต้น
- หากยังไม่ได้เพิ่ม ให้เพิ่ม Firebase ลงในโปรเจ็กต์ Android
-
ในไฟล์ Gradle ของโมดูล (ระดับแอป)
(โดยปกติคือ
<project>/<app-module>/build.gradle.ktsหรือ<project>/<app-module>/build.gradle) ให้เพิ่มทรัพยากร Dependency สำหรับคลัง Firebase Authentication สำหรับ Android เราขอแนะนำให้ใช้ Firebase Android BoM เพื่อควบคุมการกำหนดเวอร์ชันของไลบรารีdependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:35.0.0")) // 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") }
การใช้ Firebase Android BoM จะทำให้แอปใช้ไลบรารี Firebase Android เวอร์ชันที่เข้ากันได้อยู่เสมอ
(ทางเลือก) เพิ่มการอ้างอิงไลบรารี Firebase โดยไม่ใช้ BoM
หากเลือกที่จะไม่ใช้ Firebase BoM คุณต้องระบุเวอร์ชันของไลบรารี Firebase แต่ละรายการ ในบรรทัดทรัพยากร Dependency
โปรดทราบว่าหากคุณใช้ไลบรารี 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:25.0.0") }
- รับคีย์เซิร์ฟเวอร์ของโปรเจ็กต์โดยทำดังนี้
-
ในFirebaseคอนโซล ให้ไปที่
การตั้งค่า > แท็บบัญชีบริการ - ที่ด้านล่างของส่วน Firebase Admin SDK ให้คลิก สร้างคีย์ส่วนตัวใหม่
- ระบบจะบันทึกคู่คีย์สาธารณะ/ส่วนตัวของบัญชีบริการใหม่ไว้ในคอมพิวเตอร์โดยอัตโนมัติ คัดลอกไฟล์นี้ไปยังเซิร์ฟเวอร์การตรวจสอบสิทธิ์
-
ในFirebaseคอนโซล ให้ไปที่
ตรวจสอบสิทธิ์ด้วย Firebase
- ใน
onCreateเมธอดของกิจกรรมการลงชื่อเข้าใช้ ให้รับอินสแตนซ์ที่แชร์ของออบเจ็กต์FirebaseAuthKotlin
private lateinit var auth: FirebaseAuth // ... // Initialize Firebase Auth auth = Firebase.auth
Java
private FirebaseAuth mAuth; // ... // Initialize Firebase Auth mAuth = FirebaseAuth.getInstance();
- เมื่อเริ่มต้นกิจกรรม ให้ตรวจสอบว่าผู้ใช้ลงชื่อเข้าใช้อยู่หรือไม่
Kotlin
public 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); }
- เมื่อผู้ใช้ลงชื่อเข้าใช้แอป ให้ส่งข้อมูลเข้าสู่ระบบการลงชื่อเข้าใช้ (เช่น ชื่อผู้ใช้และรหัสผ่าน) ไปยังเซิร์ฟเวอร์การตรวจสอบสิทธิ์ เซิร์ฟเวอร์ของคุณจะตรวจสอบข้อมูลเข้าสู่ระบบและแสดงโทเค็นที่กำหนดเองหากข้อมูลเข้าสู่ระบบถูกต้อง
- หลังจากได้รับโทเค็นที่กำหนดเองจากเซิร์ฟเวอร์การตรวจสอบสิทธิ์แล้ว ให้ส่งโทเค็นไปยัง
signInWithCustomTokenเพื่อลงชื่อเข้าใช้ผู้ใช้หากลงชื่อเข้าใช้สำเร็จKotlin
customToken?.let { auth.signInWithCustomToken(it) .addOnCompleteListener(this) { task -> if (task.isSuccessful) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInWithCustomToken:success") val user = auth.currentUser updateUI(user) } else { // If sign in fails, display a message to the user. Log.w(TAG, "signInWithCustomToken:failure", task.exception) Toast.makeText( baseContext, "Authentication failed.", Toast.LENGTH_SHORT, ).show() updateUI(null) } } }
Java
mAuth.signInWithCustomToken(mCustomToken) .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, "signInWithCustomToken:success"); FirebaseUser user = mAuth.getCurrentUser(); updateUI(user); } else { // If sign in fails, display a message to the user. Log.w(TAG, "signInWithCustomToken:failure", task.getException()); Toast.makeText(CustomAuthActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); updateUI(null); } } });
AuthStateListenerคุณจะใช้เมธอดgetCurrentUserเพื่อรับข้อมูลบัญชีของผู้ใช้ได้
ขั้นตอนถัดไป
หลังจากที่ผู้ใช้ลงชื่อเข้าใช้เป็นครั้งแรก ระบบจะสร้างบัญชีผู้ใช้ใหม่และ ลิงก์กับข้อมูลเข้าสู่ระบบ ซึ่งได้แก่ ชื่อผู้ใช้และรหัสผ่าน หมายเลขโทรศัพท์ หรือข้อมูลผู้ให้บริการตรวจสอบสิทธิ์ที่ผู้ใช้ลงชื่อเข้าใช้ ระบบจะจัดเก็บบัญชีใหม่นี้เป็นส่วนหนึ่งของโปรเจ็กต์ Firebase และสามารถใช้เพื่อระบุผู้ใช้ในทุกแอปในโปรเจ็กต์ได้ ไม่ว่าผู้ใช้จะลงชื่อเข้าใช้ด้วยวิธีใดก็ตาม
-
ในแอป คุณสามารถรับข้อมูลโปรไฟล์พื้นฐานของผู้ใช้จากออบเจ็กต์
FirebaseUserได้ ดู จัดการผู้ใช้ ใน Firebase Realtime Database และ Cloud Storage กฎความปลอดภัย คุณสามารถ รับรหัสผู้ใช้ที่ไม่ซ้ำของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร
authและใช้รหัสดังกล่าวเพื่อควบคุมข้อมูลที่ผู้ใช้เข้าถึงได้
คุณอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปโดยใช้ผู้ให้บริการการตรวจสอบสิทธิ์หลายรายได้โดยลิงก์ข้อมูลเข้าสู่ระบบของผู้ให้บริการการตรวจสอบสิทธิ์กับบัญชีผู้ใช้ที่มีอยู่
หากต้องการให้ออกจากระบบ ให้โทรหา
signOut
Kotlin
Firebase.auth.signOut()
Java
FirebaseAuth.getInstance().signOut();