firebase-php icon indicating copy to clipboard operation
firebase-php copied to clipboard

Auth using Emulator with demo project

Open Ticlext-Altihaf opened this issue 1 year ago • 4 comments

Describe the feature you would like to see

Is it possible to use Firebase Emulator with demo Firebase project, it would be useful for CI testing where the current setup require creating new Firebase project and issue service account.

https://firebase.google.com/docs/emulator-suite/connect_auth#choose_a_firebase_project

Example from using official Admin SDK for Node.js, it works without requiring internet connection or having Firebase project. firebase emulators:start --project=demo-project

process.env.FIREBASE_AUTH_EMULATOR_HOST = "127.0.0.1:9099"
import { initializeApp } from 'firebase-admin/app';
import { getAuth } from 'firebase-admin/auth';

initializeApp({
    projectId: 'demo-project',
});

const newUserData = {
    email: Math.random()*10000 + '@example.com', // [email protected]
    emailVerified: false,
    phoneNumber: '+1' + Math.random()*10000000000, // +1XXXXXXXXXX
    password: 'secretPassword',
    displayName: 'John Doe',
    photoURL: 'http://www.example.com/12345678/photo.png',
    disabled: false,
};

const user = await getAuth().createUser(newUserData)

// Getting the ID token with signInWithPassword
let idToken = await fetch(`http://127.0.0.1:9099/identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=random`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        email: user.email,
        password: "secretPassword",
        returnSecureToken: true,
    }),
})

idToken = await idToken.json()
idToken = idToken.idToken

const userFromIdToken = await getAuth().verifyIdToken(idToken)

await getAuth().deleteUser(userFromIdToken.uid)

Ticlext-Altihaf avatar Nov 24 '23 05:11 Ticlext-Altihaf

Yes, you can read about it here:

https://firebase-php.readthedocs.io/en/stable/testing.html#using-the-firebase-emulator-suite

jeromegamez avatar Nov 24 '23 06:11 jeromegamez

Uh.. I think you misunderstood me, I did read the docs I can't find the guide where I can use Firebase Demo Project, yes it work with Real Firebase Project.

image

The code, where in Node.js SDK it work without credentials.

        $request = CreateUser::new()
            ->withEmail($email)
            ->withPhoneNumber($phoneNumber)
            ->withClearTextPassword($password)
            ->withDisplayName($faker->name);


        $firebaseUser = Firebase::auth()->createUser($request);

image

Ticlext-Altihaf avatar Nov 24 '23 06:11 Ticlext-Altihaf

Ah, I understand now, sorry for closing the issue so early 🙏🏻 (it's early here 😅)

I think this should be working, I'm running emulator tests with it, but seeing the demo- prefix... I've perhaps been doing it wrong the whole time 😬

https://github.com/kreait/firebase-php/blob/393a751a0a15291774070688cbfa4d0e954b9e54/.github/workflows/emulator-tests.yml#L71

I'll try to look into it as soon as I find the time, I'm currently working on the next major release for PHP 8.3, and I do have only limited time...

jeromegamez avatar Nov 24 '23 06:11 jeromegamez