เชื่อมต่อแอปของคุณกับ Firebase
เพิ่ม Firebase ในโปรเจ็กต์ Android ของคุณ หากยังไม่ได้ ทำ
เพิ่มการตรวจสอบสิทธิ์ Firebase ในแอปของคุณ
ใช้ Firebase Android BoM ประกาศการอ้างอิงสำหรับไลบรารี Android ของ Firebase Authentication ใน โมดูล ของคุณ (ระดับแอป) ไฟล์ Gradle (โดยทั่วไปคือapp/build.gradle
) Java
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:26.6.0') // Declare 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 { // Declare 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:20.0.3' }
โคตรลิน + KTX
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:26.6.0') // Declare 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 { // Declare 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:20.0.3' }
หากต้องการใช้ผู้ให้บริการการตรวจสอบสิทธิ์คุณต้องเปิดใช้งานใน คอนโซล Firebase ไปที่หน้าวิธีการลงชื่อเข้าใช้ในส่วนการตรวจสอบสิทธิ์ Firebase เพื่อเปิดใช้งานการลงชื่อเข้าใช้อีเมล / รหัสผ่านและผู้ให้บริการข้อมูลประจำตัวอื่น ๆ ที่คุณต้องการสำหรับแอปของคุณ
(ไม่บังคับ) สร้างต้นแบบและทดสอบกับ Firebase Local Emulator Suite
ก่อนที่จะพูดถึงวิธีที่แอปของคุณตรวจสอบสิทธิ์ผู้ใช้เรามาแนะนำชุดเครื่องมือที่คุณสามารถใช้ในการสร้างต้นแบบและทดสอบฟังก์ชันการตรวจสอบสิทธิ์: Firebase Local Emulator Suite หากคุณกำลังตัดสินใจเลือกใช้เทคนิคการพิสูจน์ตัวตนและผู้ให้บริการการลองใช้โมเดลข้อมูลที่แตกต่างกันโดยใช้ข้อมูลสาธารณะและข้อมูลส่วนตัวโดยใช้การตรวจสอบสิทธิ์และกฎความปลอดภัย Firebase หรือการออกแบบ UI การลงชื่อเข้าใช้ต้นแบบการสามารถทำงานในเครื่องโดยไม่ต้องใช้บริการสดอาจเป็นความคิดที่ดี .
ตัวจำลองการตรวจสอบความถูกต้องเป็นส่วนหนึ่งของ Local Emulator Suite ซึ่งช่วยให้แอปของคุณสามารถโต้ตอบกับเนื้อหาและการกำหนดค่าฐานข้อมูลที่จำลองรวมถึงทรัพยากรโครงการที่จำลองของคุณได้ (ฟังก์ชันฐานข้อมูลอื่น ๆ และกฎความปลอดภัย) โปรดทราบว่า Local Emulator Suite ยังไม่รองรับ Cloud Storage ที่จำลอง
การใช้ตัวจำลองการตรวจสอบความถูกต้องมีเพียงไม่กี่ขั้นตอน:
- การเพิ่มบรรทัดรหัสในการกำหนดค่าการทดสอบของแอปของคุณเพื่อเชื่อมต่อกับโปรแกรมจำลอง
- จากรูทของไดเร็กทอรีโปรเจ็กต์ในเครื่องของคุณการเรียกใช้
firebase emulators:start
- การใช้ Local Emulator Suite UI สำหรับการสร้างต้นแบบแบบโต้ตอบหรือ Authentication emulator REST API สำหรับการทดสอบแบบไม่โต้ตอบ
คำแนะนำโดยละเอียดมีอยู่ที่ Connect your app to the Authentication emulator . สำหรับข้อมูลเพิ่มเติมโปรดดู บทนำ Local Emulator Suite
ตอนนี้มาดูวิธีพิสูจน์ตัวตนผู้ใช้กันต่อ
ตรวจสอบสถานะการรับรองความถูกต้องในปัจจุบัน
ประกาศอินสแตนซ์ของ
FirebaseAuth
Java
private FirebaseAuth mAuth;
โคตรลิน + KTX
private lateinit var auth: FirebaseAuth
ใน
onCreate()
เริ่มต้นอินสแตนซ์FirebaseAuth
Java
// Initialize Firebase Auth mAuth = FirebaseAuth.getInstance();
โคตรลิน + KTX
// Initialize Firebase Auth auth = Firebase.auth
เมื่อเริ่มต้นกิจกรรมของคุณให้ตรวจสอบว่าผู้ใช้ลงชื่อเข้าใช้อยู่หรือไม่
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(); } }
โคตรลิน + 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(); } }
ลงทะเบียนผู้ใช้ใหม่
สร้างเมธอด createAccount
ใหม่ที่ใช้ที่อยู่อีเมลและรหัสผ่านตรวจสอบความถูกต้องจากนั้นสร้างผู้ใช้ใหม่ด้วยเมธอด createUserWithEmailAndPassword
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); } // ... } });
โคตรลิน + 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) } // ... }
เพิ่มแบบฟอร์มเพื่อลงทะเบียนผู้ใช้ใหม่ด้วยอีเมลและรหัสผ่านและเรียกใช้วิธีการใหม่นี้เมื่อมีการส่ง คุณสามารถดูตัวอย่างได้ใน ตัวอย่างการเริ่มต้นอย่างรวดเร็ว ของเรา
ลงชื่อเข้าใช้ผู้ใช้ที่มีอยู่
สร้างวิธีการ signIn
ใหม่ซึ่งใช้ที่อยู่อีเมลและรหัสผ่านตรวจสอบความถูกต้องจากนั้นลงชื่อผู้ใช้ด้วยเมธอด signInWithEmailAndPassword
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); // ... } // ... } });
โคตรลิน + 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) // ... } // ... }
เพิ่มแบบฟอร์มเพื่อลงชื่อเข้าใช้ผู้ใช้ด้วยอีเมลและรหัสผ่านและเรียกใช้วิธีการใหม่นี้เมื่อมีการส่ง คุณสามารถดูตัวอย่างใน ตัวอย่างการเริ่มต้นอย่างรวดเร็ว ของเรา
เข้าถึงข้อมูลผู้ใช้
หากผู้ใช้ลงชื่อเข้าใช้สำเร็จคุณสามารถรับข้อมูลบัญชีของพวกเขาได้ทุกเมื่อด้วยเมธอด getCurrentUser
Java
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user != null) { // Name, email address, and profile photo Url String name = user.getDisplayName(); String email = user.getEmail(); Uri photoUrl = user.getPhotoUrl(); // Check if user's email is verified boolean emailVerified = user.isEmailVerified(); // The user's ID, unique to the Firebase project. Do NOT use this value to // authenticate with your backend server, if you have one. Use // FirebaseUser.getIdToken() instead. String uid = user.getUid(); }
โคตรลิน + KTX
val user = Firebase.auth.currentUser user?.let { // Name, email address, and profile photo Url val name = user.displayName val email = user.email val photoUrl = user.photoUrl // Check if user's email is verified val emailVerified = user.isEmailVerified // The user's ID, unique to the Firebase project. Do NOT use this value to // authenticate with your backend server, if you have one. Use // FirebaseUser.getToken() instead. val uid = user.uid }
ขั้นตอนถัดไป
สำรวจคำแนะนำเกี่ยวกับการเพิ่มข้อมูลประจำตัวและบริการการพิสูจน์ตัวตนอื่น ๆ :