kreait / firebase-php

Unofficial Firebase Admin SDK for PHP
https://firebase-php.readthedocs.io/
MIT License
2.26k stars 430 forks source link

Auth using Emulator with demo project #844

Open Ticlext-Altihaf opened 11 months ago

Ticlext-Altihaf commented 11 months ago

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', // XXXX@example.com
    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)
jeromegamez commented 11 months ago

Yes, you can read about it here:

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

Ticlext-Altihaf commented 11 months ago

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

jeromegamez commented 11 months ago

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...

cmenkemeller commented 2 weeks ago

@jeromegamez Did you ever find a solution for this? I am running into a similar issue where verifyIdToken() is making calls to google apis even though I'm pointing to the Firebase Emulators.

Offline fails with curl error - Next Kreait\Firebase\Exception\Database\ApiConnectionFailed: Unable to connect to the API: cURL error 6: Could not resolve host: oauth2.googleapis.com (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://oauth2.googleapis.com/token in ...vendor/kreait/firebase-php/src/Firebase/Exception/DatabaseApiExceptionConverter.php:37