在Firebase中生成恢复电子邮件链接

时间:2019-04-11 06:35:14

标签: javascript node.js firebase firebase-authentication firebase-admin

有什么方法可以在firebase / firebase-admin中创建自定义的restoreEmail链接吗?

我已经检查了文档和教程,没有。 任何帮助都会很棒!

2 个答案:

答案 0 :(得分:0)

据我了解,SDK中目前尚无解决方案。相反,我们采用的方法是使用admin.auth().generateSignInWithEmailLink(email, actionCodeSettings),然后将返回的mode中的linksignIn替换为recoverEmail

const updatedLink = link.replace('signIn', 'recoverEmail');

这使我们能够自定义auth处理程序操作,如Firbase文档中的Create the email action handler page所示。

现在,我们可以再次致电admin.auth().updateUser来将电子邮件重置为以前的电子邮件,以及我们数据库,商家和其他服务的更新。您还需要在updatedLink的查询中添加原始电子邮件。

const linkWithOriginalEmail = updatedLink.concat(`&email=${email}`)

希望有帮助,如果有人有更好的解决方案,我们很乐意与您讨论。

答案 1 :(得分:0)

我不确定我是否正确理解您的问题,但是如果您的目标是在有人使用 updateEmail 更改其身份验证电子邮件地址时 Firebase 发送的自动电子邮件中包含自定义链接,那么您可以定义一个Firebase 控制台中的自定义操作网址,例如https://example.com/__/auth/action 在 Authentication 部分,并将以下代码添加到定义的 url (ref. https://firebase.google.com/docs/auth/custom-email-handler)。

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(function(info) {
    // Get the restored email address.
    restoredEmail = info['data']['email'];

    // Revert to the old email.
    return auth.applyActionCode(actionCode);
  }).then(function() {
    // 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(function() {
      // Password reset confirmation sent. Ask user to check their email.
    }).catch(function(error) {
      // Error encountered while sending password reset code.
    });
  }).catch(function(error) {
    // Invalid code.
  });
}