אתה יכול להשתמש באימות 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.0.2') // 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.4' }
Kotlin+KTX
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:30.0.2') // 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.4' }
- אם עדיין לא חיברת את האפליקציה שלך לפרויקט Firebase שלך, עשה זאת ממסוף Firebase .
- אפשר אימות אנונימי:
- במסוף Firebase , פתח את הקטע Auth .
- בדף שיטות כניסה , הפעל את שיטת הכניסה אנונימית .
אימות עם 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 של המשתמש, אסימון הגישה לפייסבוק או כתובת אימייל וסיסמה. קבל
AuthCredential
עבור ספק האימות החדש:כניסה לגוגל
Java
AuthCredential credential = GoogleAuthProvider.getCredential(googleIdToken, null);
Kotlin+KTX
val credential = GoogleAuthProvider.getCredential(googleIdToken, null)
כניסה לפייסבוק
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-05-20 UTC.
[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "translationIssue", "label":"Translation issue" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"בעיה בדוגמאות/בקוד" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }] [{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]