Parul
Parul

Reputation: 41

Otp Verification in laravel

I'm trying to get Otp verification system in my laravel app but It shows me page expired when i enter my number to get the otp.(Have provided Code and Screenshot "Page Expired")

Using MSG91

code...

 <div class="container">
        <div class="jumbotron">
            <h1>OTP</h1>
            <div class="card">
                <div class="card-body">
                    <div class="col-md-6">

                        <form action="{{url('/')}}/dashboard" method="POST">
                        <div class="form-group">
                            <label for="exampleInputPassword1">Mobile Number</label>
                            <input type="number" class="form-control" id="number" name="number" placeholder="Enter Mobile Number">
                        </div>
                        <button type="submit" name="sendotp" class="btn btn-success">Send OTP</button>

                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php


        $authKey = "auth...Key...here";
        $senderId = "Id here";

        if (isset($_POST['sendotp'])){

            $mobileNumber = $_POST['number'];

            $message = 'Your Verification Code id ######';

            $postData = array(
              'authKey' => $authKey,
              'mobiles' => $mobileNumber,
              'message' => $message,
              'sender' => $senderId,
            );

            $url ="http://control.msg91.com/api/sendotp.php";

            $curl = curl_init($url);

            curl_setopt_array($curl, array(
                CURLOPT_URL => "https://api.msg91.com/api/v5/otp?invisible=1&otp=OTP%20to%20send%20and%20verify.%20If%20not%20sent%2C%20OTP%20will%20be%20generated.&userip=IPV4%20User%20IP&authkey=Authentication%20Key&email=Email%20ID&mobile=Mobile%20Number&template_id=Template%20ID&otp_length=&otp_expiry=",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => $postData,
                CURLOPT_SSL_VERIFYHOST => 0,
                CURLOPT_SSL_VERIFYPEER => 0,
                CURLOPT_HTTPHEADER => array(
                    "content-type: application/json"
                ),
            ));

            $response = curl_exec($curl);
            $err = curl_error($curl);

            curl_close($curl);

            if ($err) {
                echo "cURL Error #:" . $err;
            } else {
                echo $response;
            }

        }

?>

error.. enter image description here

Please let me know where is my error.... and also let me know which is the best way to add OTP verification in laravel using MSG91

Thank You

Upvotes: 1

Views: 6640

Answers (1)

Akash Kumar Verma
Akash Kumar Verma

Reputation: 3318

you need to pass csrf token with form.

<form action="{{url('/dashboard')}}" method="POST">
     {{ csrf_field() }}
     <div class="form-group">
          <label for="exampleInputPassword1">Mobile Number</label>
          <input type="number" class="form-control" id="number" name="number" placeholder="Enter Mobile Number">
     </div>
     <button type="submit" name="sendotp" class="btn btn-success">Send OTP</button>
</form>
{{ csrf_field() }} will generate a hidden field with name _token.
<input type="hidden" name="_token" value="token value">

if you are using laravel > 5.6 you can also use @csrf

both {{ csrf_field() }} and @csrf gives you same hidden field.

Upvotes: 4

Related Questions