Kullanıcı oluştur
CreateUserWithEmailAndPassword yöntemini çağırarak veya Google Sign-In veya Facebook Login gibi bir birleşik kimlik sağlayıcı kullanarak bir kullanıcıyı ilk kez oturum açarak createUserWithEmailAndPassword
projenizde yeni bir kullanıcı oluşturursunuz.
Ayrıca, Firebase konsolunun Kimlik Doğrulama bölümünden, Kullanıcılar sayfasından veya Yönetici SDK'sını kullanarak parolayla doğrulanmış yeni kullanıcılar oluşturabilirsiniz.
Şu anda oturum açmış olan kullanıcıyı alın
Geçerli kullanıcıyı almanın önerilen yolu, Auth nesnesinde bir gözlemci ayarlamaktır:
Web version 9
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 // ... } });
Web version 8
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, geçerli kullanıcıyı aldığınızda Auth nesnesinin bir ara durumda (başlatma gibi) olmadığından emin olursunuz. signInWithRedirect
kullandığınızda, onAuthStateChanged
gözlemci tetiklemeden önce getRedirectResult
kadar bekler.
Ayrıca currentUser
özelliğini kullanarak şu anda oturum açmış olan kullanıcıyı da alabilirsiniz. Bir kullanıcı oturum açmadıysa, currentUser
null olur:
Web version 9
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. }
Web version 8
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. }
Bir kullanıcının profilini alın
Bir kullanıcının profil bilgilerini almak için User
örneğinin özelliklerini kullanın. Örneğin:
Web version 9
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; }
Web version 8
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; }
Bir kullanıcının sağlayıcıya özel profil bilgilerini alın
Bir kullanıcıya bağlı oturum açma sağlayıcılarından alınan profil bilgilerini almak için providerData
özelliğini kullanın. Örneğin:
Web version 9
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); }); }
Web version 8
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); }); }
Bir kullanıcının profilini güncelleme
Bir kullanıcının temel profil bilgilerini (kullanıcının görünen adı ve profil fotoğrafı URL'si) updateProfile
yöntemiyle güncelleyebilirsiniz. Örneğin:
Web version 9
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 // ... });
Web version 8
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 // ... });
Bir kullanıcının e-posta adresini ayarlayın
updateEmail
yöntemiyle bir kullanıcının e-posta adresini ayarlayabilirsiniz. Örneğin:
Web version 9
import { getAuth, updateEmail } from "firebase/auth"; const auth = getAuth(); updateEmail(auth.currentUser, "user@example.com").then(() => { // Email updated! // ... }).catch((error) => { // An error occurred // ... });
Web version 8
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önderin
sendEmailVerification
yöntemiyle bir kullanıcıya adres doğrulama e-postası gönderebilirsiniz. Örneğin:
Web version 9
import { getAuth, sendEmailVerification } from "firebase/auth"; const auth = getAuth(); sendEmailVerification(auth.currentUser) .then(() => { // Email verification sent! // ... });
Web version 8
firebase.auth().currentUser.sendEmailVerification() .then(() => { // Email verification sent! // ... });
Firebase konsolunun Kimlik Doğrulama bölümünde kullanılan e-posta şablonunu, E-posta Şablonları sayfasında özelleştirebilirsiniz. Firebase Yardım Merkezi'ndeki E-posta Şablonlarına bakın.
Bir doğrulama e-postası gönderirken uygulamaya geri yönlendirmek için durumu bir devam URL'si aracılığıyla iletmek de mümkündür.
Ayrıca, e-postayı göndermeden önce Yetkilendirme örneğindeki dil kodunu güncelleyerek doğrulama e-postasını yerelleştirebilirsiniz. Örneğin:
Web version 9
import { getAuth } from "firebase/auth"; const auth = getAuth(); auth.languageCode = 'it'; // To apply the default browser preference instead of explicitly setting it. // firebase.auth().useDeviceLanguage();
Web version 8
firebase.auth().languageCode = 'it'; // To apply the default browser preference instead of explicitly setting it. // firebase.auth().useDeviceLanguage();
Bir kullanıcının şifresini belirleyin
updatePassword
yöntemiyle bir kullanıcının parolasını belirleyebilirsiniz. Örneğin:
Web version 9
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 // ... });
Web version 8
const user = firebase.auth().currentUser; const newPassword = getASecureRandomPassword(); user.updatePassword(newPassword).then(() => { // Update successful. }).catch((error) => { // An error ocurred // ... });
Parola sıfırlama e-postası gönder
sendPasswordResetEmail
yöntemi ile bir kullanıcıya şifre sıfırlama e-postası gönderebilirsiniz. Örneğin:
Web version 9
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; // .. });
Web version 8
firebase.auth().sendPasswordResetEmail(email) .then(() => { // Password reset email sent! // .. }) .catch((error) => { var errorCode = error.code; var errorMessage = error.message; // .. });
Firebase konsolunun Kimlik Doğrulama bölümünde kullanılan e-posta şablonunu, E-posta Şablonları sayfasında özelleştirebilirsiniz. Firebase Yardım Merkezi'ndeki E-posta Şablonlarına bakın.
Parola sıfırlama e-postası gönderirken uygulamaya geri yönlendirmek için durumu bir devam URL'si aracılığıyla iletmek de mümkündür.
Ayrıca, e-postayı göndermeden önce Yetkilendirme örneğindeki dil kodunu güncelleyerek parola sıfırlama e-postasını yerelleştirebilirsiniz. Örneğin:
Web version 9
import { getAuth } from "firebase/auth"; const auth = getAuth(); auth.languageCode = 'it'; // To apply the default browser preference instead of explicitly setting it. // firebase.auth().useDeviceLanguage();
Web version 8
firebase.auth().languageCode = 'it'; // To apply the default browser preference instead of explicitly setting it. // firebase.auth().useDeviceLanguage();
Firebase konsolundan parola sıfırlama e-postaları da gönderebilirsiniz.
Bir kullanıcıyı sil
delete
yöntemi ile bir kullanıcı hesabını silebilirsiniz. Örneğin:
Web version 9
import { getAuth, deleteUser } from "firebase/auth"; const auth = getAuth(); const user = auth.currentUser; deleteUser(user).then(() => { // User deleted. }).catch((error) => { // An error ocurred // ... });
Web version 8
const user = firebase.auth().currentUser; user.delete().then(() => { // User deleted. }).catch((error) => { // An error ocurred // ... });
Kullanıcıları, Kullanıcılar sayfasındaki Firebase konsolunun Kimlik Doğrulama bölümünden de silebilirsiniz.
Bir kullanıcının kimliğini yeniden doğrulama
Bir hesabı silme , birincil e-posta adresi belirleme ve parola değiştirme gibi güvenlik açısından hassas bazı işlemler, kullanıcının yakın zamanda oturum açmasını gerektirir. Bu işlemlerden birini gerçekleştirirseniz ve kullanıcı çok uzun zaman önce oturum açmışsa, eylem bir hatayla başarısız olur. Bu olduğunda, kullanıcıdan yeni oturum açma kimlik bilgilerini alarak ve kimlik bilgilerini reauthenticateWithCredential
ileterek kullanıcının kimliğini yeniden doğrulayın. Örneğin:
Web version 9
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 // ... });
Web version 8
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 aktar
Firebase CLI'nin auth:import
komutunu kullanarak bir dosyadan kullanıcı hesaplarını Firebase projenize aktarabilirsiniz. Örneğin:
firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14