ตรวจสอบสิทธิ์ด้วย Firebase บน Android โดยใช้ระบบการตรวจสอบสิทธิ์ที่กำหนดเอง

คุณสามารถผสานรวม Firebase Authentication กับระบบการตรวจสอบสิทธิ์ที่กำหนดเองได้โดย แก้ไขเซิร์ฟเวอร์การตรวจสอบสิทธิ์เพื่อสร้างโทเค็นที่ลงนามแบบกำหนดเองเมื่อผู้ใช้ ลงชื่อเข้าใช้สำเร็จ แอปจะได้รับโทเค็นนี้และใช้เพื่อตรวจสอบสิทธิ์ กับ Firebase

ก่อนเริ่มต้น

  1. เพิ่ม Firebase ลงในโปรเจ็กต์ Android หากยังไม่ได้เพิ่ม
  2. ในไฟล์ 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:34.11.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:24.0.1")
    }
  3. รับคีย์เซิร์ฟเวอร์ของโปรเจ็กต์โดยทำดังนี้
    1. ไปที่หน้าบัญชีบริการ ในการตั้งค่าของโปรเจ็กต์
    2. คลิกสร้างคีย์ส่วนตัวใหม่ที่ด้านล่างของส่วน Firebase Admin SDK ในหน้าบัญชีบริการ
    3. ระบบจะบันทึกคู่คีย์สาธารณะ/ส่วนตัวของบัญชีบริการใหม่ไว้ในคอมพิวเตอร์โดยอัตโนมัติ คัดลอกไฟล์นี้ไปยังเซิร์ฟเวอร์การตรวจสอบสิทธิ์

ตรวจสอบสิทธิ์ด้วย Firebase

  1. ในonCreateเมธอดของกิจกรรมการลงชื่อเข้าใช้ ให้รับอินสแตนซ์ที่แชร์ของออบเจ็กต์ FirebaseAuth

    Kotlin

    private lateinit var auth: FirebaseAuth
    // ...
    // Initialize Firebase Auth
    auth = Firebase.auth

    Java

    private FirebaseAuth mAuth;
    // ...
    // Initialize Firebase Auth
    mAuth = FirebaseAuth.getInstance();
  2. เมื่อเริ่มต้นกิจกรรม ให้ตรวจสอบว่าผู้ใช้ลงชื่อเข้าใช้แล้วหรือไม่

    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);
    }
  3. เมื่อผู้ใช้ลงชื่อเข้าใช้แอป ให้ส่งข้อมูลเข้าสู่ระบบสำหรับการลงชื่อเข้าใช้ (เช่น ชื่อผู้ใช้และรหัสผ่าน) ไปยังเซิร์ฟเวอร์การตรวจสอบสิทธิ์ เซิร์ฟเวอร์ของคุณจะตรวจสอบข้อมูลเข้าสู่ระบบและแสดงโทเค็นที่กำหนดเอง หากข้อมูลเข้าสู่ระบบถูกต้อง
  4. หลังจากได้รับโทเค็นที่กำหนดเองจากเซิร์ฟเวอร์การตรวจสอบสิทธิ์แล้ว ให้ส่งโทเค็นไปยัง 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();