如何在Firebase身份验证电子邮件中发送window.location.pathname?

时间:2020-11-08 15:22:01

标签: javascript reactjs firebase firebase-authentication

我正在尝试发送与Firebase的身份验证链接。 这是Firebase文档上的代码。

export const CODE_SETTINGS = {
  // URL you want to redirect back to. The domain (www.example.com) for this
  // URL must be in the authorized domains list in the Firebase Console.
  url: 'http://localhost:3000/finishedSignUp',
  handleCodeInApp: true,
};

这是我的邮件发送功能。它接受我需要发送电子邮件的电子邮件。

export const startEmailAuth = (email: string) => {
  firebase
    .auth()
    .sendSignInLinkToEmail(email, ACTION_CODE_SETTINGS)
    .then(function(result) {
      // The link was successfully sent. Inform the user.
      // Save the email locally so you don't need to ask the user for it again
      // if they open the link on the same device.
      window.localStorage.setItem('emailForSignIn', email);
      // TODO: make a nicer UI here.
      alert(`Verification email has been sent to ${email}`);
    })
    .catch(function(error) {
      console.error(error);
    });
};

我的问题是,如果我将代码更改为类似这样的内容。

export const startEmailAuth = (email: string, location: string) => {

   firebase ......
}

我应该在哪里放置位置以便能够将位置信息与电子邮件一起发送。

1 个答案:

答案 0 :(得分:2)

如果我正确理解了您的问题,则应在actionCodeSettings方法的第二个参数传递的sendSignInLinkToEmail()对象中传递“ continue url”值,请参阅文档herehere

更准确地说,您需要将此值分配给url对象的actionCodeSettings属性。使用window.location.pathname建立此值。

  export const startEmailAuth = (email: string, location: string) => {
    const ACTION_CODE_SETTINGS = {
      url: location,
      // ....
    };

    firebase
      .auth()
      .sendSignInLinkToEmail(email, ACTION_CODE_SETTINGS)
      .then(function (result) {
        // ...
      })
      .catch(function (error) {
        console.error(error);
      });
  };