I am trying to create a React application that uses an "onSubmit" button to trigger handleSignUp()
. However, each time I call handleSignUp()
it gives this error
TypeError: _base__WEBPACK_IMPORTED_MODULE_2__.default.auth.RecaptchaVerifier is not a constructor
firebase initialize -base.js
import * as firebase from "firebase/app";
import "firebase/auth";
const app = firebase.initializeApp({
apiKey: process.env.REACT_APP_FIREBASE_KEY,
authDomain: process.env.REACT_APP_FIREBASE_DOMAIN,
databaseURL: process.env.REACT_APP_FIREBASE_DATABASE,
projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.REACT_APP_FIREBASE_SENDER_ID
});
export default app;
SignUp.js
import React, { Component } from "react";
import { withRouter } from "react-router";
import app from "./base";
class SignUp extends Component {
componentDidMount() {
window.recaptchaVerifier = new app.auth.RecaptchaVerifier(this.recaptcha, {
size: "normal",
callback: function(response) {
console.log("reCAPTCHA solved, allow signInWithPhoneNumber.");
},
"expired-callback": function() {
console.log("Response expired. Ask user to solve reCAPTCHA again.");
}
});
window.recaptchaVerifier.render().then(function(widgetId) {
window.recaptchaWidgetId = widgetId;
});
}
handleSignUp = async () => {
var phoneNumber = "+16505554567";
var appVerifier = new app.auth.RecaptchaVerifier("recaptcha-container");
try {
await app
.auth()
.signInWithPhoneNumber(phoneNumber, appVerifier)
.then(function(confirmationResult) {
window.confirmationResult = confirmationResult;
});
} catch (error) {
alert(error);
}
};
render() {
return (
<div ref={ref => (this.recaptcha = ref)}>
<h1>Sign up</h1>
<form onSubmit={this.handleSignUp}>
<button type="submit">Sign Up</button>
</form>
</div>
);
}
}
export default withRouter(SignUp);
and also run var appVerifier = new app.auth.RecaptchaVerifier("recaptcha-container");
automatically refresh the web page
I spent 3hrs on this and finally found the issue. When you initialize firebase app like
app.initializeApp(config);
then you can call methods using: app.auth()
app.auth().createUserWithEmailAndPassword(email, password);
and to create RecaptchaVerifier instance, you must use: app.auth
window.recaptchaVerifier = new app.auth.RecaptchaVerifier('captcha-container', {
'size': 'invisible'
});
I'm also face this issue and resolved. This code absolutely help to signIn using firebase phone auth
Firebase.js
import * as Firebase from 'firebase';
const app = firebase.initializeApp({
apiKey: process.env.REACT_APP_FIREBASE_KEY,
authDomain: process.env.REACT_APP_FIREBASE_DOMAIN,
databaseURL: process.env.REACT_APP_FIREBASE_DATABASE,
projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.REACT_APP_FIREBASE_SENDER_ID
});
Firebase.initializeApp(app);
export default Firebase;
signup.js
import React, { useState } from 'react';
import Firebase from './container/Firebase';
import 'react-phone-number-input/style.css'
import PhoneInput from 'react-phone-number-input'
const Signup= () => {
const [value, setValue] = useState(0);
function setuprecaptcha (){
window.recaptchaVerifier = new Firebase.auth.RecaptchaVerifier('recaptcha-container', {
size: 'invisible',
callback: function (response) {
console.log("recature resolved")
this.onSignInSubmit();
}
});
}
function onSignInSubmit(event) {
event.preventDefault();
setuprecaptcha();
var phoneNumber = value;
var appVerifier = window.recaptchaVerifier;
Firebase.auth().signInWithPhoneNumber(phoneNumber, appVerifier)
.then(function (confirmationResult) {
console.log("Success");
// SMS sent. Prompt user to type the code from the message, then sign the
// user in with confirmationResult.confirm(code).
window.confirmationResult = confirmationResult;
var verificationId = window.prompt("Enter otp")
confirmationResult
.confirm(verificationId)
.then(function (result) {
// User signed in successfully.
var user = result.user;
user.getIdToken().then(idToken => {
window.localStorage.setItem('idToken', idToken);
console.log(idToken);
});
})
.catch(function (error) {
// User couldn't sign in (bad verification code?)
console.error("Error while checking the verification code", error);
window.alert(
"Error while checking the verification code:\n\n" +
error.code +
"\n\n" +
error.message
);
});
})
.catch(function (error) {
console.log("sign Up error:" + error.code);
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With