เชื่อมต่อแอพของคุณกับ Firebase
หากคุณยังไม่ได้ เพิ่ม Firebase ในโครงการ Android ของคุณ
เพิ่มการตรวจสอบสิทธิ์ Firebase ให้กับแอปของคุณ
ใช้ Firebase Android BoM ประกาศการพึ่งพาสำหรับไลบรารี Firebase Authentication Android ใน ไฟล์ Gradle ของโมดูล (ระดับแอป) (โดยปกติคือapp/build.gradle
) Java
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:30.3.1') // 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:21.0.7' }
Kotlin+KTX
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:30.3.1') // 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:21.0.7' }
หากต้องการใช้ผู้ให้บริการตรวจสอบสิทธิ์ คุณต้องเปิดใช้งานใน คอนโซล Firebase ไปที่หน้าวิธีการลงชื่อเข้าใช้ในส่วนการตรวจสอบสิทธิ์ Firebase เพื่อเปิดใช้งานการลงชื่อเข้าใช้อีเมล/รหัสผ่าน และผู้ให้บริการข้อมูลประจำตัวอื่นๆ ที่คุณต้องการสำหรับแอปของคุณ
(ไม่บังคับ) ต้นแบบและทดสอบด้วย Firebase Local Emulator Suite
ก่อนที่จะพูดถึงวิธีที่แอปของคุณตรวจสอบสิทธิ์ผู้ใช้ เรามาแนะนำชุดเครื่องมือที่คุณสามารถใช้เพื่อสร้างต้นแบบและทดสอบฟังก์ชันการตรวจสอบสิทธิ์: Firebase Local Emulator Suite หากคุณกำลังตัดสินใจเลือกเทคนิคการตรวจสอบสิทธิ์และผู้ให้บริการ ลองใช้โมเดลข้อมูลต่างๆ กับข้อมูลสาธารณะและส่วนตัวโดยใช้ Authentication และ Firebase Security Rules หรือการออกแบบ UI ในการลงชื่อเข้าใช้ต้นแบบ ความสามารถในการทำงานในพื้นที่โดยไม่ต้องใช้บริการสดอาจเป็นแนวคิดที่ดี .
โปรแกรมจำลองการตรวจสอบสิทธิ์เป็นส่วนหนึ่งของ Local Emulator Suite ซึ่งช่วยให้แอปของคุณสามารถโต้ตอบกับเนื้อหาและการกำหนดค่าฐานข้อมูลที่จำลองได้ รวมถึงทรัพยากรโครงการจำลอง (ฟังก์ชัน ฐานข้อมูลอื่นๆ และกฎความปลอดภัย)
การใช้โปรแกรมจำลองการตรวจสอบสิทธิ์มีเพียงไม่กี่ขั้นตอน:
- การเพิ่มบรรทัดโค้ดในการกำหนดค่าการทดสอบของแอปเพื่อเชื่อมต่อกับโปรแกรมจำลอง
- จากรูทของไดเร็กทอรีโปรเจ็กต์ในเครื่องของคุณ ให้รัน
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;
Kotlin+KTX
private lateinit var auth: FirebaseAuth
ใน
onCreate()
ให้เริ่มต้นอินสแตนซ์FirebaseAuth
Java
// Initialize Firebase Auth mAuth = FirebaseAuth.getInstance();
Kotlin+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(); } }
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(); } }
สมัครสมาชิกใหม่
สร้างเมธอด 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); } } });
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) } }
เพิ่มแบบฟอร์มเพื่อลงทะเบียนผู้ใช้ใหม่ด้วยอีเมลและรหัสผ่าน และเรียกวิธีการใหม่นี้เมื่อส่ง คุณสามารถดูตัวอย่างได้ใน ตัวอย่าง การเริ่มต้นอย่างรวดเร็วของเรา
เข้าสู่ระบบผู้ใช้ที่มีอยู่
สร้างวิธีการลงชื่อเข้าใช้ใหม่ซึ่งรับที่อยู่อีเมลและรหัสผ่าน ตรวจสอบความถูกต้อง จากนั้นลงชื่อเข้าใช้ให้ผู้ใช้ด้วย signInWithEmailAndPassword
signIn
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); } } });
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) } }
เพิ่มแบบฟอร์มเพื่อลงชื่อเข้าใช้ผู้ใช้ด้วยอีเมลและรหัสผ่าน และเรียกวิธีการใหม่นี้เมื่อส่ง คุณสามารถดูตัวอย่างได้ใน ตัวอย่าง การเริ่มต้นอย่างรวดเร็วของเรา
เข้าถึงข้อมูลผู้ใช้
หากผู้ใช้ลงชื่อเข้าใช้สำเร็จ คุณสามารถรับข้อมูลบัญชีของพวกเขาได้ทุกเมื่อด้วยเมธอด 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(); }
Kotlin+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 }
ขั้นตอนถัดไป
สำรวจคำแนะนำในการเพิ่มบริการระบุตัวตนและการรับรองความถูกต้องอื่นๆ: