Download the PHP package albertojm8/laravel-sms-verification without Composer
On this page you can find all versions of the php package albertojm8/laravel-sms-verification. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download albertojm8/laravel-sms-verification
More information about albertojm8/laravel-sms-verification
Files in albertojm8/laravel-sms-verification
Package laravel-sms-verification
Short Description Library for sending out and verifying SMS codes using AWS SNS
License MIT
Informations about the package laravel-sms-verification
laravel-sms-verification
Library for sending out and verifying SMS codes using AWS SNS
The package has a trait and a custom validation rule that you can use on any model for verifying with an SMS code.
Table of contents
- Installation
- Example
- Sending out the SMS code
- Verifying the SMS code
- Setting SMS verification attempt limits
- Adding custom Validation for verifying an SMS code
- Adding custom Validation for verifying an SMS code using route model binding
- Changing the SMS message being sent
- Changing the SMS sender
Installation
Pull the lib with composer:
Add the service provider in
You can add SMS Verification to any model, and it will create:
- a function setSMSVerificationNumber($mobile) - sets the mobile number and sends out the SMS message containg the verification code
- a function verifySMSCode($code) -verifies the SMS code
- and a bunch of private methods that are used for verifying the SMS code. You can check them out in the Trait itself for more details.
Example
Add the trait to your User model (or any other model on which you might want to enable 2FA):
For MySQL databases you will need to add these attributes to your Model's migration:
That's it! :) Now you can send out SMS codes and verify them later.
Sending out the SMS code
Send an SMS by setting the SMS verification number attribute:
This will send an sms message to the given . If the message fails to send an Expception will pop up. If SMS sending succeeds, the , and attributes will be set on the model.
After this, you can verify the code at any time.
Verifying the SMS code
For simple verification you can use:
This will set the attribute to true. If attempt limiting is enabled, this will increment the attempts that the User had. Once this limit is exceeded a TooManySMSVerificationAttempts Exception will be thrown.
Setting SMS verification attempt limits
You can adjust the number of attempts a User can have at verifying the code by overwriting the on the model implementing the trait:
This value defaults to 5.
If you don't want to limit your users with attempts, set this variable to 0.
Adding custom Validation for verifying an SMS code
You can easily verify the code on any custom request by adding the validation:
The validator will try to validate the code using the logged in user (Auth::user()).
Adding custom Validation for verifying an SMS code using route model binding
Or you can leverage route model binding and create a validation in your custom Request model:
In this case the validator expects to find an object in the route. The object needs to implement the above mentioned Trait in order for the validation to work.
Changing the SMS message being sent
You can set the text that is sent via SMS by overriding the traits default getSMSVerificationMessage($code) function in your Model implementation:
Changing the SMS sender
You can set the sender by overriding the traits default getSMSVerificationMessage($code) function in your Model implementation: