เชื่อมต่อแอปกับ Firebase
เพิ่ม Firebase ลงในโปรเจ็กต์ Android หากยังไม่ได้เพิ่ม
เพิ่ม Firebase Authentication ลงในแอป
ในไฟล์ 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:34.13.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:24.1.0") }
หากต้องการใช้ผู้ให้บริการการตรวจสอบสิทธิ์ คุณต้องเปิดใช้ใน Firebase คอนโซล ไปที่หน้าวิธีการลงชื่อเข้าใช้ในส่วนFirebase Authentication เพื่อเปิดใช้การลงชื่อเข้าใช้ด้วยอีเมล/รหัสผ่านและผู้ให้บริการข้อมูลประจำตัวอื่นๆ ที่คุณต้องการสำหรับแอป
(ไม่บังคับ) สร้างต้นแบบและทดสอบด้วย Firebase Local Emulator Suite
ก่อนจะพูดถึงวิธีที่แอปตรวจสอบสิทธิ์ผู้ใช้ เรามาแนะนำชุด เครื่องมือที่คุณใช้สร้างต้นแบบและทดสอบ Authentication ฟังก์ชันการทำงานได้: Firebase Local Emulator Suite หากคุณกำลังตัดสินใจเลือกเทคนิคและผู้ให้บริการการตรวจสอบสิทธิ์ ลองใช้โมเดลข้อมูลต่างๆ กับข้อมูลสาธารณะและข้อมูลส่วนตัว โดยใช้ Authentication และ Firebase Security Rules หรือสร้างต้นแบบการออกแบบ UI การลงชื่อเข้าใช้ การทำงานในเครื่องโดยไม่ต้องติดตั้งใช้งานบริการจริงอาจเป็นความคิดที่ดี
โปรแกรมจำลองAuthenticationเป็นส่วนหนึ่งของLocal Emulator Suiteซึ่ง ช่วยให้แอปโต้ตอบกับเนื้อหาและข้อมูลกำหนดค่าของฐานข้อมูลที่จำลองขึ้น รวมถึง ทรัพยากรของโปรเจ็กต์ที่จำลองขึ้น (ฟังก์ชัน ฐานข้อมูลอื่นๆ และกฎการรักษาความปลอดภัย) ได้ด้วย
การใช้โปรแกรมจำลอง Authentication มีขั้นตอนเพียงไม่กี่ขั้นตอน ดังนี้
- เพิ่มบรรทัดโค้ดลงในข้อมูลกำหนดค่าการทดสอบของแอปเพื่อเชื่อมต่อกับโปรแกรมจำลอง
- เรียกใช้
firebase emulators:startจากไดเรกทอรีรากของโปรเจ็กต์ที่อยู่ในเครื่อง - ใช้ Local Emulator Suite UI เพื่อสร้างต้นแบบแบบอินเทอร์แอกทีฟ หรือใช้ Authentication emulator REST API เพื่อการทดสอบแบบไม่โต้ตอบ
ดูคำแนะนำโดยละเอียดได้ที่หัวข้อ เชื่อมต่อแอปกับโปรแกรมจำลองAuthentication ดูข้อมูลเพิ่มเติมได้ที่หัวข้อLocal Emulator Suiteข้อมูลเบื้องต้น
ตอนนี้เรามาดูวิธีตรวจสอบสิทธิ์ผู้ใช้กันต่อ
ตรวจสอบสถานะการตรวจสอบสิทธิ์ปัจจุบัน
ประกาศอินสแตนซ์ของ
FirebaseAuthKotlin
private lateinit var auth: FirebaseAuth
Java
private FirebaseAuth mAuth;
ในเมธอด
onCreate()ให้เริ่มต้นอินสแตนซ์FirebaseAuthKotlin
// Initialize Firebase Auth auth = Firebase.auth
Java
// Initialize Firebase Auth mAuth = FirebaseAuth.getInstance();
เมื่อเริ่มต้นกิจกรรม ให้ตรวจสอบว่าผู้ใช้ลงชื่อเข้าใช้อยู่หรือไม่
Kotlin
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(); } }
ลงชื่อสมัครใช้ผู้ใช้ใหม่
สร้างเมธอด createAccount ใหม่ที่รับอีเมลและรหัสผ่าน
ตรวจสอบความถูกต้อง แล้วสร้างผู้ใช้ใหม่ด้วย
createUserWithEmailAndPassword
เมธอด
Kotlin
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); } } });
เพิ่มแบบฟอร์มเพื่อลงทะเบียนผู้ใช้ใหม่ด้วยอีเมลและรหัสผ่าน แล้วเรียกเมธอดใหม่นี้เมื่อมีการส่งแบบฟอร์ม ดูตัวอย่างได้ในตัวอย่างคู่มือเริ่มใช้งานฉบับย่อของเรา quickstart sample
ลงชื่อเข้าใช้ผู้ใช้ที่มีอยู่
สร้างเมธอด signIn ใหม่ที่รับอีเมลและรหัสผ่าน
ตรวจสอบความถูกต้อง แล้วลงชื่อเข้าใช้ผู้ใช้ด้วยเมธอด
signInWithEmailAndPassword
Kotlin
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); } } });
เพิ่มแบบฟอร์มเพื่อลงชื่อเข้าใช้ผู้ใช้ด้วยอีเมลและรหัสผ่าน แล้วเรียกเมธอดใหม่นี้เมื่อมีการส่งแบบฟอร์ม ดูตัวอย่างได้ในตัวอย่างคู่มือเริ่มใช้งานฉบับย่อของเรา quickstart sample
เข้าถึงข้อมูลผู้ใช้
หากผู้ใช้ลงชื่อเข้าใช้สำเร็จ คุณจะรับข้อมูลบัญชีของผู้ใช้ได้ทุกเมื่อด้วยเมธอด getCurrentUser
Kotlin
val user = Firebase.auth.currentUser user?.let { // Name, email address, and profile photo Url val name = it.displayName val email = it.email val photoUrl = it.photoUrl // Check if user's email is verified val emailVerified = it.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. val uid = it.uid }
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(); }
ขั้นตอนถัดไป
ดูคำแนะนำเกี่ยวกับการเพิ่มบริการข้อมูลประจำตัวและการตรวจสอบสิทธิ์อื่นๆ ได้ที่