ईमेल कार्रवाई लिंक जनरेट करना

कभी-कभी मोबाइल ऐप्लिकेशन को उपयोगकर्ताओं से इंटरैक्ट करने और उन्हें ईमेल भेजकर कुछ कार्रवाइयां करने के लिए कहने की ज़रूरत होती है.

Firebase Client SDK टूल की मदद से, उपयोगकर्ताओं को ऐसे लिंक वाले ईमेल भेजे जा सकते हैं जिनका इस्तेमाल वे पासवर्ड रीसेट करने, ईमेल पते की पुष्टि करने, और ईमेल के ज़रिए साइन-इन करने के लिए कर सकते हैं. ये टेंप्लेट पर आधारित ईमेल, Google भेजता है. इनमें बदलाव करने की सीमित सुविधाएं होती हैं.

अगर आपको अपने ईमेल टेंप्लेट और अपनी ईमेल डिलीवरी सेवा का इस्तेमाल करना है, तो इस पेज पर बताया गया है कि Firebase Admin SDK टूल का इस्तेमाल करके, ऊपर बताए गए फ़्लो के लिए कार्रवाई के लिंक प्रोग्राम के ज़रिए कैसे जनरेट किए जाएं. इन लिंक को अपने उपयोगकर्ताओं को भेजे जाने वाले ईमेल में शामिल किया जा सकता है.

इससे ये फ़ायदे मिलते हैं:

  • ईमेल टेंप्लेट को पसंद के मुताबिक बनाना. इसमें नई स्टाइल और पसंद के मुताबिक ब्रैंडिंग जोड़ने, शब्दों और लोगो में बदलाव करने, उपयोगकर्ताओं को पूरे नाम के बजाय पहले नाम से संबोधित करने वगैरह की सुविधाएं शामिल हैं.
  • कॉन्टेक्स्ट के हिसाब से अलग-अलग टेंप्लेट लागू करना. उदाहरण के लिए, अगर कोई उपयोगकर्ता न्यूज़लेटर की सदस्यता लेने के लिए अपने ईमेल की पुष्टि कर रहा है, तो ईमेल के कॉन्टेंट में कॉन्टेक्स्ट की जानकारी देनी पड़ सकती है. ईमेल लिंक से साइन इन करने का एक और उदाहरण: किसी एक मामले में, इसे वही उपयोगकर्ता ट्रिगर कर सकता है या कोई दूसरा उपयोगकर्ता, न्योते के तौर पर इसे ट्रिगर कर सकता है. ऐसे में, ईमेल में कॉन्टेक्स्ट की जानकारी शामिल करनी होगी.
  • पसंद के मुताबिक बनाए गए ईमेल टेंप्लेट को स्थानीय भाषा में उपलब्ध कराना.
  • सुरक्षित सर्वर एनवायरमेंट से लिंक जनरेट करने की सुविधा.
  • लिंक को मोबाइल ऐप्लिकेशन या ब्राउज़र के ज़रिए खोलने का तरीका और अतिरिक्त स्टेट की जानकारी पास करने का तरीका वगैरह को पसंद के मुताबिक बनाने की सुविधा.
  • ईमेल ऐक्शन लिंक बनाते समय, मोबाइल ऐप्लिकेशन के फ़्लो के लिए इस्तेमाल किए जाने वाले मोबाइल लिंक डोमेन को पसंद के मुताबिक बनाने की सुविधा.

ActionCodeSettings को शुरू करना

ईमेल ऐक्शन लिंक जनरेट करने से पहले, आपको ActionCodeSettings इंस्टेंस को शुरू करना पड़ सकता है.

ActionCodeSettings की मदद से, जारी रखने के यूआरएल के ज़रिए अतिरिक्त स्टेट पास किया जा सकता है. उपयोगकर्ता के ईमेल लिंक पर क्लिक करने के बाद, इसे ऐक्सेस किया जा सकता है. इससे उपयोगकर्ता को कार्रवाई पूरी होने के बाद, ऐप्लिकेशन पर वापस जाने की सुविधा भी मिलती है. इसके अलावा, यह तय किया जा सकता है कि मोबाइल ऐप्लिकेशन इंस्टॉल होने पर, ईमेल ऐक्शन लिंक को सीधे उससे हैंडल किया जाए या ब्राउज़र से.

मोबाइल ऐप्लिकेशन के ज़रिए खोले जाने वाले लिंक के लिए, आपको अपने मोबाइल ऐप्लिकेशन से इन लिंक का पता लगाने के लिए कुछ टास्क पूरे करने होंगे. ईमेल ऐक्शन के लिए मोबाइल लिंक कॉन्फ़िगर करने के तरीके के बारे में निर्देश देखें.

ActionCodeSettings इंस्टेंस को शुरू करने के लिए, यह डेटा दें:

पैरामीटर टाइप ब्यौरा
url स्ट्रिंग

लिंक (स्टेट/जारी रखने का यूआरएल) सेट करता है, जिसके अलग-अलग कॉन्टेक्स्ट में अलग-अलग मतलब होते हैं:

  • जब लिंक को वेब ऐक्शन विजेट में हैंडल किया जाता है, तो यह डीप लिंक continueUrl क्वेरी पैरामीटर में होता है.
  • जब लिंक को सीधे ऐप्लिकेशन में हैंडल किया जाता है, तो यह continueUrl क्वेरी पैरामीटर होता है Hosting लिंक के डीप लिंक में.
iOS ({bundleId: string}|undefined) iOS बंडल आईडी सेट करता है, ताकि Firebase Authentication यह तय कर सके कि उसे सिर्फ़ वेब लिंक या मोबाइल लिंक बनाना चाहिए. यह लिंक, Apple डिवाइस पर खोला जाता है
android ({packageName: string, installApp:boolean|undefined, minimumVersion: string|undefined}|undefined) Android पैकेज का नाम सेट करता है, ताकि Firebase Authentication यह तय कर सके कि उसे सिर्फ़ वेब लिंक या मोबाइल लिंक बनाना चाहिए. यह लिंक, Android डिवाइस पर खोला जाता है
handleCodeInApp (boolean|undefined) ईमेल ऐक्शन लिंक, पहले मोबाइल ऐप्लिकेशन में खुलेगा या वेब लिंक में. डिफ़ॉल्ट रूप से, यह गलत पर सेट होता है. अगर इसे 'सही है' पर सेट किया जाता है, तो ऐक्शन कोड लिंक को यूनिवर्सल लिंक या Android ऐप्लिकेशन लिंक के तौर पर भेजा जाएगा. अगर ऐप्लिकेशन इंस्टॉल है, तो यह लिंक ऐप्लिकेशन में खुलेगा . अगर इसे 'गलत है' पर सेट किया जाता है, तो कोड पहले वेब विजेट पर भेजा जाएगा. इसके बाद, जारी रखने पर, अगर ऐप्लिकेशन इंस्टॉल है, तो यह ऐप्लिकेशन पर रीडायरेक्ट हो जाएगा.
linkDomain (string|undefined) जब किसी प्रोजेक्ट के लिए, होस्टिंग लिंक के कस्टम डोमेन तय किए जाते हैं, तो यह तय करें कि जब लिंक को किसी खास मोबाइल ऐप्लिकेशन से खोला जाना हो, तो किस डोमेन का इस्तेमाल किया जाए. ऐसा न करने पर, डिफ़ॉल्ट डोमेन अपने-आप चुना जाता है. उदाहरण के लिए, PROJECT_ID.firebaseapp.com).
dynamicLinkDomain (string|undefined) अब इस्तेमाल में नहीं है. यह पैरामीटर तय न करें.

यहां दिए गए उदाहरण में, ईमेल की पुष्टि करने वाला लिंक भेजने का तरीका बताया गया है. यह लिंक, पहले मोबाइल ऐप्लिकेशन में खुलेगा. डीप लिंक में, जारी रखने के यूआरएल का पेलोड https://www.example.com/checkout?cartId=1234 शामिल होगा. होस्टिंग लिंक के लिए इस्तेमाल किया गया कस्टम डोमेन Hosting है. इसे custom-domain.com के साथ इस्तेमाल करने के लिए कॉन्फ़िगर करना होगा. Firebase Hosting

Node.js

const actionCodeSettings = {
  // URL you want to redirect back to. The domain (www.example.com) for
  // this URL must be whitelisted in the Firebase Console.
  url: 'https://www.example.com/checkout?cartId=1234',
  // This must be true for email link sign-in.
  handleCodeInApp: true,
  iOS: {
    bundleId: 'com.example.ios',
  },
  android: {
    packageName: 'com.example.android',
    installApp: true,
    minimumVersion: '12',
  },
  // The domain must be configured in Firebase Hosting and owned by the project.
  linkDomain: 'custom-domain.com',
};

Java

ActionCodeSettings actionCodeSettings = ActionCodeSettings.builder()
    .setUrl("https://www.example.com/checkout?cartId=1234")
    .setHandleCodeInApp(true)
    .setIosBundleId("com.example.ios")
    .setAndroidPackageName("com.example.android")
    .setAndroidInstallApp(true)
    .setAndroidMinimumVersion("12")
    .setDynamicLinkDomain("coolapp.page.link")
    .build();

Python

action_code_settings = auth.ActionCodeSettings(
    url='https://www.example.com/checkout?cartId=1234',
    handle_code_in_app=True,
    ios_bundle_id='com.example.ios',
    android_package_name='com.example.android',
    android_install_app=True,
    android_minimum_version='12',
    dynamic_link_domain='coolapp.page.link',
)

Go

actionCodeSettings := &auth.ActionCodeSettings{
	URL:                   "https://www.example.com/checkout?cartId=1234",
	HandleCodeInApp:       true,
	IOSBundleID:           "com.example.ios",
	AndroidPackageName:    "com.example.android",
	AndroidInstallApp:     true,
	AndroidMinimumVersion: "12",
	DynamicLinkDomain:     "coolapp.page.link",
}

C#

var actionCodeSettings = new ActionCodeSettings()
{
    Url = "https://www.example.com/checkout?cartId=1234",
    HandleCodeInApp = true,
    IosBundleId = "com.example.ios",
    AndroidPackageName = "com.example.android",
    AndroidInstallApp = true,
    AndroidMinimumVersion = "12",
    LinkDomain = "coolapp.page.link",
};

ज़्यादा जानने के लिए, ईमेल ऐक्शन में स्टेट पास करना देखें.

पासवर्ड रीसेट करने के लिए ईमेल लिंक जनरेट करना

पासवर्ड रीसेट करने का लिंक जनरेट करने के लिए, मौजूदा उपयोगकर्ता का ईमेल और ActionCodeSettings का कोई ऑब्जेक्ट दें. यह ऑब्जेक्ट देना ज़रूरी नहीं है. इस कार्रवाई से, ईमेल ऐक्शन लिंक मिलेगा. इस्तेमाल किया गया ईमेल, किसी मौजूदा उपयोगकर्ता का होना चाहिए.

Node.js

// Admin SDK API to generate the password reset link.
const userEmail = 'user@example.com';
getAuth()
  .generatePasswordResetLink(userEmail, actionCodeSettings)
  .then((link) => {
    // Construct password reset email template, embed the link and send
    // using custom SMTP server.
    return sendCustomPasswordResetEmail(userEmail, displayName, link);
  })
  .catch((error) => {
    // Some error occurred.
  });

Java

String email = "user@example.com";
try {
  String link = FirebaseAuth.getInstance().generatePasswordResetLink(
      email, actionCodeSettings);
  // Construct email verification template, embed the link and send
  // using custom SMTP server.
  sendCustomEmail(email, displayName, link);
} catch (FirebaseAuthException e) {
  System.out.println("Error generating email link: " + e.getMessage());
}

Python

email = 'user@example.com'
link = auth.generate_password_reset_link(email, action_code_settings)
# Construct password reset email from a template embedding the link, and send
# using a custom SMTP server.
send_custom_email(email, link)

Go

email := "user@example.com"
link, err := client.PasswordResetLinkWithSettings(ctx, email, actionCodeSettings)
if err != nil {
	log.Fatalf("error generating email link: %v\n", err)
}

// Construct password reset template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link)

C#

var email = "user@example.com";
var link = await FirebaseAuth.DefaultInstance.GeneratePasswordResetLinkAsync(
    email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
SendCustomEmail(email, displayName, link);

लिंक जनरेट होने के बाद, इसे पासवर्ड रीसेट करने के लिए बनाए गए कस्टम ईमेल में जोड़ा जा सकता है. इसके बाद, कस्टम एसएमटीपी सर्वर का इस्तेमाल करके, इसे संबंधित उपयोगकर्ता को भेजा जा सकता है.

अगर पासवर्ड रीसेट करने के लिए डिफ़ॉल्ट लैंडिंग पेज का इस्तेमाल नहीं किया जा रहा है और अपना कस्टम हैंडलर बनाया जा रहा है, तो ईमेल ऐक्शन के लिए कस्टम हैंडलर बनाना देखें.

ईमेल की पुष्टि करने के लिए लिंक जनरेट करना

ईमेल की पुष्टि करने के लिए लिंक जनरेट करने के लिए, मौजूदा उपयोगकर्ता का ऐसा ईमेल दें जिसकी पुष्टि नहीं हुई है. साथ ही, ActionCodeSettings का कोई ऑब्जेक्ट दें. यह ऑब्जेक्ट देना ज़रूरी नहीं है. इस कार्रवाई से, ईमेल ऐक्शन लिंक मिलेगा. इस्तेमाल किया गया ईमेल, किसी मौजूदा उपयोगकर्ता का होना चाहिए.

Node.js

// Admin SDK API to generate the email verification link.
const useremail = 'user@example.com';
getAuth()
  .generateEmailVerificationLink(useremail, actionCodeSettings)
  .then((link) => {
    // Construct email verification template, embed the link and send
    // using custom SMTP server.
    return sendCustomVerificationEmail(useremail, displayName, link);
  })
  .catch((error) => {
    // Some error occurred.
  });

Java

String email = "user@example.com";
try {
  String link = FirebaseAuth.getInstance().generateEmailVerificationLink(
      email, actionCodeSettings);
  // Construct email verification template, embed the link and send
  // using custom SMTP server.
  sendCustomEmail(email, displayName, link);
} catch (FirebaseAuthException e) {
  System.out.println("Error generating email link: " + e.getMessage());
}

Python

email = 'user@example.com'
link = auth.generate_email_verification_link(email, action_code_settings)
# Construct email from a template embedding the link, and send
# using a custom SMTP server.
send_custom_email(email, link)

Go

email := "user@example.com"
link, err := client.EmailVerificationLinkWithSettings(ctx, email, actionCodeSettings)
if err != nil {
	log.Fatalf("error generating email link: %v\n", err)
}

// Construct email verification template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link)

C#

var email = "user@example.com";
var link = await FirebaseAuth.DefaultInstance.GenerateEmailVerificationLinkAsync(
    email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
SendCustomEmail(email, displayName, link);

लिंक जनरेट होने के बाद, इसे पुष्टि करने के लिए बनाए गए कस्टम ईमेल में जोड़ा जा सकता है. इसके बाद, कस्टम एसएमटीपी सर्वर का इस्तेमाल करके, इसे संबंधित उपयोगकर्ता को भेजा जा सकता है.

अगर ईमेल की पुष्टि करने के लिए डिफ़ॉल्ट लैंडिंग पेज का इस्तेमाल नहीं किया जा रहा है और अपना कस्टम हैंडलर बनाया जा रहा है, तो ईमेल ऐक्शन के लिए कस्टम हैंडलर बनाना देखें.

साइन-इन करने के लिए ईमेल लिंक जनरेट करना

ईमेल लिंक से साइन-इन करने की सुविधा का इस्तेमाल करके, उपयोगकर्ताओं की पुष्टि करने से पहले, आपको अपने Firebase प्रोजेक्ट के लिए ईमेल लिंक से साइन-इन करने की सुविधा चालू करनी होगी.

साइन-इन करने का लिंक जनरेट करने के लिए, उपयोगकर्ता का ईमेल और ActionCodeSettings का कोई ऑब्जेक्ट दें. इस मामले में, ActionCodeSettings ऑब्जेक्ट की ज़रूरत होती है, ताकि यह जानकारी दी जा सके कि साइन-इन पूरा करने के लिए लिंक पर क्लिक करने के बाद, उपयोगकर्ता को कहां वापस भेजना है. इस कार्रवाई से, ईमेल ऐक्शन लिंक मिलेगा.

पासवर्ड रीसेट करने और ईमेल की पुष्टि करने के उलट, इस्तेमाल किया गया ईमेल किसी मौजूदा उपयोगकर्ता का होना ज़रूरी नहीं है. ऐसा इसलिए, क्योंकि इस कार्रवाई का इस्तेमाल, ईमेल लिंक के ज़रिए आपके ऐप्लिकेशन में नए उपयोगकर्ताओं को साइन अप करने के लिए किया जा सकता है.

Node.js

// Admin SDK API to generate the sign in with email link.
const useremail = 'user@example.com';
getAuth()
  .generateSignInWithEmailLink(useremail, actionCodeSettings)
  .then((link) => {
    // Construct sign-in with email link template, embed the link and
    // send using custom SMTP server.
    return sendSignInEmail(useremail, displayName, link);
  })
  .catch((error) => {
    // Some error occurred.
  });

Java

String email = "user@example.com";
try {
  String link = FirebaseAuth.getInstance().generateSignInWithEmailLink(
      email, actionCodeSettings);
  // Construct email verification template, embed the link and send
  // using custom SMTP server.
  sendCustomEmail(email, displayName, link);
} catch (FirebaseAuthException e) {
  System.out.println("Error generating email link: " + e.getMessage());
}

Python

email = 'user@example.com'
link = auth.generate_sign_in_with_email_link(email, action_code_settings)
# Construct email from a template embedding the link, and send
# using a custom SMTP server.
send_custom_email(email, link)

Go

email := "user@example.com"
link, err := client.EmailSignInLink(ctx, email, actionCodeSettings)
if err != nil {
	log.Fatalf("error generating email link: %v\n", err)
}

// Construct sign-in with email link template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link)

C#

var email = "user@example.com";
var link = await FirebaseAuth.DefaultInstance.GenerateSignInWithEmailLinkAsync(
    email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
SendCustomEmail(email, displayName, link);

लिंक जनरेट होने के बाद, इसे साइन-इन करने के लिए बनाए गए कस्टम ईमेल में जोड़ा जा सकता है. इसके बाद, कस्टम एसएमटीपी सर्वर का इस्तेमाल करके, इसे संबंधित उपयोगकर्ता को भेजा जा सकता है.

ईमेल लिंक का इस्तेमाल करके, Firebase से उपयोगकर्ताओं की पुष्टि करने के बारे में ज़्यादा जानें . इससे यह जानकारी मिलेगी कि उपयोगकर्ता के लिंक पर क्लिक करने और ऐप्लिकेशन पर वापस रीडायरेक्ट होने के बाद, साइन-इन कैसे पूरा किया जाए.