Özel e-posta işlem işleyicileri oluşturma

Bir kullanıcının e-posta adresini güncelleme ve kullanıcıya e-posta gönderilmesine neden olabilir. Bu e-postalar, alıcıların kullanıcıyı tamamlamak veya iptal etmek için açabileceği bağlantılar içerir eyleme dökülebilir. Varsayılan olarak, kullanıcı yönetimi e-postaları varsayılan işleme bağlanır işleyicisi (projenizin Firebase Hosting'indeki bir URL'de barındırılan web sayfasıdır) alan adına sahip olmanız gerekir.

Bunun yerine, özel işlemler için özel bir e-posta işlem işleyicisi oluşturup barındırabilirsiniz. ve e-posta işlem işleyicisini web sitenizle entegre edebilirsiniz.

Aşağıdaki kullanıcı yönetimi işlemleri için kullanıcının bu işlemi tamamlaması gerekir aşağıdaki adımları izleyin:

  • Şifreleri sıfırlama
  • E-posta adresi değişikliklerini iptal etme: Kullanıcılar hesaplarını değiştirdiğinde birincil bir e-posta adresi kullanıyorsanız Firebase, kullanıcıların geri almak için
  • E-posta adreslerini doğrulama

Firebase projenizin e-posta işlemi işleyicisini özelleştirmek için isteği doğrulamak için Firebase JavaScript SDK'yı kullanan bir web sayfası barındırın. geçerli olduğunu doğrulamalı ve isteği tamamlamalıdır. Ardından, Firebase'i manuel olarak özel işlem işleyicinize bağlamak için projenizin e-posta şablonlarını kullanabilirsiniz.

E-posta işlem işleyici sayfasını oluşturma

  1. Firebase, aşağıdaki durumlarda işlem işleyici URL'nize çeşitli sorgu parametreleri ekler: kullanıcı yönetimi e-postaları oluşturur. Örnek:

    https://example.com/usermgmt?mode=resetPassword&oobCode=ABC123&apiKey=AIzaSy...&lang=fr
    .

    Bu parametreler, kullanıcının şu anda kullandığı kullanıcı yönetimi görevini teşekkür etmenin de önemli bir yoludur. E-posta işlem işleyici sayfanız aşağıdaki sorguyu işlemelidir: parametre:

    Parametreler
    mod

    Tamamlanacak kullanıcı yönetimi işlemi. Şunlardan biri olabilir: şu değerlere sahiptir:

    • resetPassword
    • recoverEmail
    • verifyEmail
    oobKodu İsteği tanımlamak ve doğrulamak için kullanılan tek seferlik bir kod
    apiAnahtarı Kolaylık sağlamak için Firebase projenizin API anahtarı
    devamURL'si Bu, durumu tekrar bir uygulamadır. Bu, şifre sıfırlama ve e-posta ile ilgili doğrulama modlarını kullanabilirsiniz. Şifre sıfırlama e-postası gönderirken veya bir ActionCodeSettings nesnesinin bir devam URL'si ile birlikte belirtilmesi gerekir. Bu kullanıcı kaldığı yerden devam edebilir e-posta işleminden sonra belirlenecek.
    dil

    Bu, isteğe bağlı BCP47 'nı inceleyin. kullanıcının yerel ayarını temsil eden dil etiketi (örneğin, fr). Yerelleştirilmiş e-posta sağlamak için bu değeri kullanabilirsiniz. işlem işleyici sayfaları oluşturabilirsiniz.

    Yerelleştirme, Firebase konsolu üzerinden veya e-postayı tetiklemeden önce ilgili istemci API'sini çağırmak eyleme dökülebilir. Örneğin, JavaScript kullanarak: firebase.auth().languageCode = 'fr';

    Tutarlı bir kullanıcı deneyimi için e-posta işlem işleyicisinin yerelleştirme e-posta şablonlarınınkiyle eşleşir.

    Aşağıdaki örnek, tarayıcı tabanlı işleyici. (İşleyiciyi Node.js olarak da uygulayabilirsiniz) benzer bir mantık kullanarak yapabilirsiniz.)

    Web

    import { initializeApp } from "firebase/app";
    import { getAuth } from "firebase/auth";
    
    document.addEventListener('DOMContentLoaded', () => {
      // TODO: Implement getParameterByName()
    
      // Get the action to complete.
      const mode = getParameterByName('mode');
      // Get the one-time code from the query parameter.
      const actionCode = getParameterByName('oobCode');
      // (Optional) Get the continue URL from the query parameter if available.
      const continueUrl = getParameterByName('continueUrl');
      // (Optional) Get the language code if available.
      const lang = getParameterByName('lang') || 'en';
    
      // Configure the Firebase SDK.
      // This is the minimum configuration required for the API to be used.
      const config = {
        'apiKey': "YOUR_API_KEY" // Copy this key from the web initialization
                                 // snippet found in the Firebase console.
      };
      const app = initializeApp(config);
      const auth = getAuth(app);
    
      // Handle the user management action.
      switch (mode) {
        case 'resetPassword':
          // Display reset password handler and UI.
          handleResetPassword(auth, actionCode, continueUrl, lang);
          break;
        case 'recoverEmail':
          // Display email recovery handler and UI.
          handleRecoverEmail(auth, actionCode, lang);
          break;
        case 'verifyEmail':
          // Display email verification handler and UI.
          handleVerifyEmail(auth, actionCode, continueUrl, lang);
          break;
        default:
          // Error: invalid mode.
      }
    }, false);

    Web

    document.addEventListener('DOMContentLoaded', () => {
      // TODO: Implement getParameterByName()
    
      // Get the action to complete.
      var mode = getParameterByName('mode');
      // Get the one-time code from the query parameter.
      var actionCode = getParameterByName('oobCode');
      // (Optional) Get the continue URL from the query parameter if available.
      var continueUrl = getParameterByName('continueUrl');
      // (Optional) Get the language code if available.
      var lang = getParameterByName('lang') || 'en';
    
      // Configure the Firebase SDK.
      // This is the minimum configuration required for the API to be used.
      var config = {
        'apiKey': "YOU_API_KEY" // Copy this key from the web initialization
                                // snippet found in the Firebase console.
      };
      var app = firebase.initializeApp(config);
      var auth = app.auth();
    
      // Handle the user management action.
      switch (mode) {
        case 'resetPassword':
          // Display reset password handler and UI.
          handleResetPassword(auth, actionCode, continueUrl, lang);
          break;
        case 'recoverEmail':
          // Display email recovery handler and UI.
          handleRecoverEmail(auth, actionCode, lang);
          break;
        case 'verifyEmail':
          // Display email verification handler and UI.
          handleVerifyEmail(auth, actionCode, continueUrl, lang);
          break;
        default:
          // Error: invalid mode.
      }
    }, false);
  2. Şifre sıfırlama isteklerini öncelikle şurada doğrulama yaparak işleme alın: verifyPasswordResetCode; ardından kullanıcıdan yeni bir şifre alıp Hedef: confirmPasswordReset. Örneğin:

    Web

    import { verifyPasswordResetCode, confirmPasswordReset } from "firebase/auth";
    
    function handleResetPassword(auth, actionCode, continueUrl, lang) {
      // Localize the UI to the selected language as determined by the lang
      // parameter.
    
      // Verify the password reset code is valid.
      verifyPasswordResetCode(auth, actionCode).then((email) => {
        const accountEmail = email;
    
        // TODO: Show the reset screen with the user's email and ask the user for
        // the new password.
        const newPassword = "...";
    
        // Save the new password.
        confirmPasswordReset(auth, actionCode, newPassword).then((resp) => {
          // Password reset has been confirmed and new password updated.
    
          // TODO: Display a link back to the app, or sign-in the user directly
          // if the page belongs to the same domain as the app:
          // auth.signInWithEmailAndPassword(accountEmail, newPassword);
    
          // TODO: If a continue URL is available, display a button which on
          // click redirects the user back to the app via continueUrl with
          // additional state determined from that URL's parameters.
        }).catch((error) => {
          // Error occurred during confirmation. The code might have expired or the
          // password is too weak.
        });
      }).catch((error) => {
        // Invalid or expired action code. Ask user to try to reset the password
        // again.
      });
    }

    Web

    function handleResetPassword(auth, actionCode, continueUrl, lang) {
      // Localize the UI to the selected language as determined by the lang
      // parameter.
    
      // Verify the password reset code is valid.
      auth.verifyPasswordResetCode(actionCode).then((email) => {
        var accountEmail = email;
    
        // TODO: Show the reset screen with the user's email and ask the user for
        // the new password.
        var newPassword = "...";
    
        // Save the new password.
        auth.confirmPasswordReset(actionCode, newPassword).then((resp) => {
          // Password reset has been confirmed and new password updated.
    
          // TODO: Display a link back to the app, or sign-in the user directly
          // if the page belongs to the same domain as the app:
          // auth.signInWithEmailAndPassword(accountEmail, newPassword);
    
          // TODO: If a continue URL is available, display a button which on
          // click redirects the user back to the app via continueUrl with
          // additional state determined from that URL's parameters.
        }).catch((error) => {
          // Error occurred during confirmation. The code might have expired or the
          // password is too weak.
        });
      }).catch((error) => {
        // Invalid or expired action code. Ask user to try to reset the password
        // again.
      });
    }
  3. E-posta adresi değişikliği iptallerini yönetmek için önce işlem kodunu doğrulayın checkActionCode ile; ardından kullanıcının e-posta adresini applyActionCode. Örneğin:

    Web

    import { checkActionCode, applyActionCode, sendPasswordResetEmail } from "firebase/auth";
    
    function handleRecoverEmail(auth, actionCode, lang) {
      // Localize the UI to the selected language as determined by the lang
      // parameter.
      let restoredEmail = null;
      // Confirm the action code is valid.
      checkActionCode(auth, actionCode).then((info) => {
        // Get the restored email address.
        restoredEmail = info['data']['email'];
    
        // Revert to the old email.
        return applyActionCode(auth, actionCode);
      }).then(() => {
        // Account email reverted to restoredEmail
    
        // TODO: Display a confirmation message to the user.
    
        // You might also want to give the user the option to reset their password
        // in case the account was compromised:
        sendPasswordResetEmail(auth, restoredEmail).then(() => {
          // Password reset confirmation sent. Ask user to check their email.
        }).catch((error) => {
          // Error encountered while sending password reset code.
        });
      }).catch((error) => {
        // Invalid code.
      });
    }

    Web

    function handleRecoverEmail(auth, actionCode, lang) {
      // Localize the UI to the selected language as determined by the lang
      // parameter.
      var restoredEmail = null;
      // Confirm the action code is valid.
      auth.checkActionCode(actionCode).then((info) => {
        // Get the restored email address.
        restoredEmail = info['data']['email'];
    
        // Revert to the old email.
        return auth.applyActionCode(actionCode);
      }).then(() => {
        // Account email reverted to restoredEmail
    
        // TODO: Display a confirmation message to the user.
    
        // You might also want to give the user the option to reset their password
        // in case the account was compromised:
        auth.sendPasswordResetEmail(restoredEmail).then(() => {
          // Password reset confirmation sent. Ask user to check their email.
        }).catch((error) => {
          // Error encountered while sending password reset code.
        });
      }).catch((error) => {
        // Invalid code.
      });
    }
  4. applyActionCode numaralı telefonu arayarak e-posta adresi doğrulamasını tamamlayın. Örneğin:

    Web

    function handleVerifyEmail(auth, actionCode, continueUrl, lang) {
      // Localize the UI to the selected language as determined by the lang
      // parameter.
      // Try to apply the email verification code.
      applyActionCode(auth, actionCode).then((resp) => {
        // Email address has been verified.
    
        // TODO: Display a confirmation message to the user.
        // You could also provide the user with a link back to the app.
    
        // TODO: If a continue URL is available, display a button which on
        // click redirects the user back to the app via continueUrl with
        // additional state determined from that URL's parameters.
      }).catch((error) => {
        // Code is invalid or expired. Ask the user to verify their email address
        // again.
      });
    }

    Web

    function handleVerifyEmail(auth, actionCode, continueUrl, lang) {
      // Localize the UI to the selected language as determined by the lang
      // parameter.
      // Try to apply the email verification code.
      auth.applyActionCode(actionCode).then((resp) => {
        // Email address has been verified.
    
        // TODO: Display a confirmation message to the user.
        // You could also provide the user with a link back to the app.
    
        // TODO: If a continue URL is available, display a button which on
        // click redirects the user back to the app via continueUrl with
        // additional state determined from that URL's parameters.
      }).catch((error) => {
        // Code is invalid or expired. Ask the user to verify their email address
        // again.
      });
    }
  5. Sayfayı bir yerde barındırın (ör. Firebase Hosting kullanın).

Ardından, Firebase projenizi özel e-posta adresinize bağlanacak şekilde yapılandırmanız gerekir işlem işleyici ile ilgili daha fazla bilgiyi kullanıcı yönetimi e-postalarında bulabilirsiniz.

Firebase projenizi, özel e-posta işlem işleyicinizi kullanacak şekilde yapılandırmak için:

  1. Firebase konsolunda projenizi açın.
  2. Kimlik Doğrulama bölümündeki E-posta Şablonları sayfasına gidin.
  3. E-posta Türleri girişlerinden herhangi birinde, e-posta şablonu.
  4. İşlem URL'sini özelleştir'i tıklayın ve özel e-postanızın URL'sini belirtin. işlem işleyici.

URL kaydedildikten sonra Firebase projenizin tüm e-postaları tarafından kullanılır kullanabilirsiniz.