تؤدي بعض إجراءات إدارة المستخدمين، مثل تعديل عنوان البريد الإلكتروني لمستخدم وإعادة ضبط كلمة مرور مستخدم، إلى إرسال رسائل إلكترونية إلى المستخدم. وتحتوي هذه الرسائل الإلكترونية على روابط يمكن للمستلِمين فتحها لإكمال إجراء إدارة المستخدمين أو إلغائه. By default, user management emails link to the default action handler, which is a web page hosted at a URL in your project's Firebase Hosting domain.
يمكنك بدلاً من ذلك إنشاء معالج إجراءات مخصّص للبريد الإلكتروني واستضافته لتنفيذ معالجة مخصّصة ودمج معالج إجراءات البريد الإلكتروني مع موقعك الإلكتروني.
تتطلّب إجراءات إدارة المستخدمين التالية من المستخدم إكمال الإجراء باستخدام معالج إجراءات البريد الإلكتروني:
- إعادة ضبط كلمات المرور
- إبطال تغييرات عنوان البريد الإلكتروني: عندما يغيّر المستخدمون عناوين البريد الإلكتروني الرئيسية لحساباتهم، ترسل Firebase رسالة إلكترونية إلى عناوينهم القديمة تتيح لهم التراجع عن التغيير
- التحقّق من عناوين البريد الإلكتروني
لتخصيص معالج إجراءات البريد الإلكتروني لمشروع Firebase، يجب إنشاء صفحة ويب واستضافتها تستخدم حزمة تطوير البرامج (SDK) لـ Firebase JavaScript للتحقّق من صلاحية الطلب وإكماله. بعد ذلك، عليك تخصيص نماذج الرسائل الإلكترونية لمشروعك على Firebase لربطها بمعالج الإجراءات المخصّص.
إنشاء صفحة معالِج إجراءات البريد الإلكتروني
تضيف Firebase عدة معلمات طلب بحث إلى عنوان URL الخاص بمعالج الإجراءات عندما تنشئ رسائل بريد إلكتروني لإدارة المستخدمين. على سبيل المثال:
https://example.com/usermgmt?mode=resetPassword&oobCode=ABC123&apiKey=AIzaSy...&lang=fr
تحدّد هذه المَعلمات مهمة إدارة المستخدمين التي يكملها المستخدم. يجب أن تعالج صفحة معالِج إجراءات البريد الإلكتروني معلمات طلب البحث التالية:
المَعلمات الوضع إجراء إدارة المستخدمين المطلوب إكماله يمكن أن تكون إحدى القيم التالية:
resetPasswordrecoverEmailverifyEmail
oobCode رمز صالح لمرة واحدة يُستخدَم لتحديد طلب والتحقّق منه apiKey مفتاح واجهة برمجة التطبيقات لمشروع Firebase، والذي يتم توفيره لتسهيل الاستخدام continueUrl هذا عنوان URL اختياري يوفّر طريقة لإرسال الحالة إلى التطبيق من خلال عنوان URL. ينطبق ذلك على وضعَي إعادة ضبط كلمة المرور وتأكيد عنوان البريد الإلكتروني. عند إرسال رسالة إلكترونية لإعادة ضبط كلمة المرور أو رسالة تأكيد، يجب تحديد عنصر ActionCodeSettingsمع متابعة عنوان URL لكي يكون ذلك متاحًا. يتيح ذلك للمستخدم المتابعة من حيث توقف بعد تنفيذ إجراء في رسالة إلكترونية.lang هذه هي علامة اللغة الاختيارية BCP47 التي تمثّل لغة المستخدم (على سبيل المثال،
fr). يمكنك استخدام هذه القيمة لتوفير صفحات معالجة إجراءات البريد الإلكتروني المترجَمة للمستخدمين.يمكن ضبط الأقلمة من خلال وحدة تحكُّم Firebase أو ديناميكيًا من خلال استدعاء واجهة برمجة التطبيقات الخاصة بالعميل قبل بدء إجراء البريد الإلكتروني. على سبيل المثال، باستخدام JavaScript:
firebase.auth().languageCode = 'fr';.لضمان تجربة مستخدم متّسقة، تأكَّد من أنّ لغة معالِج إجراءات الرسائل الإلكترونية تتطابق مع لغة نموذج الرسالة الإلكترونية.
يوضّح المثال التالي كيفية التعامل مع معلمات طلب البحث في معالج يستند إلى المتصفّح. (يمكنك أيضًا تنفيذ المعالج كتطبيق Node.js باستخدام منطق مشابه).
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);
يمكنك معالجة طلبات إعادة ضبط كلمة المرور من خلال التحقّق أولاً من رمز الإجراء باستخدام
verifyPasswordResetCode، ثم الحصول على كلمة مرور جديدة من المستخدم وتمريرها إلىconfirmPasswordReset. على سبيل المثال: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. }); }
يمكنك معالجة عمليات إلغاء تغيير عنوان البريد الإلكتروني من خلال التحقّق أولاً من رمز الإجراء باستخدام
checkActionCode، ثم استعادة عنوان البريد الإلكتروني للمستخدم باستخدامapplyActionCode. على سبيل المثال: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. }); }
يمكنك معالجة عملية التحقّق من عنوان البريد الإلكتروني من خلال عرض زر تأكيد وطلب نقرة من المستخدم قبل استدعاء
applyActionCode. يؤدي طلب نقرة من المستخدم إلى منع برامج فحص أمان البريد الإلكتروني الآلية التي تجلب الروابط مسبقًا من استخدام رموز التحقّق لمرة واحدة قبل أن يفتح المستخدم الصفحة:Web
import { applyActionCode } from "firebase/auth"; function handleVerifyEmail(auth, actionCode, continueUrl, lang) { // Localize the UI to the selected language as determined by the lang // parameter. // Show the confirmation button and apply the action code only after // the user clicks it. const confirmButton = document.getElementById('confirm-verify-email'); if (!confirmButton) { return; } confirmButton.hidden = false; confirmButton.addEventListener('click', () => { confirmButton.disabled = true; applyActionCode(auth, actionCode).then(() => { // Email address has been verified. Display a confirmation // message, and if continueUrl is available, provide a button // that redirects back to the app. }).catch((error) => { confirmButton.disabled = false; // Code is invalid, expired, or failed due to a network error. // Ask the user to verify their email address again if needed. }); }); }Web
function handleVerifyEmail(auth, actionCode, continueUrl, lang) { // Localize the UI to the selected language as determined by the lang // parameter. // Show the confirmation button and apply the action code only after // the user clicks it. var confirmButton = document.getElementById('confirm-verify-email'); if (!confirmButton) { return; } confirmButton.hidden = false; confirmButton.addEventListener('click', function() { confirmButton.disabled = true; auth.applyActionCode(actionCode).then(function() { // Email address has been verified. Display a confirmation // message, and if continueUrl is available, provide a button // that redirects back to the app. }).catch(function(error) { confirmButton.disabled = false; // Code is invalid, expired, or failed due to a network error. // Ask the user to verify their email address again if needed. }); }); }استضِف الصفحة في مكان ما، على سبيل المثال، استخدِم Firebase Hosting.
تحديد المشاكل وحلّها: مفتاح واجهة برمجة التطبيقات قديم أو غير صالح في روابط لاتّخاذ إجراء
تدير الخلفية في "مصادقة Firebase" مفتاح واجهة برمجة التطبيقات المضمّن في روابط لاتّخاذ إجراء التي يتم إنشاؤها، مثل المَعلمة apiKey في عنوان URL. في حال تدوير مفاتيح واجهة برمجة التطبيقات لمشروعك أو حذف المفتاح الذي كان مرتبطًا في الأصل بـ Firebase Auth، قد يتلقّى المستخدمون روابط تتضمّن مفاتيح غير صالحة، ما يؤدي إلى حدوث أخطاء auth/invalid-api-key.
لحلّ هذه المشكلة، اتّبِع الخطوات التالية:
- إذا كنت تستخدم استضافة Firebase: قد يتم تخزين مفتاح واجهة برمجة التطبيقات مؤقتًا في
init.json. انشر إصدارًا جديدًا من موقعك الإلكتروني المستضاف على Hosting (firebase deploy --only hosting) لفرض إعادة إنشاء الإعدادات باستخدام مفتاح واجهة برمجة التطبيقات النشط على خدمة البيانات الوصفية في الخلفية. - إذا كان مفتاح الخلفية نفسه بحاجة إلى تعديل: بما أنّه لا يمكن تعديل ربط مفتاح واجهة برمجة التطبيقات في الخلفية مباشرةً باستخدام وحدة التحكّم أو حِزم تطوير البرامج (SDK)، عليك التواصل مع فريق الدعم لطلب تعديل إعدادات المصادقة في الخلفية لمشروعك.
بعد ذلك، يجب ضبط مشروع Firebase لربطه بمعالج إجراءات البريد الإلكتروني المخصّص في رسائل البريد الإلكتروني الخاصة بإدارة المستخدمين.
الربط بمعالجك المخصّص في نماذج الرسائل الإلكترونية
لضبط مشروع Firebase من أجل استخدام معالج إجراءات البريد الإلكتروني المخصّص:
في وحدة تحكّم Firebase، انتقِل إلى علامة التبويب الأمان > المصادقة > النماذج.
في أي من إدخالات أنواع الرسائل الإلكترونية، انقر على رمز القلم الرصاص لتعديل نموذج الرسالة الإلكترونية.
انقر على تخصيص عنوان URL للإجراء، وحدِّد عنوان URL لمعالج إجراءات البريد الإلكتروني المخصّص.
بعد حفظ عنوان URL، سيتم استخدامه في جميع نماذج الرسائل الإلكترونية الخاصة بمشروعك على Firebase.