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

Kullanıcı oluşturma

Firebase projenizde, yeni bir kullanıcı oluşturmak için createUserWithEmailAndPassword yöntemini kullanarak veya bir kullanıcının birleştirilmiş kimlik kullanarak ilk kez oturum açmasını sağlayarak sağlayıcı (ör. Google ile Oturum Açma veya Facebook'a Giriş.

Ayrıca Kimlik Doğrulama bölümünden şifreyle kimliği doğrulanmış yeni kullanıcılar da Firebase konsolunun "Kullanıcılar" sayfasından veya Yönetici SDK'sı.

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

Geçerli kullanıcıyı edinmenin önerilen yolu Kimlik doğrulama nesnesi:

WebWeb
import { getAuth, onAuthStateChanged } from "firebase/auth";

const auth = getAuth();
onAuthStateChanged(auth, (user) => {
  if (user) {
    // User is signed in, see docs for a list of available properties
    // https://firebase.google.com/docs/reference/js/auth.user
    const uid = user.uid;
    // ...
  } else {
    // User is signed out
    // ...
  }
});
firebase.auth().onAuthStateChanged((user) => {
  if (user) {
    // User is signed in, see docs for a list of available properties
    // https://firebase.google.com/docs/reference/js/v8/firebase.User
    var uid = user.uid;
    // ...
  } else {
    // User is signed out
    // ...
  }
});

Bir gözlemci kullanarak, Auth nesnesinin bir arada olmadığından emin olun durumu (ör. başlatma) ekleyebilirsiniz. Google Takvim widget'ını signInWithRedirect kullanılırsa onAuthStateChanged gözlemleyicisi getRedirectResult tetiklenmeden önce çözümlenir.

Şu anda oturum açmış kullanıcıyı currentUser kullanarak da öğrenebilirsiniz Kullanıcı oturum açmamışsa currentUser boştur:

WebWeb
import { getAuth } from "firebase/auth";

const auth = getAuth();
const user = auth.currentUser;

if (user) {
  // User is signed in, see docs for a list of available properties
  // https://firebase.google.com/docs/reference/js/auth.user
  // ...
} else {
  // No user is signed in.
}
const user = firebase.auth().currentUser;

if (user) {
  // User is signed in, see docs for a list of available properties
  // https://firebase.google.com/docs/reference/js/v8/firebase.User
  // ...
} else {
  // No user is signed in.
}

Kullanıcının profilini alma

Kullanıcının profil bilgilerini almak için şunu kullanın: User Örneğin:

WebWeb
import { getAuth } from "firebase/auth";

const auth = getAuth();
const user = auth.currentUser;
if (user !== null) {
  // The user object has basic properties such as display name, email, etc.
  const displayName = user.displayName;
  const email = user.email;
  const photoURL = user.photoURL;
  const emailVerified = user.emailVerified;

  // 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.getToken() instead.
  const uid = user.uid;
}
const user = firebase.auth().currentUser;
if (user !== null) {
  // The user object has basic properties such as display name, email, etc.
  const displayName = user.displayName;
  const email = user.email;
  const photoURL = user.photoURL;
  const emailVerified = user.emailVerified;

  // 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.getIdToken() instead.
  const uid = user.uid;
}

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

Bir için providerData özelliğini kullanın. Örneğin:

WebWeb
import { getAuth } from "firebase/auth";

const auth = getAuth();
const user = auth.currentUser;

if (user !== null) {
  user.providerData.forEach((profile) => {
    console.log("Sign-in provider: " + profile.providerId);
    console.log("  Provider-specific UID: " + profile.uid);
    console.log("  Name: " + profile.displayName);
    console.log("  Email: " + profile.email);
    console.log("  Photo URL: " + profile.photoURL);
  });
}
const user = firebase.auth().currentUser;

if (user !== null) {
  user.providerData.forEach((profile) => {
    console.log("Sign-in provider: " + profile.providerId);
    console.log("  Provider-specific UID: " + profile.uid);
    console.log("  Name: " + profile.displayName);
    console.log("  Email: " + profile.email);
    console.log("  Photo URL: " + profile.photoURL);
  });
}

Kullanıcı profilini güncelleme

Kullanıcının temel profil bilgilerini (kullanıcının görünen adı) güncelleyebilirsiniz. ve profil fotoğrafı URL'si (updateProfile yöntemiyle). Örneğin:

WebWeb
import { getAuth, updateProfile } from "firebase/auth";
const auth = getAuth();
updateProfile(auth.currentUser, {
  displayName: "Jane Q. User", photoURL: "https://example.com/jane-q-user/profile.jpg"
}).then(() => {
  // Profile updated!
  // ...
}).catch((error) => {
  // An error occurred
  // ...
});
const user = firebase.auth().currentUser;

user.updateProfile({
  displayName: "Jane Q. User",
  photoURL: "https://example.com/jane-q-user/profile.jpg"
}).then(() => {
  // Update successful
  // ...
}).catch((error) => {
  // An error occurred
  // ...
});  

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

Kullanıcının e-posta adresini updateEmail yöntemiyle ayarlayabilirsiniz. Örneğin:

WebWeb
import { getAuth, updateEmail } from "firebase/auth";
const auth = getAuth();
updateEmail(auth.currentUser, "user@example.com").then(() => {
  // Email updated!
  // ...
}).catch((error) => {
  // An error occurred
  // ...
});
const user = firebase.auth().currentUser;

user.updateEmail("user@example.com").then(() => {
  // Update successful
  // ...
}).catch((error) => {
  // An error occurred
  // ...
});

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

Şu adrese sahip bir kullanıcıya adres doğrulama e-postası gönderebilirsiniz: sendEmailVerification yöntemini çağırın. Örneğin:

WebWeb
import { getAuth, sendEmailVerification } from "firebase/auth";

const auth = getAuth();
sendEmailVerification(auth.currentUser)
  .then(() => {
    // Email verification sent!
    // ...
  });
firebase.auth().currentUser.sendEmailVerification()
  .then(() => {
    // Email verification sent!
    // ...
  });

Şu sayfanın Kimlik doğrulama bölümünde kullanılan e-posta şablonunu özelleştirebilirsiniz: Firebase konsolunda, E-posta Şablonları sayfasından ulaşabilirsiniz. E-posta Şablonları'na göz atın: Firebase Yardım Merkezi.

Durum ayrıca bir geri yönlendirmek için devam URL'si uygulamaya koymayı tercih etmiş olursunuz.

Ayrıca, e-posta adresinin dilini güncelleyerek doğrulama e-postasını da yerelleştirebilirsiniz. Auth örneğine eklemeniz gerekir. Örneğin:

WebWeb
import { getAuth } from "firebase/auth";

const auth = getAuth();
auth.languageCode = 'it';
// To apply the default browser preference instead of explicitly setting it.
// auth.useDeviceLanguage();
firebase.auth().languageCode = 'it';
// To apply the default browser preference instead of explicitly setting it.
// firebase.auth().useDeviceLanguage();

Kullanıcı şifresi ayarlayın

Kullanıcı şifresini updatePassword yöntemiyle ayarlayabilirsiniz. Örneğin:

WebWeb
import { getAuth, updatePassword } from "firebase/auth";

const auth = getAuth();

const user = auth.currentUser;
const newPassword = getASecureRandomPassword();

updatePassword(user, newPassword).then(() => {
  // Update successful.
}).catch((error) => {
  // An error ocurred
  // ...
});
const user = firebase.auth().currentUser;
const newPassword = getASecureRandomPassword();

user.updatePassword(newPassword).then(() => {
  // Update successful.
}).catch((error) => {
  // An error ocurred
  // ...
});

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

sendPasswordResetEmail kullanan bir kullanıcıya şifre sıfırlama e-postası gönderebilirsiniz. yöntemidir. Örneğin:

WebWeb
import { getAuth, sendPasswordResetEmail } from "firebase/auth";

const auth = getAuth();
sendPasswordResetEmail(auth, email)
  .then(() => {
    // Password reset email sent!
    // ..
  })
  .catch((error) => {
    const errorCode = error.code;
    const errorMessage = error.message;
    // ..
  });
firebase.auth().sendPasswordResetEmail(email)
  .then(() => {
    // Password reset email sent!
    // ..
  })
  .catch((error) => {
    var errorCode = error.code;
    var errorMessage = error.message;
    // ..
  });

Şu sayfanın Kimlik doğrulama bölümünde kullanılan e-posta şablonunu özelleştirebilirsiniz: Firebase konsolunda, E-posta Şablonları sayfasından ulaşabilirsiniz. E-posta Şablonları'na göz atın: Firebase Yardım Merkezi.

Durum ayrıca bir geri yönlendirmek için devam URL'si uygulamaya bir şifre sıfırlama e-postası gönderir.

Ayrıca, dili güncelleyerek şifre sıfırlama e-postasını yerelleştirebilirsiniz. Auth örneğine eklemeniz gerekir. Örneğin:

WebWeb
import { getAuth } from "firebase/auth";

const auth = getAuth();
auth.languageCode = 'it';
// To apply the default browser preference instead of explicitly setting it.
// auth.useDeviceLanguage();
firebase.auth().languageCode = 'it';
// To apply the default browser preference instead of explicitly setting it.
// firebase.auth().useDeviceLanguage();

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

Kullanıcı silme

Bir kullanıcı hesabını delete yöntemini kullanarak silebilirsiniz. Örneğin:

WebWeb
import { getAuth, deleteUser } from "firebase/auth";

const auth = getAuth();
const user = auth.currentUser;

deleteUser(user).then(() => {
  // User deleted.
}).catch((error) => {
  // An error ocurred
  // ...
});
const user = firebase.auth().currentUser;

user.delete().then(() => {
  // User deleted.
}).catch((error) => {
  // An error ocurred
  // ...
});

Ayrıca, Firebase konsolu'nu seçin.

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

Güvenlik açısından hassas işlemler (ör. hesap silme, birincil e-posta adresini ayarlama ve şifreyi değiştirme - kullanıcının kısa bir süre önce oturum açtı. Bu işlemlerden birini gerçekleştirirseniz ve kullanıcı oturum açtıysa çok uzun zaman önce işlem bir hata döndürerek başarısız oluyor. Bu durumda, yeni oturum açma kimlik bilgileri alarak kullanıcının kimliğini tekrar doğrulayın kimlik bilgilerini reauthenticateWithCredential adlı sağlayıcıya iletmelidir. Örneğin:

WebWeb
import { getAuth, reauthenticateWithCredential } from "firebase/auth";

const auth = getAuth();
const user = auth.currentUser;

// TODO(you): prompt the user to re-provide their sign-in credentials
const credential = promptForCredentials();

reauthenticateWithCredential(user, credential).then(() => {
  // User re-authenticated.
}).catch((error) => {
  // An error ocurred
  // ...
});
const user = firebase.auth().currentUser;

// TODO(you): prompt the user to re-provide their sign-in credentials
const credential = promptForCredentials();

user.reauthenticateWithCredential(credential).then(() => {
  // User re-authenticated.
}).catch((error) => {
  // An error occurred
  // ...
});

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

Kullanıcı hesaplarını bir dosyadan Firebase projenize aktarmak için Firebase CLI'ın auth:import komutu. Örneğin:

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