【问题标题】:verifyPasswordResetCode is not a function firebase Admin SDKverifyPasswordResetCode 不是函数 firebase Admin SDK
【发布时间】:2020-04-03 12:26:51
【问题描述】:

我在我的网站“www.website.com/auth/**”上成功托管了 node.js/firebase 功能。

该代码是用于重置密码/电子邮件验证等的自定义电子邮件处理程序。

目前我只是想查看 oobCode 是否有效并将代码中给出的电子邮件返回给用户只是为了进行实验,在我的函数日志中我不断收到错误:

auth.verifyPasswordResetCode is not a function at resetPassword (/srv/index.js:51:8)

这是托管在我的 firebase 项目上的服务器代码:

const express = require('express');
const admin = require('firebase-admin');
const functions = require('firebase-functions')


admin.initializeApp({
 credential: admin.credential.applicationDefault(),
 databaseURL: "https://clipify-1b5ce.firebaseio.com"
});

const auth = admin.auth();

const app = express();

app.get('/', (req, res) => {
  res.send('Website in progress...');
});

app.get('/auth/**', (request, response) => {
  // Get the action to complete.
  var mode = request.query.mode;
  // Get the one time code to authenticate.
  var actionCode = request.query.oobCode;
  // (Optional) Get the contine URL .
  var continueURL = request.query.continueURL; 
  // (Optional) Get the language code.
  var lang = request.query.lang;


  // What mode is it?
  switch (mode){
    case 'resetPassword':
        // Reset password - display UI and init backend code.
        response.send(resetPassword(actionCode, continueURL, lang));
        break;
    case 'recoverEmail':
        // Recover email - display UI and init backend code.
        //recoverEmail(auth, actionCode, lang);
        break;
    case 'verifyEmail':
        // Verify email - display UI and init backend code. 
        //verifyEmail(auth, actionCode, continueURL, lang);
        break;
    default:
        //response.send('Error')
        response.send("Internal server error - no API token.")
  }
  });

  function resetPassword(actionCode, continueURL, lang){
   auth.verifyPasswordResetCode(actionCode).then(function(email) {
   var accountEmail = email;
   return accountEmail

  }).catch(function(error){
   return error
  });
  }


  exports.app = functions.https.onRequest(app);

【问题讨论】:

    标签: node.js firebase firebase-authentication google-cloud-functions


    【解决方案1】:

    实际上,在您的 Cloud Function 中,您使用的是 Firebase Admin Node.js SDK,而不是 JavaScript SDK,后者“使用 Firebase 服务实现了应用程序使用的客户端库”。

    您将在 Admin Node.js SDK Reference 中看到 verifyPasswordResetCode() 不是 Admin Node.js SDK Auth service 的方法,而是 JavaScript SDK 的方法。

    原因是包含发送“一次性代码”并使用此代码更新密码的工作流应该在前端而不是在后端发生-结束云函数。

    简而言之,在前端(在您的应用程序中),您验证用户收到的代码,如果代码有效,您将用户重定向到他/她将输入新密码的表单,您将使用它与验证代码一起调用confirmPasswordReset() 方法。

    【讨论】:

    • 嗨,@Renaud Tarnec 我将在这个项目上制作一个 IOS 应用程序,而不是 Web 应用程序。因此,如果用户忘记了他们的电子邮件,我希望他们收到一封电子邮件,该电子邮件将转到我的网站,然后验证代码并允许他们更改密码。你提到前端应该验证代码,是指我的应用还是我网站上的前端?
    • 此方法适用于三个 Firebase 客户端 SDK(Android、iOS 和 JS/Web)。所以这意味着它应该从你的 iOS 应用程序中完成。您将在 Swift 参考(或 Objective-C 参考)中找到完全相同的方法:firebase.google.com/docs/reference/swift/firebaseauth/api/…
    • 此外,您还可以在此处找到有关“完整概念”的更多详细信息:firebase.google.com/docs/auth/custom-email-handler。即使它是针对 JS SDK 进行描述的,您也可以对 iOS 执行相同的操作(AFAIK,我从未尝试过!:-))
    • 非常感谢,所以在检查代码后,我使用执行“auth.confirmPasswordReset”功能的 node.js 代码重定向我的网站?
    • “虽然 'verifyPasswordResetcode()' 是 node.js sdk 参考的一部分。” -> 是的,但这是 Firebase JavaScript SDK Reference For Node.js,而不是需要在 Cloud Function 中使用的 Firebase Admin Node.js SDK。
    猜你喜欢
    • 2018-03-20
    • 1970-01-01
    • 2018-08-07
    • 2018-05-02
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 2020-04-10
    相关资源
    最近更新 更多