firebase / firebase-functions

Firebase SDK for Cloud Functions
https://firebase.google.com/docs/functions/
MIT License
1.01k stars 202 forks source link

Auth onCreate trigger doesn't trigger in emulators with magic link sign in flow #1565

Closed pschoffer closed 1 month ago

pschoffer commented 1 month ago

Related issues

[REQUIRED] Version info

node:

$ node --version v18.17.1 firebase-functions: 5.0.1

firebase-tools: $ firebase --version 13.7.2

firebase-admin:11.8.0

[REQUIRED] Test case

1) Setup User function:

import * as v1Functions from 'firebase-functions/v1';

export const setupNewUser = v1Functions.region(region).auth.user().onCreate(async user => {
    const { uid, email } = user;
    await admin.firestore().collection(COLLECTIONS.USERS).doc(uid).set({ uid, email });
    v1Functions.logger.info(`New user: ${email} (${uid})`);
});

2) Create user via link login:

    import { sendSignInLinkToEmail } from 'firebase/auth';
   ....
    const handleLogin = async (data: Inputs) => {
        const url = window.location.href + `?signinFinished=true`;
        await sendSignInLinkToEmail(auth, data.email, {
            url,
            handleCodeInApp: true
        });
    }

3) user gets logged in and created in auth emulator. The emulator output:

⚠  canHandleCodeInApp is unsupported in Auth Emulator. All OOB operations will complete via web.
i  To sign in as schoffer.pavel@gmail.com, follow this link: http://127.0.0.1:9099/emulator/action?mode=signIn&lang=en&oobCode=ygnXe-FGeJbfOmrGaessvJzEVl-lwk-HKfZzllWG-8Kvkgm5mSreqY&apiKey=fake-api-key&continueUrl=http%3A%2F%2Flocalhost%3A3000%2Fbilling_plan%3Femail_signin_finished%3Dtrue

The user now exists in auth emulator: image

[REQUIRED] Steps to reproduce

[REQUIRED] Expected behavior

Expected: setupNewUser function gets triggered.

[REQUIRED] Actual behavior

Actual: setupNewUser is not triggered.

Note: setupNewUser function gets triggered as expected if user is created via emulators UI directly as well in production once deployed. This issue seems to be limited to combination of auth emulators and login via link method.

Were you able to successfully deploy your functions?

Nope. All works correctly when deployed.

pschoffer commented 1 month ago

I am happy to investigate further, but could use some pointers to how these triggers work under the hood.

pschoffer commented 1 month ago

Oh my bad. I had an initEmulators script that ran too early for the functions to be bootstraped probably and that created the user in the first place not the sign in link. Ignore me:)