Example of how to test auth triggers
See original GitHub issueHi, 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:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@hernantz I feel like you’re exactly one step ahead of me! You’re running into two bugs that I am working on today:
FIREBASE_AUTH_EMULATOR_HOST
missing inemulators:exec
- https://github.com/firebase/firebase-tools/pull/2800firebase-admin
requires credentials for auth operations even when talking to the emulator - https://github.com/firebase/firebase-admin-node/pull/1085The good news is they should be fairly simple to workaround until I fix them:
gcloud auth application-default login
on your development machine@hernantz thanks for following up! Glad to hear this was solved. I’ll add an auth example to this repo now.