How to add odoo sms otp authentication in laravel 8 with example

Mobile OTP verification is a crucial feature that adds an extra layer of security to your website or app. This feature ensures that only authorized users can access your website or app, thus providing a more secure environment. In this article, we will guide you through the process of adding Odoo SMS OTP authentication in Laravel 8 with examples.

Odoo is an open-source enterprise resource planning (ERP) software that provides a range of business applications that form an integrated suite. It includes modules for accounting, inventory management, human resources, project management, and more. One of the modules offered by Odoo is the SMS notification module that enables users to send SMS notifications to their customers, vendors, and employees.

Laravel is a free, open-source PHP web framework that is widely used for building web applications. It offers a range of features, including MVC architecture, routing, middleware, and more. Laravel also provides an easy-to-use authentication system that can be used to implement mobile OTP verification.

To add Odoo SMS OTP authentication in Laravel 8, we will use the Sasta OTP API that provides a free mobile OTP verification service. Here are the steps you need to follow:

Step 1: Install Laravel 8

The first step is to install Laravel 8 on your system. You can do this by following the official Laravel documentation.

Step 2: Install the Sasta OTP package

Next, you need to install the Sasta OTP package using Composer. Run the following command in your terminal:

composer require sasta/otp

Also Read : how to implement mobile otp verification in laravel 8 step by step

Step 3: Configure the SMS provider

Now, you need to configure the SMS provider that you will use to send the OTP. In our case, we will use the Odoo SMS notification module. You need to create an account with the Odoo SMS provider and obtain an API key.

Once you have the API key, add the following code to your .env file:

ODOO_SMS_API_KEY=your_api_keyODOO_SMS_SENDER=your_sender_idODOO_SMS_ENDPOINT=https://api.odoo.com/sms/send

Step 4: Create a new controller

Next, create a new controller that will handle the OTP verification process. You can use the following command to create a new controller:

php artisan make:controller OTPController

Read Also : nexmo sms otp verification in laravel 8

Step 5: Add the OTP verification logic

In the OTPController, add the following code to handle the OTP verification process:

use Illuminate\Http\Request;use Illuminate\Support\Facades\Http;use Illuminate\Support\Facades\Session;class OTPController extends Controller{    public function sendOTP(Request $request)    {        $otp = mt_rand(1000, 9999);        $response = Http::post(config('app.sasta_otp_endpoint'), [            'api_key' => config('app.sasta_otp_api_key'),            'phone_number' => $request->input('phone_number'),            'otp' => $otp,        ]);        if ($response->json('success')) {            Session::put('phone_number', $request->input('phone_number'));            Session::put('otp', $otp);            return response()->json([                'success' => true,                'message' => 'OTP sent successfully.',            ]);        } else {            return response()->json([                'success' => false,                'message' => 'Failed to send OTP. Please try again.',            ]);        }    }    public function verifyOTP(Request $request)    {        $phone_number = Session::get('phone_number');        $otp = Session::get('otp');        if ($request->input('phone_number') == $phone_number && $request->input('otp') == $otp) {Session::forget('phone_number');Session::forget('otp');        return response()->json([            'success' => true,            'message' => 'OTP verified successfully.',        ]);    } else {        return response()->json([            'success' => false,            'message' => 'OTP verification failed. Please try again.',        ]);    }}}

This code sends an OTP to the phone number provided by the user and verifies the OTP entered by the user. It uses the Sasta OTP API to send the OTP and the Laravel session to store the OTP and phone number.

Also Read : how to add textlocal otp verification in laravel 8

Step 6: Create the routes

Now, create the routes to handle the OTP verification process. You can add the following code to your web.php file:

Route::post('/send-otp', [OTPController::class, 'sendOTP']);Route::post('/verify-otp', [OTPController::class, 'verifyOTP']);

These routes will handle the requests to send and verify the OTP.

Step 7: Add the front-end code

Finally, add the front-end code to your website or app. You can use the following HTML and JavaScript code to display the OTP verification form:

<form id="otp-form">  <div class="form-group">    <label for="phone-number">Phone Number</label>    <input type="text" class="form-control" id="phone-number" name="phone_number" required>  </div>  <div class="form-group">    <label for="otp">OTP</label>    <input type="text" class="form-control" id="otp" name="otp" required>  </div>  <button type="submit" class="btn btn-primary">Verify OTP</button></form><script>$(document).ready(function() {  $('#otp-form').on('submit', function(event) {    event.preventDefault();    $.ajax({      url: '/verify-otp',      method: 'POST',      data: $(this).serialize(),      dataType: 'json',      success: function(response) {        if (response.success) {          alert(response.message);        } else {          alert(response.message);        }      }    });  });  $('#phone-number').on('blur', function() {    $.ajax({      url: '/send-otp',      method: 'POST',      data: $(this).serialize(),      dataType: 'json',      success: function(response) {        if (response.success) {          alert(response.message);        } else {          alert(response.message);        }      }    });  });});</script>

This code uses jQuery to handle the form submission and sends the OTP to the user’s phone number when the phone number field loses focus.

Conclusion

In this article, we have explained how to add Odoo SMS OTP authentication in Laravel 8 with examples. We have used the Sasta OTP API to send and verify the OTP and the Odoo SMS notification module to send the SMS messages. By following these steps, you can add mobile OTP verification to your website or app, providing a more secure environment for your users.

Comments