Laravel 9 and Twilio Verify V2 "...must be of type array, string given" Error

Viewed 386

I am setting up Twilio's Verify service to authenticate with a text number. The service works and sends a code to a cell phone, but when it gets to the verificationChecks part of the code, I receive the following error Twilio\Rest\Verify\V2\Service\VerificationCheckList::create(): Argument #1 ($options) must be of type array, string given, called in /Users/charles/Documents/Development/guard/app/Http/Controllers/Auth/RegisteredUserController.php on line 83.

If it helps, I am using the following tutorial on Twilio's site. And if you look at Twilio's API where their API still matches their tutorial document.

RegisteredUserController.php

...
protected function verify(Request $request)
{
    $data = $request->validate([
        'verification_code' => ['required', 'numeric'],
        'phone_number' => ['required', 'string'],
    ]);
    /* Get credentials from .env */
    $token = getenv("TWILIO_AUTH_TOKEN");
    $twilio_sid = getenv("TWILIO_SID");
    $twilio_verify_sid = getenv("TWILIO_VERIFY_SID");
    $twilio = new Client($twilio_sid, $token);
    $verification = $twilio->verify->v2->services($twilio_verify_sid)
        ->verificationChecks
        ->create($data['verification_code'], array('to' => $data['phone_number']));
    if ($verification->valid) {
        $user = tap(User::where('phone_number', $data['phone_number']))->update(['isVerified' => true]);
        /* Authenticate user */
        Auth::login($user->first_name());
        return redirect()->route('home')->with(['message' => 'Phone number verified']);
    }
    return back()->with(['phone_number' => $data['phone_number'], 'error' => 'Invalid verification code entered!']);
}
...
1 Answers

The same issue just popped up for me over the weekend on code that I haven't touched in a very long time. I believe they updated something recently and the docs may be outdated. The docs show sending a string, while if you look at the actual vendor file, it is looking for an array now.

Try this:

$verification = $twilio->verify->v2->services($twilio_verify_sid)
        ->verificationChecks
        ->create(['code' => $data['verification_code'], 'to' => $data['phone_number']]);
    
Related