A package for verifying a user via call or SMS

Overview

Verify your users by call or SMS

PHPStan run-tests

It's a common practice: a user signs up, you send an SMS to their phone with a code, they enter that code in your application and they're off to the races.

This package makes it simple to add this capability to your Laravel application.

Installation

You can install the package via composer:

composer require worksome/verify-by-phone

You can publish the config file by running:

php artisan vendor:publish --tag="verify-by-phone-config"

Configuration

This package is built to support multiple verification services. The primary service is Twilio. You may configure the service in the config file at config/verify-by-phone.php under driver, or by using the dedicated .env variable: VERIFY_BY_PHONE_DRIVER.

twilio

To use our Twilio integration, you'll need to provide an account_sid, auth_token and verify_sid. All of these can be set in the config/verify-by-phone.php file under services.twilio.

Usage

To use this package, you'll want to inject the \Worksome\VerifyByPhone\Contracts\PhoneVerificationService contract into your application. Let's imagine that you want to send the verification code in a controller method:

public function sendVerificationCode(Request $request, PhoneVerificationService $verificationService)
{
    // Send a verification code to the given number
    $verificationService->send(new PhoneNumber($request->input('phone')));
    
    return redirect(route('home'))->with('message', 'Verification code sent!');
}

It's as simple as that! Note that we are using \Propaganistas\LaravelPhone\PhoneNumber to safely parse numbers in different formats.

Now, when a user receives their verification code, you'll want to check that it is valid. Use the verify method for this:

public function verifyCode(Request $request, PhoneVerificationService $verificationService)
{
    // Verify the verification code for the given phone number
    $valid = $verificationService->verify(
        new PhoneNumber($request->input('phone')), 
        $request->input('code')
    );
    
    if ($valid) {
        // Mark your user as valid
    }
}

The first parameter is the phone number (again using \Propaganistas\LaravelPhone\PhoneNumber), and the second is the verification code provided by the user.

Validation rule

We offer a rule to make it easy to verify a verification code during validation.

Be aware that this rule will call the verify method of the PhoneVerificationService contract, and likely will make an HTTP request.

Validator::validate($request->all(), [
    'phone_number' => ['required'],
    'verification_code' => ['required', Rule::verificationCodeIsValid('phone_number')],
]);

If your data doesn't include the phone number, you may instead pass it in manually:

Validator::validate($request->all(), [
    'verification_code' => ['required', Rule::verificationCodeIsValid('+441174960123')],
]);

We extend the Rule class for visual consistency with other rules, but you can also use the VerificationCodeIsValid rule directly for better IDE support:

Validator::validate($request->all(), [
    'verification_code' => ['required', new VerificationCodeIsValid('+441174960123')],
]);

This rule will also handle the case where the verification code has expired and return a suitable error message.

Artisan commands

This package ships with a couple of artisan commands that allow you to send and verify verification codes.

# Send a verication code to the given phone number
php artisan verify-by-phone:send "+441174960123"

# Check that a given verication code is valid for the given phone number
php artisan verify-by-phone:verify "+441174960123" 1234

The verify command will return a console failure if verification fails.

Testing

When writing tests, you likely do not want to make real requests to services such as Twilio. To support testing, we provide a FakeVerificationService that can be used to mock the verification service. To use it, you should set an env variable in your phpunit.xml with the following value:

<env name='VERIFY_BY_PHONE_DRIVER' value='null'/>

Alternatively, you may manually swap out the integration in your test via using the swap method. The fake implementation has some useful testing methods you can use:

it('tests something to do with SMS verification', function () {
    $service = new FakeVerificationService();
    $this->swap(PhoneVerificationService::class, $service);
   
    // Customise what happens when calling `send`
    $service->sendUsing(fn () => throw new Exception('Something went wrong'));
    
    // Customise what happens when calling `verify`
    $service->verifyUsing(fn () => throw new Exception('Something went wrong'));
    
    // Throw a VerificationCodeExpiredException
    $service->actAsThoughTheVerificationCodeExpired();
    
    // Assert that a verification was "sent" on the given number
    $service->assertSentVerification(new PhoneNumber('+441174960123'));
});

You may execute this project's tests by running:

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

Comments
Releases(v0.3.0)
Owner
Worksome
Building a future platform for flexible employment.
Worksome
Implementations for different SMS providers for EspoCRM. Can be used for 2-factor authentication or automatic SMS sending via Workflow and BPM tools.

SMS Providers for EspoCRM An installable extension. Supported SMS Providers Twilio Spryng sms77 Setting up Install the extension. At Administration >

EspoCRM - Open Source CRM 11 Nov 30, 2022
@Authy notification channel for @Laravel, with the ability to send in-app, sms, and call verification tokens.

Authy Notification Channel for Laravel Authy notification channel for Laravel, with the ability to send in-app, sms, and call verification tokens. Tab

Laravel Notification Channels 57 Dec 19, 2022
Sends notifications via one or more channels (email, SMS, ...).

Notifier Component The Notifier component sends notifications via one or more channels (email, SMS, ...). Resources Documentation Contributing Report

Symfony 610 Jan 3, 2023
Fake SMS (as email during development) Notifier Bridge

Fake SMS Notifier Provides Fake SMS (as email during development) integration for Symfony Notifier. DSN example FAKE_SMS_DSN=fakesms+email://default?t

Symfony 17 May 23, 2022
Provides Turbo SMS integration for Symfony Notifier.

TurboSms Notifier Provides TurboSms integration for Symfony Notifier. DSN example TURBOSMS_DSN=turbosms://AUTH_TOKEN@default?from=FROM where: AUTH_TO

Symfony 5 May 23, 2022
Laravel SMS Notification Channel

Laravel SMS Notification Channel Installation composer require guangda/laravel-sms-notification-channel env 配置 SMS_PROVIDER=yunpian SMS_SIGNATURE=【签名

Guangda 6 Dec 29, 2021
all geteways (SMS&Payments geteways) By (Mahmoud Ibrahim)

All gateways payments & SMS package name: gateways package version: v1.1 author : mahmoud ibrahim description :provide gateways payments and SMS to in

php anonymous 24 Dec 26, 2022
SMS service provider for Laravel

laravel-sms SMS service provider for Laravel and Lumen. Uses SMS Client to enable sending SMS messages using the following drivers: nexmo clockwork te

Matthew Daly 34 Nov 29, 2022
Send SMS with easy using BEEM

beem-sms-api Send SMS with easy using BEEM Installation You must be using composer to be able to use this library. If composer 1.x is installed, make

Hosanna Higher Technologies 4 Dec 24, 2021
laravel send sms. kavenegar, ghasedak

Requirements laravel >= 7 Installation composer require ehsanmoradi/laravel-sms Publish the configuration file (this will create a laravel-sms.php

ehsan moradi 3 Feb 6, 2022
An SMS notification channel for the PHP framework Laravel.

Laravel SMS notification channel An SMS notification channel for the PHP framework Laravel. Supported SMS gateways: 46elks Cellsynt Telenor SMS Pro Tw

Andreas 2 Jan 22, 2022
Larafirebase is a package thats offers you to send push notifications or custom messages via Firebase in Laravel.

Introduction Larafirebase is a package thats offers you to send push notifications or custom messages via Firebase in Laravel. Firebase Cloud Messagin

Kutia Software Company 264 Jan 7, 2023
Livewire Package to display Toast Notification based on TALL Stack.

livewire-toast Livewire Package to display Toast Notification based on TALL Stack. Requirements Make sure that Livewire is installed properly on your

AscSoftwares 35 Nov 12, 2022
Laravel package to launch toast notifications.

Laravel package to launch toast notifications. This package provides assistance when using toast notifications. Using the iziTOAST package, which allo

Anthony Medina 7 Nov 25, 2022
This package allows you to send notifications to Microsoft Teams.

Teams connector This package allows you to send notifications to Microsoft Teams. Installation You can install the package using the Composer package

skrepr 20 Oct 4, 2022
Our Laravel Sendgrid Notification package

laravel-sendgrid-notification-channel Laravel Sendgrid Notification channel Installation To get started, you need to require this package: composer re

Konstruktiv B.V. 4 Feb 3, 2022
Alerts is a package that handles global site messages.

Alerts for Laravel 5, 6, 7 and 8 Global site messages in Laravel 8, 7, 6 and 5. Helps trigger notification bubbles with a simple API, both in the curr

Prologue PHP 341 Dec 30, 2022
A package to simplify automating future notifications and reminders in Laravel

Laravel Snooze Schedule future notifications and reminders in Laravel Why use this package? Ever wanted to schedule a future notification to go out at

Thomas Kane 720 Jan 7, 2023
Lara-Izitoast : Laravel Notification Package

Lara-Izitoast : Laravel Notification Package This is a laravel notification wrapper build with http://izitoast.marcelodolce.com javascript library. Ho

Apps:Lab KE 34 Nov 19, 2022