Monday, May 20, 2024
157
rated 0 times [  158] [ 1]  / answers: 1 / hits: 23524  / 7 Years ago, sun, august 13, 2017, 12:00:00

I am trying to create Google authentication but I copy var provider = new firebase.auth.GoogleAuthProvider(); to my JavaScript and it keeps telling me that Uncaught ReferenceError: firebase is not defined.



Here is my code:



var provider = new firebase.auth.GoogleAuthProvider();

function signin(){

firebase.auth().signInWithPopup(provider).then(function(result) {

var token = result.credential.accessToken;
var user = result.user;

}).catch(function(error) {

var errorCode = error.code;
var errorMessage = error.message;
var email = error.email;
var credential = error.credential;

});
}

More From » authentication

 Answers
159

You are getting this error because firebase variable has not been instantiated and initialized on the page on which you are running the code. To do so you need to -




  1. Include the relevant scripts-



    <script src=https://www.gstatic.com/firebasejs/4.1.3/firebase.js></script>
    <script src=https://www.gstatic.com/firebasejs/4.1.3/firebase-auth.js></script>




firebase.js contains the variable firebase representing your firebase app. firebase-auth.js contains the firebase.auth library which your code is using.




  1. Initialize your app using -



    var config = {
    apiKey: <API_KEY>,
    authDomain: <PROJECT_ID>.firebaseapp.com,
    databaseURL: https://<DATABASE_NAME>.firebaseio.com,
    storageBucket: <BUCKET>.appspot.com,
    messagingSenderId: <SENDER_ID>,
    };

    firebase.initializeApp(config);



To learn more refer - https://firebase.google.com/docs/web/setup


[#56789] Wednesday, August 9, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zachariaho

Total Points: 34
Total Questions: 87
Total Answers: 100

Location: England
Member since Tue, Sep 8, 2020
4 Years ago
;