question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Example of how to test auth triggers

See original GitHub issue

Hi, now that we have the auth emulator, it would be nice to have an example test that showcases how to work with the auth emulator on the test suit. I’ve been trying to get this to work, without success.

const firebase = require('@firebase/testing');

// Project id passed by emulators:exec.
const PROJECT_ID = process.env.GCLOUD_PROJECT;
const CREDS = { uid: TEST_USER_ID };

// To get a logged in user pass the CREDS object as param, or null for unauthenticated requests.
const getDB = (creds) => firebase.initializeTestApp({ projectId: PROJECT_ID, auth: creds }).firestore();

// Admin tool to bypass rules and generate data in the db.
const admin = firebase.initializeAdminApp({ projectId: PROJECT_ID });
const getAdminDB = () => admin.firestore();
const getAuth = () => admin.auth();

describe("The cloud functions backend", () => {

    describe("when a user is created", () => {

        it("populates some fields for that user document", async () => {
            const admin = getAdminDB();
            const auth = getAuth();
            const phone = "+11234567891";
            const photo = "https://foo.bar";

            const user = await auth.createUser({ phoneNumber: phone, photoURL: photo, uid: "123" });
            const userRef = admin.collection("users").doc("123");

            await new Promise((resolve) => {
                unsubscribe = userRef.onSnapshot(snap => {
                    const { phoneNumber, photoURL } = snap.data();
                    expect(phoneNumber).to.eq(phone);
                    expect(photoURL).to.eq(photo);
                    unsubscribe();
                    resolve();
                });
            });
        });

    });

});

The error is:

Your API key is invalid, please check you have copied it correctly.

Could it be that the FIREBASE_AUTH_EMULATOR_HOST is not being set by firebase emulators:exec? and need to be specified explicitly? Like: auth().useEmulator("localhost:9099")?

Appreciate some ideas on how to troubleshoot or some code snippets that use the auth emulator in tests. Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
samtsterncommented, Nov 9, 2020

@hernantz I feel like you’re exactly one step ahead of me! You’re running into two bugs that I am working on today:

  1. FIREBASE_AUTH_EMULATOR_HOST missing in emulators:exec - https://github.com/firebase/firebase-tools/pull/2800
  2. firebase-admin requires credentials for auth operations even when talking to the emulator - https://github.com/firebase/firebase-admin-node/pull/1085

The good news is they should be fairly simple to workaround until I fix them:

  1. Set the env var yourself
  2. Run gcloud auth application-default login on your development machine
0reactions
samtsterncommented, Nov 19, 2020

@hernantz thanks for following up! Glad to hear this was solved. I’ll add an auth example to this repo now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Firebase Authentication triggers - Cloud Functions
Firebase Authentication triggers ... You can trigger Cloud Functions in response to the creation and deletion of Firebase user accounts. For example, you...
Read more >
Auth-check and service-check triggers - Perforce
Triggers of type auth-check fire when standard or operator users run the p4 login command. Similarly, service-check triggers fire when service users users ......
Read more >
Firebase Functions Tutorial #8 - Auth Triggers - YouTube
Hey gang, in this Firebase Functions tutorial we'll take a look at auth triggers - functions which run when a new user signs...
Read more >
How to create authentication for triggers and actions using ...
1. mock_input: Lets you add sample input data which can be used to test your authentication. 2. validate: Lets you add validation logic...
Read more >
Define Auth challenge Lambda trigger - Amazon Cognito
Amazon Cognito invokes your define auth challenge Lambda trigger with an initial session that contains challengeName: SRP_A and challengeResult: true .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found