Download the PHP package alhaji-aki/laravel-otp-token without Composer
On this page you can find all versions of the php package alhaji-aki/laravel-otp-token. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alhaji-aki/laravel-otp-token
More information about alhaji-aki/laravel-otp-token
Files in alhaji-aki/laravel-otp-token
Package laravel-otp-token
Short Description Create otp tokens for users on your application
License MIT
Homepage https://github.com/alhaji-aki/laravel-otp-token
Informations about the package laravel-otp-token
Laravel Otp Token
This is a simple package to help generate otp tokens for users. This package follows the same approach as laravel's password reset functionality.
Installation
You can install the package via composer by running:
After the installation has completed, the package will automatically register itself. Run the following to publish the migration, config and lang file
After publishing the migration you can create the otp_tokens table by running the migrations:
The contents of the config file looks like:
With this configuration you can have multiple otp token providers just like laravel's password and auth providers.
The lang file that is published looks like:
Usage
The model that u want to generate otp token for should implement the following interface and trait
The getColumnForOtpToken($field)
is used the get the value of the field that will be associated to an otp token. This comes in handy if for example you want to be able to generate different otp tokens for say a user's email and mobile phone number during verification.
Generating an otp token
The OtpToken facade exposes a sendOtpToken()
method which expects two parameters. They are:
- an array containing the field to use, the action to associate with the token and the value for the field specified.
- the second parameter is a closure that would be executed after the otp token has been generated. The closure will get an instance of the
CanSendOtpToken
contract and the token that was generated.
The method returns a response which can be one of the following constants :
- OTP_TOKEN_SENT: When the closure passed executes successfully.
- INVALID_USER: When the user cannot be found.
- OTP_TOKEN_THROTTLED: the user has to wait for some minutes before they can create a new token.
A common usage example of the sendOtpToken()
is when you want to send an otp verification token to a user's phone number. Here is an example implementation:
The facade also has a performAction()
method to perform an action when the provided otp token is valid. This method also expects two parameters. They are:
- an array containing the field to use, this should be the same as the field passed when the token was generated, the action that was associated to the token when it was generated and the value for the field specified.
- the second parameter is a closure that would be executed after the otp token has been verified. The closure will get an instance of the
CanSendOtpToken
contract.
The method returns a response which can be one of the following constants :
- ACTION_COMPLETED: When the closure passed executes successfully.
- INVALID_USER: When the user cannot be found.
- INVALID_TOKEN: When the token submitted is an invalid one.
A common usage example of the performAction()
is when you want to verify that the otp token is a corrent one. Here is an example implementation:
Testing
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-otp-token with dependencies
illuminate/contracts Version ^10.0|^11.0
illuminate/database Version ^10.0|^11.0
illuminate/support Version ^10.0|^11.0