Download the PHP package fouladgar/laravel-otp without Composer
On this page you can find all versions of the php package fouladgar/laravel-otp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fouladgar/laravel-otp
More information about fouladgar/laravel-otp
Files in fouladgar/laravel-otp
Package laravel-otp
Short Description This package provides convenient methods for sending and validating OTP notifications to users for authentication.
License MIT
Informations about the package laravel-otp
Laravel OTP(One-Time Password)
Introduction
Most web applications need an OTP(one-time password) or secure code to validate their users. This package allows you to send/resend and validate OTP for users authentication with user-friendly methods.
Version Compatibility
Laravel | Laravel-OTP |
---|---|
11.0.x | 4.2.x |
10.0.x | 4.0.x |
9.0.x | 3.0.x |
6.0.x to 8.0.x | 1.0.x |
Basic Usage:
Installation
You can install the package via composer:
Configuration
As next step, let's publish config file config/otp.php
by executing:
Token Storage
Package allows you to store the generated one-time password on either cache
or database
driver, default is cache
.
You can change the preferred driver through config file that we published earlier:
Cache
Note that Laravel OTP
package uses the already configured cache
driver to storage token, if you have not configured
one yet or have not planned to do it you can use database
instead.
Database
It means after migrating, a table will be created which your application needs to store verification tokens.
If you’re using another column name for
mobile
phone or evenotp_tokens
table, you can customize their values in config file:
Depending on the token_storage
config, the package will create a token table. Also, a mobile
column will be added to
your users
(default provider) table to show user verification state and store user's mobile phone.
All right! Now you should migrate the database:
Note: When you are using OTP to login user, consider all columns must be nullable except for the
mobile
column. Because, after verifying OTP, a user record will be created if the user does not exist.
User providers
You may wish to use the OTP for variant users. Laravel OTP allows you to define and manage many user providers that you
need. In order to set up, you should open config/otp.php
file and define your providers:
Note: You may also change the default repository and replace your own repository. however, every repository must implement
Fouladgar\OTP\Contracts\NotifiableRepositoryInterface
interface.
Model Preparation
Every model must implement Fouladgar\OTP\Contracts\OTPNotifiable
and also use
this Fouladgar\OTP\Concerns\HasOTPNotify
trait:
SMS Client
You can use any SMS services for sending OTP message(it depends on your choice).
For sending notifications via this package, first you need to implement the Fouladgar\OTP\Contracts\SMSClient
contract. This contract requires you to implement sendMessage
method.
This method will return your SMS service API results via a Fouladgar\OTP\Notifications\Messages\MessagePayload
object
which contains user mobile and token message:
In above example,
SMSService
can be replaced with your chosen SMS service along with its respective method.
Next, you should set the client wrapper SampleSMSClient
class in config file:
Practical Example
Here we have prepared a practical example. Suppose you are going to login/register a customer by sending an OTP:
Customization
Notification Default Channel Customization
For sending OTP notification there is a default channel. But this package allows you to use your own notification channel. In order to replace, you should specify channel class here:
Note: If you change the default sms channel, the
sms_client
will be an optional config. Otherwise, you must define your sms client.
Notification SMS and Email Customization
OTP notification prepares a default sms and email format that are satisfied for most application. However, you can customize how the mail/sms message is constructed.
To get started, pass a closure to the toSMSUsing/toMailUsing
method provided by
the Fouladgar\OTP\Notifications\OTPNotification
notification. The closure will receive the notifiable model instance
that is receiving the notification as well as the token
for validating. Typically, you should call the those methods
from the boot method of your application's App\Providers\AuthServiceProvider
class:
Translates
To publish translation file you may use this command:
you can customize in provided language file:
Testing
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
License
Laravel-OTP is released under the MIT License. See the bundled LICENSE file for details.
Built with :heart: for you.
All versions of laravel-otp with dependencies
illuminate/database Version ^10.0|^11.0
illuminate/support Version ^10.0|^11.0
illuminate/notifications Version ^10.0|^11.0