Vonage / vonage-laravel

Laravel Service Provider for integrating Vonage APIs
Apache License 2.0
33 stars 8 forks source link

Typed property Vonage\\Client\\Credentials\\Container::$credentials must not be accessed before initialization #9

Closed daniel-for-j closed 1 year ago

daniel-for-j commented 1 year ago

Verifying a user with Vonage client package on Laravel, everything works fine on local but once I update to the server it throws

"Typed property Vonage\Client\Credentials\Container::$credentials must not be accessed before"

I have updated my .env with VONAGE_API_KEY='XXXXXX' VONAGE_API_SECRET='XXXXXXXXXXXXXX'

My Controller ` $basic = new Basic(env('VONAGE_API_KEY'),env('VONAGE_API_SECRET')); $client = new Client(new Container($basic)); $otpRequest = new OtpRequest($inputData['mobile_number'], "COMPANY");

        // choose PIN length (4 or 6)
        $otpRequest->setCodeLength(4);

        // set locale
        // $otpRequest->setCountry('ng');

        $response = $client->verify()->start($otpRequest);

`

It breaks at the very second line.

I will appreciate any help rendered

daniel-for-j commented 1 year ago

I solved this by changing the variable in vonage/client-core/src/Client/Credentials/Container.php from protected array $credentials; to protected array $credentials = []

Hope it helps!

SecondeJK commented 1 year ago

This isn't a bug, more of an implementation problem. The reason the error is thrown is because you haven't passed in environment variables on your server - so the "fix" isn't actually a fix (PHP8 errors if you attempt to initialize a property before it is set, if there is no default).

This means all of your verifications will actually be erroring on the server, and I expect it is because you are using env(). It is not recommended to use env() over the Laravel config() helper - I'd suggest moving these over.

At any rate, the root of the problem is not the library, it's how you fetch credentials.