คุณสามารถใช้การตรวจสอบสิทธิ์ Firebase เพื่อสร้างและใช้บัญชีที่ไม่ระบุชื่อชั่วคราวเพื่อตรวจสอบสิทธิ์กับ Firebase บัญชีที่ไม่ระบุตัวตนชั่วคราวเหล่านี้สามารถใช้เพื่อให้ผู้ใช้ที่ยังไม่ได้สมัครใช้งานแอปของคุณทำงานกับข้อมูลที่ได้รับการคุ้มครองโดยกฎความปลอดภัย หากผู้ใช้ที่ไม่ระบุชื่อตัดสินใจลงชื่อสมัครใช้แอปของคุณ คุณสามารถ เชื่อมโยงข้อมูลรับรองการลงชื่อเข้าใช้ของพวกเขากับบัญชีที่ไม่ระบุตัวตน เพื่อให้พวกเขาสามารถทำงานกับข้อมูลที่ได้รับการป้องกันต่อไปได้ในเซสชันต่อๆ ไป
ก่อนจะเริ่ม
- หากคุณยังไม่ได้ เพิ่ม 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.1.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:21.0.6' }
Kotlin+KTX
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:30.1.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:21.0.6' }
- หากคุณยังไม่ได้เชื่อมต่อแอปกับโปรเจ็กต์ Firebase ให้ดำเนินการจาก คอนโซล Firebase
- เปิดใช้งานการตรวจสอบแบบไม่ระบุชื่อ:
- ใน คอนโซล Firebase ให้เปิดส่วนการ ตรวจสอบสิทธิ์
- ในหน้า วิธีการลงชื่อเข้าใช้ ให้เปิดใช้งานวิธีการลงชื่อเข้าใช้แบบ ไม่ระบุชื่อ
ตรวจสอบสิทธิ์ด้วย Firebase โดยไม่เปิดเผยตัวตน
เมื่อผู้ใช้ที่ออกจากระบบใช้ฟีเจอร์แอปที่ต้องมีการตรวจสอบสิทธิ์ด้วย 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) }
- สุดท้าย โทร
signInAnonymously
เพื่อลงชื่อเข้าใช้ในฐานะผู้ใช้ที่ไม่ระบุชื่อ:หากลงชื่อเข้าใช้สำเร็จ คุณสามารถใช้เมธอดJava
mAuth.signInAnonymously() .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, "signInAnonymously:success"); FirebaseUser user = mAuth.getCurrentUser(); updateUI(user); } else { // If sign in fails, display a message to the user. Log.w(TAG, "signInAnonymously:failure", task.getException()); Toast.makeText(AnonymousAuthActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); updateUI(null); } } });
Kotlin+KTX
auth.signInAnonymously() .addOnCompleteListener(this) { task -> if (task.isSuccessful) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInAnonymously:success") val user = auth.currentUser updateUI(user) } else { // If sign in fails, display a message to the user. Log.w(TAG, "signInAnonymously:failure", task.exception) Toast.makeText(baseContext, "Authentication failed.", Toast.LENGTH_SHORT).show() updateUI(null) } }
getCurrentUser
เพื่อรับข้อมูลบัญชีของผู้ใช้
แปลงบัญชีที่ไม่ระบุตัวตนเป็นบัญชีถาวร
เมื่อผู้ใช้ที่ไม่ระบุชื่อลงชื่อสมัครใช้แอปของคุณ คุณอาจต้องการอนุญาตให้พวกเขาทำงานต่อด้วยบัญชีใหม่ของพวกเขา ตัวอย่างเช่น คุณอาจต้องการให้สินค้าที่ผู้ใช้เพิ่มลงในตะกร้าสินค้าก่อนที่จะลงชื่อสมัครใช้ในใหม่ ตะกร้าสินค้าของบัญชี โดยทำตามขั้นตอนต่อไปนี้:
- เมื่อผู้ใช้ลงชื่อสมัครใช้ ให้ทำตามขั้นตอนการลงชื่อเข้าใช้สำหรับผู้ให้บริการการตรวจสอบสิทธิ์ของผู้ใช้ให้เสร็จสิ้น แต่ไม่รวมการเรียกหนึ่งในวิธี
FirebaseAuth.signInWith
ตัวอย่างเช่น รับโทเค็น Google ID ของผู้ใช้ โทเค็นการเข้าถึง Facebook หรือที่อยู่อีเมลและรหัสผ่าน รับ
AuthCredential
สำหรับผู้ให้บริการตรวจสอบสิทธิ์ใหม่:ลงชื่อเข้าใช้ Google
Java
AuthCredential credential = GoogleAuthProvider.getCredential(googleIdToken, null);
Kotlin+KTX
val credential = GoogleAuthProvider.getCredential(googleIdToken, null)
เข้าสู่ระบบ Facebook
Java
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
Kotlin+KTX
val credential = FacebookAuthProvider.getCredential(token.token)
อีเมลรหัสผ่านลงชื่อเข้าใช้
Java
AuthCredential credential = EmailAuthProvider.getCredential(email, password);
Kotlin+KTX
val credential = EmailAuthProvider.getCredential(email, password)
ส่งผ่านอ็อบเจ็กต์
AuthCredential
ไปยังเมธอดlinkWithCredential
ของผู้ใช้ที่ลงชื่อเข้าใช้:Java
mAuth.getCurrentUser().linkWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { Log.d(TAG, "linkWithCredential:success"); FirebaseUser user = task.getResult().getUser(); updateUI(user); } else { Log.w(TAG, "linkWithCredential:failure", task.getException()); Toast.makeText(AnonymousAuthActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); updateUI(null); } } });
Kotlin+KTX
auth.currentUser!!.linkWithCredential(credential) .addOnCompleteListener(this) { task -> if (task.isSuccessful) { Log.d(TAG, "linkWithCredential:success") val user = task.result?.user updateUI(user) } else { Log.w(TAG, "linkWithCredential:failure", task.exception) Toast.makeText(baseContext, "Authentication failed.", Toast.LENGTH_SHORT).show() updateUI(null) } }
หากการเรียก
linkWithCredential
สำเร็จ บัญชีใหม่ของผู้ใช้จะสามารถเข้าถึงข้อมูล Firebase ของบัญชีที่ไม่ระบุตัวตนได้ขั้นตอนถัดไป
ขณะนี้ผู้ใช้สามารถตรวจสอบสิทธิ์กับ Firebase ได้แล้ว คุณสามารถควบคุมการเข้าถึงข้อมูลในฐานข้อมูล Firebase ได้โดยใช้ กฎของ Firebase
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-06-30 UTC.
[] []