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

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

Firebase क्लाइंट 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 (बूलियन|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',
  },
  // FDL custom domain.
  dynamicLinkDomain: 'coolapp.page.link',
};

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',
)

शुरू करें

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",
    DynamicLinkDomain = "coolapp.page.link",
};

ज़्यादा जानने के लिए, ईमेल कार्रवाइयों में स्टेटस पास करना लेख पढ़ें.

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

पासवर्ड रीसेट करने का लिंक जनरेट करने के लिए, मौजूदा उपयोगकर्ता का ईमेल पता और ActionCodeSettings ऑब्जेक्ट दें. हालांकि, 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)

शुरू करें

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 ऑब्जेक्ट दें. हालांकि, 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)

शुरू करें

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)

शुरू करें

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);

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

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