ตรวจสอบสิทธิ์ด้วย Firebase โดยใช้บัญชีแบบใช้รหัสผ่านใน Android

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

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

  1. หากคุณยังไม่ได้ดำเนินการ เพิ่ม Firebase ลงในโปรเจ็กต์ Android

  2. หากยังไม่ได้เชื่อมต่อแอปกับโปรเจ็กต์ Firebase ให้ดำเนินการจาก คอนโซล Firebase
  3. เปิดใช้งานการลงชื่อเข้าใช้ด้วยอีเมล/รหัสผ่าน:
    1. ในคอนโซล Firebase ให้เปิด ส่วน Auth
    2. ในแท็บวิธีการลงชื่อเข้าใช้ ให้เปิดใช้การลงชื่อเข้าใช้อีเมล/รหัสผ่าน และคลิกบันทึก
  4. ในไฟล์ 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:33.2.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:23.0.0")
    }
    
    หากกำลังมองหาโมดูลไลบรารีสำหรับ Kotlin โดยเฉพาะ จะเริ่มต้นใน ตุลาคม 2023 (Firebase BoM 32.5.0) ทั้งนักพัฒนา Kotlin และ Java สามารถ ขึ้นอยู่กับโมดูลไลบรารีหลัก (ดูรายละเอียดได้ที่ คําถามที่พบบ่อยเกี่ยวกับโครงการริเริ่มนี้)

สร้างบัญชีที่ใช้รหัสผ่าน

หากต้องการสร้างบัญชีผู้ใช้ใหม่ด้วยรหัสผ่าน ให้ทำตามขั้นตอนต่อไปนี้ใน กิจกรรมการลงชื่อเข้าใช้ของแอป

  1. ในเมธอด 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();
  2. เมื่อเริ่มกิจกรรม ให้ตรวจสอบว่าผู้ใช้ลงชื่อเข้าใช้อยู่หรือไม่

    Kotlin+KTX

    public override fun onStart() {
        super.onStart()
        // Check if user is signed in (non-null) and update UI accordingly.
        val currentUser = auth.currentUser
        if (currentUser != null) {
            reload()
        }
    }

    Java

    @Override
    public void onStart() {
        super.onStart();
        // Check if user is signed in (non-null) and update UI accordingly.
        FirebaseUser currentUser = mAuth.getCurrentUser();
        if(currentUser != null){
            reload();
        }
    }
  3. เมื่อผู้ใช้ใหม่ลงชื่อสมัครใช้โดยใช้แบบฟอร์มลงชื่อสมัครใช้ของแอป ให้กรอก ขั้นตอนการตรวจสอบบัญชีที่แอปของคุณต้องการ เช่น การยืนยันว่า รหัสผ่านของบัญชีใหม่ได้พิมพ์อย่างถูกต้องและตรงกับความซับซ้อนของคุณ
  4. สร้างบัญชีใหม่ด้วยการส่งอีเมลและรหัสผ่านของผู้ใช้ใหม่ ถึง createUserWithEmailAndPassword:

    Kotlin+KTX

    auth.createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                // Sign in success, update UI with the signed-in user's information
                Log.d(TAG, "createUserWithEmail:success")
                val user = auth.currentUser
                updateUI(user)
            } else {
                // If sign in fails, display a message to the user.
                Log.w(TAG, "createUserWithEmail:failure", task.exception)
                Toast.makeText(
                    baseContext,
                    "Authentication failed.",
                    Toast.LENGTH_SHORT,
                ).show()
                updateUI(null)
            }
        }

    Java

    mAuth.createUserWithEmailAndPassword(email, password)
            .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, "createUserWithEmail:success");
                        FirebaseUser user = mAuth.getCurrentUser();
                        updateUI(user);
                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "createUserWithEmail:failure", task.getException());
                        Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                        updateUI(null);
                    }
                }
            });
    หากสร้างบัญชีใหม่ ผู้ใช้จะลงชื่อเข้าใช้ด้วย ใน Callback ให้ใช้เมธอด getCurrentUser เพื่อรับข้อมูลบัญชีของผู้ใช้

ลงชื่อเข้าใช้ผู้ใช้ด้วยอีเมลและรหัสผ่าน

ขั้นตอนในการลงชื่อเข้าใช้ให้ผู้ใช้ด้วยรหัสผ่านคล้ายกับขั้นตอนสำหรับ การสร้างบัญชีใหม่ ในกิจกรรมการลงชื่อเข้าใช้ของแอป

  1. ในเมธอด 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();
  2. เมื่อเริ่มกิจกรรม ให้ตรวจสอบว่าผู้ใช้ลงชื่อเข้าใช้อยู่หรือไม่

    Kotlin+KTX

    public override fun onStart() {
        super.onStart()
        // Check if user is signed in (non-null) and update UI accordingly.
        val currentUser = auth.currentUser
        if (currentUser != null) {
            reload()
        }
    }

    Java

    @Override
    public void onStart() {
        super.onStart();
        // Check if user is signed in (non-null) and update UI accordingly.
        FirebaseUser currentUser = mAuth.getCurrentUser();
        if(currentUser != null){
            reload();
        }
    }
  3. เมื่อผู้ใช้ลงชื่อเข้าใช้แอป ให้ส่งอีเมลของผู้ใช้และ รหัสผ่านไปยัง signInWithEmailAndPassword:

    Kotlin+KTX

    auth.signInWithEmailAndPassword(email, password)
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                // Sign in success, update UI with the signed-in user's information
                Log.d(TAG, "signInWithEmail:success")
                val user = auth.currentUser
                updateUI(user)
            } else {
                // If sign in fails, display a message to the user.
                Log.w(TAG, "signInWithEmail:failure", task.exception)
                Toast.makeText(
                    baseContext,
                    "Authentication failed.",
                    Toast.LENGTH_SHORT,
                ).show()
                updateUI(null)
            }
        }

    Java

    mAuth.signInWithEmailAndPassword(email, password)
            .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, "signInWithEmail:success");
                        FirebaseUser user = mAuth.getCurrentUser();
                        updateUI(user);
                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "signInWithEmail:failure", task.getException());
                        Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                        updateUI(null);
                    }
                }
            });
    หากลงชื่อเข้าใช้สำเร็จ คุณจะใช้ FirebaseUser ที่ส่งคืนแล้วเพื่อดำเนินการต่อได้

แนะนำ: เปิดใช้การป้องกันการนับอีเมล

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

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

ขั้นตอนถัดไป

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

  • ในแอป คุณสามารถดูข้อมูลโปรไฟล์พื้นฐานของผู้ใช้ได้จาก FirebaseUser โปรดดู จัดการผู้ใช้

  • ในFirebase Realtime DatabaseและCloud Storage กฎความปลอดภัย คุณสามารถทำสิ่งต่อไปนี้ รับรหัสผู้ใช้ที่ไม่ซ้ำของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร auth และใช้เพื่อควบคุมข้อมูลที่ผู้ใช้เข้าถึงได้

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

หากต้องการนำผู้ใช้ออกจากระบบ โปรดโทร signOut

Kotlin+KTX

Firebase.auth.signOut()

Java

FirebaseAuth.getInstance().signOut();