คุณสามารถใช้การตรวจสอบสิทธิ์ Firebase เพื่อให้ผู้ใช้ตรวจสอบสิทธิ์กับ Firebase โดยใช้ที่อยู่อีเมลและรหัสผ่าน และเพื่อจัดการแอปของคุณ บัญชีที่ใช้รหัสผ่าน
ก่อนเริ่มต้น
หากคุณยังไม่ได้ดำเนินการ เพิ่ม Firebase ลงในโปรเจ็กต์ Android
- หากยังไม่ได้เชื่อมต่อแอปกับโปรเจ็กต์ Firebase ให้ดำเนินการจาก คอนโซล Firebase
- เปิดใช้งานการลงชื่อเข้าใช้ด้วยอีเมล/รหัสผ่าน:
- ในคอนโซล Firebase ให้เปิด ส่วน Auth
- ในแท็บวิธีการลงชื่อเข้าใช้ ให้เปิดใช้การลงชื่อเข้าใช้อีเมล/รหัสผ่าน และคลิกบันทึก
ในไฟล์ Gradle ของโมดูล (ระดับแอป) (ปกติ
<project>/<app-module>/build.gradle.kts
หรือ<project>/<app-module>/build.gradle
) เพิ่มทรัพยากร Dependency สำหรับไลบรารีการตรวจสอบสิทธิ์ Firebase สำหรับ Android เราขอแนะนำให้ใช้ BoM ของ Firebase Android เพื่อควบคุมการกำหนดเวอร์ชันไลบรารีdependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:33.1.2")) // 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") }
สร้างบัญชีที่ใช้รหัสผ่าน
หากต้องการสร้างบัญชีผู้ใช้ใหม่ด้วยรหัสผ่าน ให้ทำตามขั้นตอนต่อไปนี้ใน กิจกรรมการลงชื่อเข้าใช้ของแอป
- ในเมธอด
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();
- เมื่อเริ่มกิจกรรม ให้ตรวจสอบว่าผู้ใช้ลงชื่อเข้าใช้อยู่หรือไม่
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(); } }
- เมื่อผู้ใช้ใหม่ลงชื่อสมัครใช้โดยใช้แบบฟอร์มลงชื่อสมัครใช้ของแอป ให้กรอก ขั้นตอนการตรวจสอบบัญชีที่แอปของคุณต้องการ เช่น การยืนยันว่า รหัสผ่านของบัญชีใหม่ได้พิมพ์อย่างถูกต้องและตรงกับความซับซ้อนของคุณ
- สร้างบัญชีใหม่ด้วยการส่งอีเมลและรหัสผ่านของผู้ใช้ใหม่
ถึง
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
เพื่อรับข้อมูลบัญชีของผู้ใช้
ลงชื่อเข้าใช้ผู้ใช้ด้วยอีเมลและรหัสผ่าน
ขั้นตอนในการลงชื่อเข้าใช้ให้ผู้ใช้ด้วยรหัสผ่านคล้ายกับขั้นตอนสำหรับ การสร้างบัญชีใหม่ ในกิจกรรมการลงชื่อเข้าใช้ของแอป
- ในเมธอด
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();
- เมื่อเริ่มกิจกรรม ให้ตรวจสอบว่าผู้ใช้ลงชื่อเข้าใช้อยู่หรือไม่
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(); } }
- เมื่อผู้ใช้ลงชื่อเข้าใช้แอป ให้ส่งอีเมลของผู้ใช้และ
รหัสผ่านไปยัง
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: ตรวจสอบว่าแอปของคุณ
ไม่ได้ขึ้นอยู่กับข้อผิดพลาดที่เจาะจงมากกว่า
ขั้นตอนถัดไป
หลังจากผู้ใช้ลงชื่อเข้าใช้เป็นครั้งแรก ระบบจะสร้างบัญชีผู้ใช้ใหม่ และ ซึ่งก็คือชื่อผู้ใช้และรหัสผ่าน โทรศัพท์ หมายเลข หรือข้อมูลของผู้ให้บริการตรวจสอบสิทธิ์ ซึ่งก็คือผู้ใช้ที่ลงชื่อเข้าใช้ ใหม่นี้ จัดเก็บเป็นส่วนหนึ่งของโปรเจ็กต์ Firebase และสามารถใช้เพื่อระบุ ผู้ใช้สำหรับทุกแอปในโปรเจ็กต์ของคุณ ไม่ว่าผู้ใช้จะลงชื่อเข้าใช้ด้วยวิธีใดก็ตาม
-
ในแอป คุณสามารถดูข้อมูลโปรไฟล์พื้นฐานของผู้ใช้ได้จาก
FirebaseUser
โปรดดู จัดการผู้ใช้ ในฐานข้อมูลเรียลไทม์และ Cloud Storage ของ Firebase กฎความปลอดภัย คุณสามารถทำสิ่งต่อไปนี้ รับรหัสผู้ใช้ที่ไม่ซ้ำของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร
auth
และใช้เพื่อควบคุมข้อมูลที่ผู้ใช้เข้าถึงได้
คุณอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปโดยใช้การตรวจสอบสิทธิ์หลายรายการได้ โดยลิงก์ข้อมูลเข้าสู่ระบบของผู้ให้บริการการตรวจสอบสิทธิ์กับ บัญชีผู้ใช้ที่มีอยู่เดิม
หากต้องการนำผู้ใช้ออกจากระบบ โปรดโท��
signOut
Kotlin+KTX
Firebase.auth.signOut()
Java
FirebaseAuth.getInstance().signOut();