DEV Community

Cover image for Laravel Mobile Verification
Fouladgar.dev
Fouladgar.dev

Posted on • Edited on

Laravel Mobile Verification

Many web applications require users to verify their mobile numbers before using the application. Rather than forcing you to re-implement this on each application, ‘Laravel Mobile Verification’ is a package that provides convenient methods and features for send, verify and resend verification codes.

Basic Setup

In the beginning, verify that your User model implements the MustVerifyMobile interface and use respected trait:

Next, you should specify your SMS service which any service (i.e. Nexmo, Twilio) are applicable. For sending SMS notifications via this package, you need to implement the SMSClient interface. This interface requires you to implement sendMessage method and this method will return your SMS service API result via a Payload object which contains user mobile number and token message:

In order to set your SMS Client, you should publish the mobile_verifier.php config file with:

php artisan vendor:publish --provider="Fouladgar\MobileVerification\ServiceProvider" --tag="config"

And set your client class:

Usage

Here is how you can send a verification token after user registration:

Verify

You should send token message of an authenticated user to this route /auth/mobile/verify:

curl -X POST \
      http://example.com/auth/mobile/verify \
      -H 'Accept: application/json' \
      -H 'Authorization: YOUR_JWT_TOKEN' \
      -F token=YOUR_VERIFICATION_TOKEN

Resend

If you need to resend a verification token message, you can use this route /auth/mobile/resend for an authenticated user:

curl -X POST \
      http://example.com/auth/mobile/resend \
      -H 'Accept: application/json' \
      -H 'Authorization: YOUR_JWT_TOKEN'

For more details, please check out the documentation:

GitHub logo mohammad-fouladgar / laravel-mobile-verification

This package provides convenient methods for sending and verifying mobile verification requests.

Laravel Mobile Verification

alt text

Build Status Coverage Status Quality Score Latest Stable Version Total Downloads License

Introduction

Many web applications require users to verify their mobile numbers before using the application. Rather than forcing you to re-implement this on each application, this package provides convenient methods for sending and verifying mobile verification requests.

Installation

You can install the package via composer:

composer require fouladgar/laravel-mobile-verification

Laravel 5.5 uses Package Auto-Discovery, so you are not required to add ServiceProvider manually.

Laravel <= 5.4.x

If you don't use Auto-Discovery, add the ServiceProvider to the providers array in config/app.php file

'providers' => [
  /*
   * Package Service Providers...
   */
  Fouladgar\MobileVerification\ServiceProvider::class,
],

Configuration

To get started, you should publish the config/mobile_verification.php config file with:

php artisan vendor:publish --provider="Fouladgar\MobileVerification\ServiceProvider" --tag="config"

If you’re using another table name for users table or different column name for mobile or even mobile_verification_tokens table, you can customize their values in config file:

// config/mobile_verification.php
<?php
return

Top comments (0)