Firebase'de Kullanıcıları Yönetin

Kullanıcı oluşturma

Firebase projenizde CreateUserWithEmailAndPassword yöntemini çağırarak veya Google ile Oturum Açma ya da Facebook Girişi gibi bir birleşik kimlik sağlayıcı kullanarak bir kullanıcının ilk kez oturum açmasını sağlayarak yeni bir kullanıcı oluşturursunuz.

Kullanıcılar sayfasındaki Firebase konsolunun Kimlik Doğrulama bölümünden de şifreyle kimliği doğrulanmış yeni kullanıcılar oluşturabilirsiniz.

Oturum açmış durumdaki kullanıcıyı getir

Geçerli kullanıcıyı edinmenin önerilen yolu, Auth nesnesinde bir işleyici ayarlamaktır:

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;
}

Bir işleyici kullanarak, geçerli kullanıcıyı aldığınızda Auth nesnesinin başlatma gibi bir ara durumda kalmamasını sağlarsınız.

Şu anda oturum açmış olan kullanıcıyı CurrentUser numaralı telefonu arayarak da öğrenebilirsiniz. Kullanıcı oturum açmamışsa CurrentUser, null değerini döndürür. Kullanıcı oturumu kapatırsa kullanıcının IsValid() ayarı "false" (yanlış) değerini döndürür.

Kullanıcının kimlik bilgilerini korumak

Kullanıcının kimlik bilgileri, oturum açtıktan sonra yerel anahtar deposunda depolanır. Kullanıcı kimlik bilgilerinin yerel önbelleği, kullanıcının oturumu kapatılarak silinebilir. Anahtar deposu platforma özgüdür:

Kullanıcının profilini alma

Kullanıcının profil bilgilerini almak için Firebase.Auth.FirebaseUser örneğindeki erişim yöntemlerini kullanın. Örnek:

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;
}

Kullanıcının sağlayıcıya özel profil bilgilerini alma

Bir kullanıcıya bağlı oturum açma sağlayıcılarından alınan profil bilgilerini almak için ProviderData yöntemini kullanın. Örnek:

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;
  }
}

Kullanıcı profilini güncelleme

UpdateUserProfile yöntemini kullanarak kullanıcının temel profil bilgilerini (kullanıcının görünen adı ve profil fotoğrafı URL'si) güncelleyebilirsiniz. Örnek:

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.");
  });
}

Kullanıcının e-posta adresini ayarlama

Kullanıcının e-posta adresini UpdateEmail yöntemiyle ayarlayabilirsiniz. Örnek:

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.");
  });
}

Bir kullanıcıya doğrulama e-postası gönderme

Kullanıcılara SendEmailVerification yöntemini kullanarak adres doğrulama e-postası gönderebilirsiniz. Örnek:

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 konsolunun Kimlik Doğrulama bölümünde kullanılan e-posta şablonunu E-posta Şablonları sayfasından özelleştirebilirsiniz. Firebase Yardım Merkezi'nde E-posta Şablonları'na göz atın.

Kullanıcı şifresi ayarlayın

Kullanıcı şifresini UpdatePassword yöntemiyle ayarlayabilirsiniz. Örnek:

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.");
  });
}

Şifre sıfırlama e-postası gönderin

SendPasswordResetEmail yöntemini kullanarak bir kullanıcıya şifre sıfırlama e-postası gönderebilirsiniz. Örnek:

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 konsolunun Kimlik Doğrulama bölümünde kullanılan e-posta şablonunu E-posta Şablonları sayfasından özelleştirebilirsiniz. Firebase Yardım Merkezi'nde E-posta Şablonları'na göz atın.

Firebase konsolundan şifre sıfırlama e-postaları da gönderebilirsiniz.

Kullanıcı silme

Bir kullanıcı hesabını Delete yöntemini kullanarak silebilirsiniz. Örnek:

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.");
  });
}

Ayrıca, Kullanıcılar sayfasındaki Firebase konsolunun Kimlik Doğrulama bölümünden de kullanıcıları silebilirsiniz.

Kullanıcının kimliğini yeniden doğrulama

Hesap silme, birincil e-posta adresi ayarlama ve şifre değiştirme gibi güvenlik açısından hassas işlemler için kullanıcının kısa süre önce oturum açmış olması gerekir. Bu işlemlerden birini gerçekleştirirseniz ve kullanıcı çok uzun süre önce oturum açmışsa işlem başarısız olur.

Bu durumda, kullanıcıdan yeni oturum açma kimlik bilgilerini alıp kimlik bilgilerini Reauthenticate hizmetine ileterek kullanıcının kimliğini yeniden doğrulayın. Örnek:

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.");
  });
}

Kullanıcı hesaplarını içe aktarma

Firebase CLI'ın auth:import komutunu kullanarak kullanıcı hesaplarını bir dosyadan Firebase projenize aktarabilirsiniz. Örnek:

firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14