Ric
Ric

Reputation: 65

firebase.auth is not a function - with multiple firebase app initialization

I am getting the error 'Uncaught TypeError: firebase.auth is not a function'.

My code was working perfectly, and then I implemented my clients api, and discovered that their api already uses and has initialized a different firebase app. So, as I couldn't initialise my firebase app in the usual way (ie. firebase.initializeApp(config) ), I followed the documentation on initializing multiple firebase apps as follows. (This results in the error)

var configOther = {
apiKey: "xxxx",
authDomain: "xxxx",
databaseURL: "xxxx",
projectId: "xxxx",
storageBucket: "xxxx",
messagingSenderId: "xxxx"
};

var firebaseOther = firebase.initializeApp(configOther, "firebaseOther");

console.log("auth: ", firebase.auth(firebaseOther)); //"Uncaught TypeError: firebase.auth is not a function"

Any ideas about what I'm doing wrong here? Many thanks.

Note - I have also tried the following shorthand notation:

firebaseOther.auth() 

instead of

firebase.auth(firebaseOther)

but this results in the same error.

Upvotes: 3

Views: 14115

Answers (3)

Jan Wirth
Jan Wirth

Reputation: 460

I had a similar problem which was solved by removing defer in the HTML that was given by the documentation:

        <script src="https://www.gstatic.com/firebasejs/7.17.2/firebase-app.js"></script>
        <script defer src="https://www.gstatic.com/firebasejs/7.17.2/firebase-auth.js"></script>
        <script defer src="https://www.gstatic.com/firebasejs/7.17.2/firebase-firestore.js"></script>

Upvotes: 2

Phil
Phil

Reputation: 164752

The only explanation is that somewhere in the page, there is another <script> tag referencing the firebase-app.js file, eg

<script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-app.js"></script>

This tag will be appearing after your inclusion of firebase.js or firebase-app.js and firebase-auth.js.

What happens is the inclusion of firebase-app.js sets the value of the global firebase variable, overriding anything that was previously set.

The solution is to either remove the duplicate Firebase script inclusions or at the very least, make sure the ones you want active are included last.

Upvotes: 4

Doug Stevenson
Doug Stevenson

Reputation: 317392

You likely skipped the step for including the firebase-auth script in your page, as described by the documentation:

<!-- Firebase App is always required and must be first -->
<script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-app.js"></script>

<!-- Add additional services that you want to use -->
<script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-auth.js"></script>

Upvotes: 11

Related Questions