Catch up on highlights from Firebase at Google I/O 2023. Learn more

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

kullanıcı oluştur

Firebase projenizde, createUserWithEmailAndPassword yöntemini çağırarak veya Google Sign-In veya Facebook Login gibi bir birleşik kimlik sağlayıcı kullanarak bir kullanıcıda ilk kez oturum açarak yeni bir kullanıcı oluşturursunuz.

Ayrıca, Firebase konsolunun Kullanıcılar sayfasındaki Kimlik Doğrulama bölümünden parola doğrulaması yapılmış yeni kullanıcılar da oluşturabilirsiniz.

Şu anda oturum açmış olan kullanıcıyı al

Geçerli kullanıcıyı almanın önerilen yolu, getCurrentUser yöntemini çağırmaktır. Hiçbir kullanıcı oturum açmamışsa, getCurrentUser null değerini döndürür:

Kotlin+KTX

val user = Firebase.auth.currentUser
if (user != null) {
    // User is signed in
} else {
    // No user is signed in
}

Java

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // User is signed in
} else {
    // No user is signed in
}

getCurrentUser'ın boş olmayan bir getCurrentUser FirebaseUser , ancak temel belirtecin geçerli olmadığı bazı durumlar vardır. Bu, örneğin kullanıcı başka bir cihazda silinmişse ve yerel belirteç yenilenmemişse olabilir. Bu durumda, geçerli bir getCurrentUser kullanıcısı alabilirsiniz, ancak kimliği doğrulanmış kaynaklara yapılan sonraki çağrılar başarısız olur.

auth nesnesi başlatmayı tamamlamadığı için getCurrentUser ayrıca null değer döndürebilir.

Bir AuthStateListener eklerseniz, temel belirteç durumu her değiştiğinde bir geri arama alırsınız. Bu, yukarıda belirtilenler gibi uç durumlara tepki vermek için yararlı olabilir.

Bir kullanıcının profilini alın

Bir kullanıcının profil bilgilerini almak için FirebaseUser örneğinin erişimci yöntemlerini kullanın. Örneğin:

Kotlin+KTX

val user = Firebase.auth.currentUser
user?.let {
    // Name, email address, and profile photo Url
    val name = it.displayName
    val email = it.email
    val photoUrl = it.photoUrl

    // Check if user's email is verified
    val emailVerified = it.isEmailVerified

    // 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
    // FirebaseUser.getIdToken() instead.
    val uid = it.uid
}

Java

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // Name, email address, and profile photo Url
    String name = user.getDisplayName();
    String email = user.getEmail();
    Uri photoUrl = user.getPhotoUrl();

    // Check if user's email is verified
    boolean emailVerified = user.isEmailVerified();

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

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 getProviderData yöntemini kullanın. Örneğin:

Kotlin+KTX

val user = Firebase.auth.currentUser
user?.let {
    for (profile in it.providerData) {
        // Id of the provider (ex: google.com)
        val providerId = profile.providerId

        // UID specific to the provider
        val uid = profile.uid

        // Name, email address, and profile photo Url
        val name = profile.displayName
        val email = profile.email
        val photoUrl = profile.photoUrl
    }
}

Java

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    for (UserInfo profile : user.getProviderData()) {
        // Id of the provider (ex: google.com)
        String providerId = profile.getProviderId();

        // UID specific to the provider
        String uid = profile.getUid();

        // Name, email address, and profile photo Url
        String name = profile.getDisplayName();
        String email = profile.getEmail();
        Uri photoUrl = profile.getPhotoUrl();
    }
}

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:

Kotlin+KTX

val user = Firebase.auth.currentUser

val profileUpdates = userProfileChangeRequest {
    displayName = "Jane Q. User"
    photoUri = Uri.parse("https://example.com/jane-q-user/profile.jpg")
}

user!!.updateProfile(profileUpdates)
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            Log.d(TAG, "User profile updated.")
        }
    }

Java

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
        .setDisplayName("Jane Q. User")
        .setPhotoUri(Uri.parse("https://example.com/jane-q-user/profile.jpg"))
        .build();

user.updateProfile(profileUpdates)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "User profile updated.");
                }
            }
        });

Bir kullanıcının e-posta adresini ayarlayın

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

Kotlin+KTX

val user = Firebase.auth.currentUser

user!!.updateEmail("user@example.com")
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            Log.d(TAG, "User email address updated.")
        }
    }

Java

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

user.updateEmail("user@example.com")
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "User email address updated.");
                }
            }
        });

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:

Kotlin+KTX

val user = Firebase.auth.currentUser

user!!.sendEmailVerification()
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            Log.d(TAG, "Email sent.")
        }
    }

Java

FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseUser user = auth.getCurrentUser();

user.sendEmailVerification()
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "Email sent.");
                }
            }
        });

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'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.

Ek olarak, e-postayı göndermeden önce Auth örneğindeki dil kodunu güncelleyerek doğrulama e-postasını yerelleştirebilirsiniz. Örneğin:

Kotlin+KTX

auth.setLanguageCode("fr")
// To apply the default app language instead of explicitly setting it.
// auth.useAppLanguage()

Java

auth.setLanguageCode("fr");
// To apply the default app language instead of explicitly setting it.
// auth.useAppLanguage();

Kullanıcı parolası belirleyin

Bir kullanıcının parolasını updatePassword yöntemiyle belirleyebilirsiniz. Örneğin:

Kotlin+KTX

val user = Firebase.auth.currentUser
val newPassword = "SOME-SECURE-PASSWORD"

user!!.updatePassword(newPassword)
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            Log.d(TAG, "User password updated.")
        }
    }

Java

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String newPassword = "SOME-SECURE-PASSWORD";

user.updatePassword(newPassword)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "User password updated.");
                }
            }
        });

Parola sıfırlama e-postası gönder

Bir kullanıcıya sendPasswordResetEmail yöntemiyle parola sıfırlama e-postası gönderebilirsiniz. Örneğin:

Kotlin+KTX

val emailAddress = "user@example.com"

Firebase.auth.sendPasswordResetEmail(emailAddress)
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            Log.d(TAG, "Email sent.")
        }
    }

Java

FirebaseAuth auth = FirebaseAuth.getInstance();
String emailAddress = "user@example.com";

auth.sendPasswordResetEmail(emailAddress)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "Email sent.");
                }
            }
        });

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'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.

Ek olarak, e-postayı göndermeden önce Auth örneğindeki dil kodunu güncelleyerek parola sıfırlama e-postasını yerelleştirebilirsiniz. Örneğin:

Kotlin+KTX

auth.setLanguageCode("fr")
// To apply the default app language instead of explicitly setting it.
// auth.useAppLanguage()

Java

auth.setLanguageCode("fr");
// To apply the default app language instead of explicitly setting it.
// auth.useAppLanguage();

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

Bir kullanıcıyı sil

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

Kotlin+KTX

val user = Firebase.auth.currentUser!!

user.delete()
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            Log.d(TAG, "User account deleted.")
        }
    }

Java

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

user.delete()
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "User account deleted.");
                }
            }
        });

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

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

Bir hesabı silmek , birincil e-posta adresini ayarlamak ve parolayı değiştirmek gibi güvenlik açısından hassas bazı işlemler, kullanıcının yakın zamanda oturum açmış olmasını gerektirir. Bu işlemlerden birini gerçekleştirirseniz ve kullanıcı çok uzun zaman önce oturum açmışsa, eylem başarısız olur ve FirebaseAuthRecentLoginRequiredException . Bu olduğunda, kullanıcıdan yeni oturum açma kimlik bilgileri alarak ve kimlik bilgilerini reauthenticate ileterek kullanıcının kimliğini yeniden doğrulayın. Örneğin:

Kotlin+KTX

val user = Firebase.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.
val credential = EmailAuthProvider
    .getCredential("user@example.com", "password1234")

// Prompt the user to re-provide their sign-in credentials
user.reauthenticate(credential)
    .addOnCompleteListener { Log.d(TAG, "User re-authenticated.") }

Java

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

// 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.
AuthCredential credential = EmailAuthProvider
        .getCredential("user@example.com", "password1234");

// Prompt the user to re-provide their sign-in credentials
user.reauthenticate(credential)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                Log.d(TAG, "User re-authenticated.");
            }
        });

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

Firebase CLI'nin auth:import komutunu kullanarak kullanıcı hesaplarını bir dosyadan Firebase projenize aktarabilirsiniz. Örneğin:

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