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

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

ก่อนที่คุณจะเริ่มต้น

  1. หากคุณยังไม่ได้ ดำเนินการ ให้เพิ่ม Firebase ในโครงการ Android ของคุณ

  2. หากคุณยังไม่ได้เชื่อมต่อแอปกับโปรเจ็กต์ Firebase ให้เชื่อมต่อจาก คอนโซล Firebase
  3. เปิดใช้งานการลงชื่อเข้าใช้ด้วยอีเมล/รหัสผ่าน:
    1. ใน คอนโซล Firebase ให้เปิดส่วน การรับรองความถูกต้อง
    2. บนแท็บ วิธีการลงชื่อเข้า ใช้ ให้เปิดใช้งานวิธีการลงชื่อเข้าใช้ ด้วยอีเมล/รหัสผ่าน แล้วคลิก บันทึก
  4. ใน ไฟล์ Gradle ของโมดูล (ระดับแอป) (โดยปกติคือ <project>/<app-module>/build.gradle.kts หรือ <project>/<app-module>/build.gradle ) เพิ่มการพึ่งพาสำหรับ Firebase Authentication ห้องสมุด Android ขอแนะนำให้ใช้ Firebase Android BoM เพื่อควบคุมการกำหนดเวอร์ชันของไลบรารี

    Kotlin+KTX

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.3.1"))
    
        // 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-ktx")
    }
    

    เมื่อใช้ Firebase Android BoM แอปของคุณจะใช้ไลบรารี Firebase Android เวอร์ชันที่เข้ากันได้เสมอ

    (ทางเลือก) เพิ่มการอ้างอิงไลบรารี Firebase โดยไม่ ใช้ BoM

    หากคุณเลือกที่จะไม่ใช้ Firebase BoM คุณต้องระบุแต่ละเวอร์ชันของไลบรารี Firebase ในบรรทัดอ้างอิง

    โปรดทราบว่าหากคุณใช้ไลบรารี 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-ktx:22.1.2")
    }
    

    Java

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.3.1"))
    
        // 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 ในบรรทัดอ้างอิง

    โปรดทราบว่าหากคุณใช้ไลบรารี 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:22.1.2")
    }
    

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

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

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

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

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

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

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

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

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

ในการออกจากระบบผู้ใช้ โทร signOut :

Kotlin+KTX

Firebase.auth.signOut()

Java

FirebaseAuth.getInstance().signOut();