יצירת משתמש
כדי ליצור משתמש חדש בפרויקט Firebase, שולחים אל
CreateUserWithEmailAndPassword
או על ידי כניסה של משתמש בפעם הראשונה באמצעות זהות מאוחדת
ספק, כמו כניסה באמצעות חשבון Google או
התחברות ל-Facebook.
אפשר גם ליצור משתמשים חדשים שמאומתים באמצעות סיסמה דרך 'אימות' בקטע במסוף Firebase בדף 'משתמשים'.
איתור המשתמש שמחובר כרגע
הדרך המומלצת להשיג את המשתמש הנוכחי היא להגדיר האזנה אובייקט אימות:
Firebase.Auth.FirebaseAuth auth; Firebase.Auth.FirebaseUser user; // Handle initialization of the necessary firebase modules: void InitializeFirebase() { Debug.Log("Setting up Firebase Auth"); auth = Firebase.Auth.FirebaseAuth.DefaultInstance; auth.StateChanged += AuthStateChanged; AuthStateChanged(this, null); } // Track state changes of the auth object. void AuthStateChanged(object sender, System.EventArgs eventArgs) { if (auth.CurrentUser != user) { bool signedIn = user != auth.CurrentUser && auth.CurrentUser != null; if (!signedIn && user != null) { Debug.Log("Signed out " + user.UserId); } user = auth.CurrentUser; if (signedIn) { Debug.Log("Signed in " + user.UserId); } } } // Handle removing subscription and reference to the Auth instance. // Automatically called by a Monobehaviour after Destroy is called on it. void OnDestroy() { auth.StateChanged -= AuthStateChanged; auth = null; }
שימוש ב-listener מבטיח שאובייקט האימות לא נמצא בשלב הביניים במצב הזה - כמו אתחול, כשמקבלים את המשתמש הנוכחי.
אפשר גם להתקשר למספר CurrentUser
כדי למצוא את המשתמש שמחובר כרגע. אם
המשתמש לא מחובר, הפונקציה CurrentUser
תחזיר את הערך null. אם משתמש לא מחובר לחשבון,
הפונקציה IsValid()
של המשתמש תחזיר את הערך False.
שמירת פרטי הכניסה של משתמש
פרטי הכניסה של המשתמש יישמרו במאגר המפתחות המקומי אחרי שהמשתמש יתחבר. כדי למחוק את המטמון המקומי של פרטי הכניסה של המשתמש, צריך לבטל את ההתחברות שלו. מאגר המפתחות הוא ספציפי לפלטפורמה:
- פלטפורמות של Apple: Keychain Services.
- Android: Android Keystore.
- Windows: Credential Management API.
- OS X: Keychain Services.
- Linux: libsecret על המשתמש, שצריך להתקין.
טעינת פרופיל של משתמש
כדי לקבל את פרטי הפרופיל של המשתמש, צריך להשתמש בשיטות ה-accessor של מופע
Firebase.Auth.FirebaseUser
לדוגמה:
Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { string name = user.DisplayName; string email = user.Email; System.Uri photo_url = user.PhotoUrl; // 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 User.TokenAsync() instead. string uid = user.UserId; }
אחזור פרטי הפרופיל של משתמש שספציפיים לספק
כדי לקבל את פרטי הפרופיל שאוחזרו מספקי הכניסה שמקושרים למשתמש, משתמשים בשיטה ProviderData
. לדוגמה:
Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { foreach (var profile in user.ProviderData) { // Id of the provider (ex: google.com) string providerId = profile.ProviderId; // UID specific to the provider string uid = profile.UserId; // Name, email address, and profile photo Url string name = profile.DisplayName; string email = profile.Email; System.Uri photoUrl = profile.PhotoUrl; } }
עדכון פרופיל של משתמש
אפשר לעדכן את פרטי הפרופיל הבסיסיים של משתמש – השם המוצג של המשתמש וכתובת ה-URL של תמונת הפרופיל – באמצעות השיטה UpdateUserProfile
. לדוגמה:
Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { Firebase.Auth.UserProfile profile = new Firebase.Auth.UserProfile { DisplayName = "Jane Q. User", PhotoUrl = new System.Uri("https://example.com/jane-q-user/profile.jpg"), }; user.UpdateUserProfileAsync(profile).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("UpdateUserProfileAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("UpdateUserProfileAsync encountered an error: " + task.Exception); return; } Debug.Log("User profile updated successfully."); }); }
איך מגדירים כתובת אימייל של משתמש
אפשר להגדיר את כתובת האימייל של משתמש באמצעות השיטה UpdateEmail
. לדוגמה:
Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { user.UpdateEmailAsync("user@example.com").ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("UpdateEmailAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("UpdateEmailAsync encountered an error: " + task.Exception); return; } Debug.Log("User email updated successfully."); }); }
שליחת הודעת אימות למשתמש
אפשר לשלוח אימייל לאימות כתובת למשתמש עם
אמצעי תשלום אחד (SendEmailVerification
). לדוגמה:
Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { user.SendEmailVerificationAsync().ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("SendEmailVerificationAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("SendEmailVerificationAsync encountered an error: " + task.Exception); return; } Debug.Log("Email sent successfully."); }); }
אפשר להתאים אישית את תבנית האימייל שבה נעשה שימוש בקטע 'אימות' ב- במסוף Firebase, בדף 'תבניות אימייל'. מידע נוסף זמין בקטע תבניות אימייל ב- מרכז העזרה של Firebase.
איך מגדירים סיסמה למשתמש
אפשר להגדיר סיסמה של משתמש באמצעות השיטה UpdatePassword
. לדוגמה:
Firebase.Auth.FirebaseUser user = auth.CurrentUser; string newPassword = "SOME-SECURE-PASSWORD"; if (user != null) { user.UpdatePasswordAsync(newPassword).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("UpdatePasswordAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("UpdatePasswordAsync encountered an error: " + task.Exception); return; } Debug.Log("Password updated successfully."); }); }
שליחת אימייל בנושא איפוס סיסמה
אפשר לשלוח למשתמש אימייל על איפוס סיסמה באמצעות SendPasswordResetEmail
. לדוגמה:
string emailAddress = "user@example.com"; if (user != null) { auth.SendPasswordResetEmailAsync(emailAddress).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("SendPasswordResetEmailAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("SendPasswordResetEmailAsync encountered an error: " + task.Exception); return; } Debug.Log("Password reset email sent successfully."); }); }
אפשר להתאים אישית את תבנית האימייל שבה נעשה שימוש בקטע 'אימות' ב- במסוף Firebase, בדף 'תבניות אימייל'. מידע נוסף זמין בקטע תבניות אימייל ב- מרכז העזרה של Firebase.
אפשר לשלוח אימיילים בנושא איפוס סיסמה גם מהמסוף של Firebase.
מחיקת משתמש
אפשר למחוק חשבון משתמש באמצעות השיטה Delete
. לדוגמה:
Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { user.DeleteAsync().ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("DeleteAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("DeleteAsync encountered an error: " + task.Exception); return; } Debug.Log("User deleted successfully."); }); }
אפשר למחוק משתמשים גם מהקטע 'אימות' של מסוף Firebase, בדף 'משתמשים'.
אימות מחדש של משתמש
פעולות רגישות מסוימות לאבטחה, כמו מחיקת חשבון, להגדיר כתובת אימייל ראשית, וכן החלפת סיסמה – המשתמש צריך להזין נכנסו לאחרונה. אם ביצעתם אחת מהפעולות האלה, והמשתמש נכנס לחשבון לפני יותר מדי זמן, הפעולה נכשלת.
במקרה כזה, צריך לבצע אימות מחדש של המשתמש על ידי קבלת פרטי כניסה חדשים מהמשתמש והעברת פרטי הכניסה אל Reauthenticate
. לדוגמה:
Firebase.Auth.FirebaseUser user = auth.CurrentUser; // Get auth credentials from the user for re-authentication. The example below shows // email and password credentials but there are multiple possible providers, // such as GoogleAuthProvider or FacebookAuthProvider. Firebase.Auth.Credential credential = Firebase.Auth.EmailAuthProvider.GetCredential("user@example.com", "password1234"); if (user != null) { user.ReauthenticateAsync(credential).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("ReauthenticateAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("ReauthenticateAsync encountered an error: " + task.Exception); return; } Debug.Log("User reauthenticated successfully."); }); }
ייבוא חשבונות משתמשים
אפשר לייבא חשבונות משתמשים מקובץ לפרויקט Firebase באמצעות
הפקודה auth:import
ב-CLI של Firebase. לדוגמה:
firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14