menu

Questions & Answers

How can I use firebase without the emulator?

I am learning firebase and I checked this tutorial regards the authentication. (https://www.youtube.com/watch?v=rbuSx1yEgV8&t=502s). In this video, the emulator seems to be essential, however I want to communicate with the server. How do I do it? If I do not initialize the auth emulator ( by removing the connectEmulator() function) I just get the error 'auth/network-request-failed'.

    const firebaseConfig = {
        //...
    };
    
    const user = {
        email: 'user@test.me',
        password: 'test1234'
    }

    function func() {
        createUserWithEmailAndPassword(auth, user.email, user.password)
        .then((userCredential) => {
            const user = userCredential.user;
            console.log(user)
        })
        .catch((error) => {
            console.log(error)
            // ..
        });         
    }

As you can see from the minute 7:37 of that video, I am getting his issue! So I assume I am following the wrong approach. Can someone help me? I would be really grateful.

Answers(1) :

You should be able to authenticate with the server.

The emulator is optional. Personally I rarely use it, and essentially always use the real online Firebase server. However there are many steps before you are able to authenticate with the server.

Step 1. Check you have copied the configuration correctly

Go to this link, but replace PROJECT_ID with your actual project Id:

https://console.firebase.google.com/u/0/project/PROJECT_ID/settings/general/

Check that you have correctly copied the value of this into your app code from that page. If you have not "added an app", you may need to click "Add app", to get this config to display.

const firebaseConfig = { 
    ... blah blah ... 
};

Step 2. Check that you have enabled a "Sign-in provider"

Go to this link (again PROJECT_ID should be replaced by your project Id):

https://console.firebase.google.com/u/0/project/PROJECT_ID/authentication/providers

At least one of the providers needs to be switched on, like so:

At least one provider is switched on

Step 3. Your code looks good.

I assume you have set up auth correctly - we can't see that in the snippet above.

Please paste into your question the exact error message you are seeing on the console, as text.

You might want to intensify the debugging as follows:

function func() {
    console.log(`user: ${JSON.stringify(user,null,2)}`)
    createUserWithEmailAndPassword(auth, user.email, user.password)
    .then((userCredential) => {
        console.log(`userCredential.user: ${JSON.stringify(userCredential.user,null,2)}`)
    })
    .catch((error) => {
        console.error(error)
    });         
}

A small thing, but I suggest avoiding using the same variable name, user, for two different things. Javascript will keep them separate, but we as programmers sometimes will get muddled when looking back at the code.

Step 4. Make sure you have authorised the domain you are using.

Go to:

https://console.firebase.google.com/u/0/project/PROJECT_ID/authentication/settings

Make sure you have authorised the domain from which you are "calling" the Firebase server.

Only localhost is authorised

If your app is running on "127.0.0.1" instead of "localhost", you might need to add that IP address too. Or if you have deployed, the deployed domain.

Comments:
2023-01-07 20:29:52
Thank you for the answer. Everything is correct, in fact when I use the emulator it works. However I still have this issue.
2023-01-07 20:29:52
And step 3? What is the exact error message?