kreait / firebase-bundle

A Symfony Bundle for the Firebase PHP Admin SDK
https://github.com/kreait/firebase-php
MIT License
135 stars 25 forks source link

Cannot autowire "Kreait\Firebase\Contract\Auth" #40

Closed Julen10 closed 2 years ago

Julen10 commented 2 years ago

The problem

When I try to inject Auth from Kreait\Firebase\Contract\Auth an error shows:

Cannot autowire service "App\Service\FirebaseService": argument "$auth" of method "__construct()" references interface "Kreait\Firebase\Contract\Auth" but no such service exists. Did you create a class that implements this interface?

Environment

Details

I just follow the instructions below

Code to reproduce issue

<?php

namespace App\Service;

use Kreait\Firebase\Auth\UserRecord;
use Kreait\Firebase\Contract\Auth;
use Kreait\Firebase\Exception\Auth\FailedToVerifyToken;
use Kreait\Firebase\Exception\AuthException;
use Kreait\Firebase\Exception\FirebaseException;

class FirebaseService
{
    private Auth $auth;

    public function __construct(Auth $auth) {
        $this->auth = $auth;
    }

    /**
     * @throws FirebaseException
     * @throws AuthException
     */
    public function verifyIdToken(string $idToken): UserRecord
    {
        try {
            $verifiedIdToken = $this->auth->verifyIdToken($idToken);
        } catch (FailedToVerifyToken $e) {
            echo 'The token is invalid: '.$e->getMessage();
        }

        $uid = $verifiedIdToken->claims()->get('sub');

        $user = $this->auth->getUser($uid);
        return $user;
    }
}
jeromegamez commented 2 years ago

Hey, and thank you for raising this issue. Unfortunately, I'm not able to reproduce it - the same way as you described is used in https://github.com/jeromegamez/firebase-php-examples, specifically in https://github.com/jeromegamez/firebase-php-examples/blob/main/src/Controller/UserController.php

I just tested it locally with different PHP versions, including PHP 8, and am not able to reproduce the problem :/ - could you check your project's configuration with the example project? Perhaps there's a difference that isn't obvious at first glance 🤞

Julen10 commented 2 years ago

I just moved all the changes to another branch, take again the "main" branch, require the package (I had to make it with the -W option because some of the required packages are behind mines) and it seems to work now... The configuration is exactly the same in both projects... Thanks anyways