简体   繁体   English

firebase.auth()。signInWithPhoneNumber +在Node.js上表达

[英]firebase.auth().signInWithPhoneNumber + express on Node.js

I've got a trouble when trying to implement backend firebase Authorization. 尝试实施后端Firebase授权时遇到麻烦。 After I had read carefully the docs ( https://firebase.google.com/docs/auth/web/phone-auth ), I figured out that Authorization need to be proceded by two steps: 在仔细阅读了文档( https://firebase.google.com/docs/auth/web/phone-auth )之后,我发现必须通过两个步骤来进行授权:

  1. send phone 发送电话
  2. send code 发送代码

I divided Google's example on two promises, but can't understand how to store current user and whether or not I've done everything in an appropriate way. 我将Google的示例分为两个承诺,但不了解如何存储当前用户以及我是否以适当的方式完成了所有工作。

  app.post("/appSignInByPhone", (req, res) => {
        let {phoneNumber, applicationVerifier} = req.body;
        firebase.auth().signInWithPhoneNumber(phoneNumber, applicationVerifier)
            .then(() => res.end("Waiting for code"))
            .catch(
                error => res.json(error)
            );
});

app.post("/appSignInPhoneVerify", (req, res) => {
        let {verificationCode} = req.body;
            firebase.auth.ConfirmationResult.confirm(verificationCode)
            .then( user => res.json(user))
            .catch(
                error => res.json(error)
            );
});

Maybe there are some ways of merging these to request to one... 也许有一些方法可以合并这些请求到一个...

Try the following: 请尝试以下操作:

In step #1 either store or send back the confirmationResult you get from the fulfillment handler: 在#1存储或发送回confirmationResult你履行处理程序得到:

    firebase.auth().signInWithPhoneNumber(phoneNumber, applicationVerifier)
        .then((confirmationResult) => {
               // either store confirmationResult 
               // or send it to the client and ask for it in step #2)
         }).catch(
            error => res.json(error)
        );

then in step #2: 然后在步骤2中:

     // get the confirmationResult from the client or from some storage
     // in the server
     // for example let { verificationCode, confirmationResult } = req.body;
        confirmationResult.confirm(verificationCode)
        .then( user => res.json(user))
        .catch(
            error => res.json(error)
        );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM