我在通过电话号码登录使用 React 和 Firebase 身份验证时遇到了挑战。
这是我 following 的文档。
问题是我有两个(组件):
我的挑战是,一旦我生成了代码,当我导航到验证代码的组件时,如果不再次调用 signInWithPhoneNumber 方法,我就无法做到这一点。
有没有一种方法可以在不调用函数两次的情况下确认代码?
有关更多上下文,这是生成和发送代码的代码:
const phoneNumber = getPhoneNumberFromUserInput();
const appVerifier = window.recaptchaVerifier;
firebase.auth().signInWithPhoneNumber(phoneNumber, appVerifier)
.then((confirmationResult) => {
// SMS sent. Prompt user to type the code from the message, then sign the
// user in with confirmationResult.confirm(code).
window.confirmationResult = confirmationResult;
// ...
}).catch((error) => {
// Error; SMS not sent
// ...
});
这是确认验证码的代码:
const code = getCodeFromUserInput();
confirmationResult.confirm(code).then((result) => {
// User signed in successfully.
const user = result.user;
// ...
}).catch((error) => {
// User couldn't sign in (bad verification code?)
// ...
});
我将不胜感激任何形式的建议。谢谢!