ตรวจสอบสิทธิ์ด้วย 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.13.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 เวอร์ชันที่เข้ากันได้อยู่เสมอ

    (อีกวิธีหนึ่ง)  เพิ่มทรัพยากร Dependency ของไลบรารี 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.1.0")
    }
  3. รับคีย์เซิร์ฟเวอร์ของโปรเจ็กต์โดยทำดังนี้
    1. ในคอนโซล Firebase ให้ไปที่แท็บ การตั้งค่า > บัญชีบริการ
    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();