ก่อนจะเริ่ม
- หากคุณยังไม่ได้ เพิ่ม Firebase ในโครงการ Android ของคุณ
- ใช้ 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 Admin SDK ของหน้า บัญชีบริการ
- คู่คีย์สาธารณะ/ส่วนตัวของบัญชีบริการใหม่จะถูกบันทึกไว้ในคอมพิวเตอร์ของคุณโดยอัตโนมัติ คัดลอกไฟล์นี้ไปยังเซิร์ฟเวอร์การตรวจสอบสิทธิ์ของคุณ
ตรวจสอบสิทธิ์ด้วย Firebase
- ในวิธีการ
onCreate
ของกิจกรรมการลงชื่อเข้าใช้ รับอินสแตนซ์ที่ใช้ร่วมกันของวัตถุFirebaseAuth
:Java
private FirebaseAuth mAuth; // ... // Initialize Firebase Auth mAuth = FirebaseAuth.getInstance();
Kotlin+KTX
private lateinit var auth: FirebaseAuth // ... // 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(); updateUI(currentUser); }
Kotlin+KTX
public override fun onStart() { super.onStart() // Check if user is signed in (non-null) and update UI accordingly. val currentUser = auth.currentUser updateUI(currentUser) }
- เมื่อผู้ใช้ลงชื่อเข้าใช้แอปของคุณ ให้ส่งข้อมูลรับรองการลงชื่อเข้าใช้ (เช่น ชื่อผู้ใช้และรหัสผ่าน) ไปยังเซิร์ฟเวอร์การตรวจสอบสิทธิ์ของคุณ เซิร์ฟเวอร์ของคุณตรวจสอบข้อมูลประจำตัวและส่งคืน โทเค็นที่กำหนดเอง หากถูกต้อง
- หลังจากที่คุณได้รับโทเค็นที่กำหนดเองจากเซิร์ฟเวอร์การตรวจสอบสิทธิ์ของคุณแล้ว ให้ส่งต่อไปยัง
signInWithCustomToken
เพื่อลงชื่อเข้าใช้ผู้ใช้:หากลงชื่อเข้าใช้สำเร็จJava
mAuth.signInWithCustomToken(mCustomToken) .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, "signInWithCustomToken:success"); FirebaseUser user = mAuth.getCurrentUser(); updateUI(user); } else { // If sign in fails, display a message to the user. Log.w(TAG, "signInWithCustomToken:failure", task.getException()); Toast.makeText(CustomAuthActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); updateUI(null); } } });
Kotlin+KTX
customToken?.let { auth.signInWithCustomToken(it) .addOnCompleteListener(this) { task -> if (task.isSuccessful) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInWithCustomToken:success") val user = auth.currentUser updateUI(user) } else { // If sign in fails, display a message to the user. Log.w(TAG, "signInWithCustomToken:failure", task.exception) Toast.makeText(baseContext, "Authentication failed.", Toast.LENGTH_SHORT).show() updateUI(null) } } }
AuthStateListener
คุณสามารถใช้เมธอดgetCurrentUser
เพื่อรับข้อมูลบัญชีของผู้ใช้ได้
ขั้นตอนถัดไป
หลังจากที่ผู้ใช้ลงชื่อเข้าใช้เป็นครั้งแรก บัญชีผู้ใช้ใหม่จะถูกสร้างขึ้นและเชื่อมโยงกับข้อมูลประจำตัว กล่าวคือ ชื่อผู้ใช้และรหัสผ่าน หมายเลขโทรศัพท์ หรือข้อมูลผู้ให้บริการตรวจสอบสิทธิ์ ซึ่งผู้ใช้ลงชื่อเข้าใช้ บัญชีใหม่นี้จัดเก็บเป็นส่วนหนึ่งของโปรเจ็กต์ Firebase และใช้เพื่อระบุผู้ใช้ในทุกแอปในโปรเจ็กต์ของคุณ ไม่ว่าผู้ใช้จะลงชื่อเข้าใช้ด้วยวิธีใดก็ตาม
ในแอปของคุณ คุณสามารถรับข้อมูลโปรไฟล์พื้นฐานของผู้ใช้จากอ็อบเจ็กต์
FirebaseUser
ดู จัดการผู้ใช้ในฐานข้อมูล Firebase Realtime Database และ Cloud Storage Security Rules คุณสามารถรับ ID ผู้ใช้เฉพาะของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร
auth
และใช้เพื่อควบคุมข้อมูลที่ผู้ใช้สามารถเข้าถึงได้
คุณสามารถอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปของคุณโดยใช้ผู้ให้บริการตรวจสอบสิทธิ์หลายรายโดย เชื่อมโยงข้อมูลรับรองของผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีผู้ใช้ที่มีอยู่
หากต้องการออกจากระบบผู้ใช้ ให้โทร
signOut
:Java
FirebaseAuth.getInstance().signOut();
Kotlin+KTX
Firebase.auth.signOut()
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2022-08-09 UTC.
[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"ไม่มีข้อมูลที่ฉันต้องการ" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป" },{ "type": "thumb-down", "id": "outOfDate", "label":"ล้าสมัย" },{ "type": "thumb-down", "id": "translationIssue", "label":"ปัญหาเกี่ยวกับการแปล" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด" },{ "type": "thumb-down", "id": "otherDown", "label":"อื่นๆ" }] [{ "type": "thumb-up", "id": "easyToUnderstand", "label":"เข้าใจง่าย" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"แก้ปัญหาของฉันได้" },{ "type": "thumb-up", "id": "otherUp", "label":"อื่นๆ" }] - รับคีย์เซิร์ฟเวอร์ของโปรเจ็กต์ของคุณ: