Credentials are required to create a Client twilio

Published

I am trying to register a new on my laravel and i am getting this error from twilio when I tried to create a new account , sometimes the registration might work, and sometimes I will get this error below how can I solve this easily

TwilioExceptionsConfigurationException Credentials are required to create a Client


<?php

namespace AppHttpControllersAuth;

use AppHttpControllersController;
use AppModelsUser;
use AppProvidersRouteServiceProvider;
use IlluminateAuthEventsRegistered;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacadesHash;
use IlluminateValidationRules;
use TwilioRestClient;

class RegisteredUserController extends Controller
{
    /**
     * Display the registration view.
     *
     * @return IlluminateViewView
     */
    public function create()
    {
        return view('auth.register');
    }

    /**
     * Handle an incoming registration request.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpRedirectResponse
     *
     * @throws IlluminateValidationValidationException
     */
    public function store(Request $request)
    {
    $random =  str_pad(mt_rand(1,9999),4,'0',STR_PAD_LEFT);
       $data = $request->validate([
            'username' => ['required', 'string', 'max:255', 'unique:users'],
            'phone_number' => ['required', 'numeric', 'unique:users'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'confirmed', RulesPassword::defaults()],
        ]);
        /* 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);
        $twilio->verify->v2->services($twilio_verify_sid)
            ->verifications
            ->create($data['phone_number'], "sms");
        $user = User::create([
            'username' => $request->username,
            'tribe_pin' => $random,
            'phone_number' => $request->phone_number,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);
        return redirect()->route('verify')->with(['phone_number' => $data['phone_number']]);
        // event(new Registered($user));

        // Auth::login($user);

        // return redirect(RouteServiceProvider::HOME);
    }

    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());
            return redirect('/')->with(['message' => 'Phone number verified']);
        }
        return back()->with(['phone_number' => $data['phone_number'], 'error' => 'Invalid verification code entered!']);
    }
}

env file

TWILIO_USERNAME=null
TWILIO_PASSWORD=null
TWILIO_SID=ACb739b8060f2237eaf2a..........
TWILIO_AUTH_TOKEN=e3104ba5d1f4b858...........
TWILIO_VERIFY_SID=VA41b1f11b1dd945b241ba....

Source: Laravel

Published
Categorised as laravel, twilio Tagged ,

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq