PHP code example of alhaji-aki / laravel-otp-token
1. Go to this page and download the library: Download alhaji-aki/laravel-otp-token library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
alhaji-aki / laravel-otp-token example snippets
return [
/*
|--------------------------------------------------------------------------
| Otp Token Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as del in the application and you want to have
| separate otp token settings based on the specific user types.
|
| The expire time is the number of seconds that the token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'otp_tokens' => [
'users' => [
'provider' => 'users',
'table' => 'otp_tokens',
'expire' => 60,
'throttle' => 60,
],
],
];
return [
/*
|--------------------------------------------------------------------------
| Password Reset Language Lines
|--------------------------------------------------------------------------
|
| The following language lines are the default lines which match reasons
| that are given by the password broker for a password update attempt
| has failed, such as for an invalid token or invalid new password.
|
*/
'completed' => 'We have completed the verification process.',
'sent' => 'We have sent your otp token.',
'throttled' => 'Please wait before retrying.',
'token' => 'This otp token is invalid.',
'user' => "We can't find the user account.",
];
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use AlhajiAki\OtpToken\Contracts\CanSendOtpToken;
use AlhajiAki\OtpToken\Traits\CanSendOtpToken as CanSendOtpTokenTrait;
class YourModel extends Model implements CanSendOtpToken
{
use CanSendOtpTokenTrait;
}
namespace App\Http\Controllers;
use AlhajiAki\OtpToken\OtpToken;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class MobileNumberVerificationController extends Controller
{
public function store(Request $request)
{
$response = OtpToken->broker()->sendOtpToken([
'mobile' => '+233248000000',
'action' => 'verify-user',
'field' => 'mobile'
], function ($user, $token) {
$user->notify(new VerifyMobile($token));
});
return response()->json([
'message' => trans($response)
], $response == OtpToken::OTP_TOKEN_SENT ? Response::HTTP_OK : Response::HTTP_UNAUTHORIZED);
}
}
namespace App\Http\Controllers;
use AlhajiAki\OtpToken\OtpToken;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Validation\ValidationException;
class MobileNumberVerificationController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function verify(Request $request)
{
$response = OtpToken::performAction([
'token' => $request->token,
'mobile' => $request->user()->mobile,
'action' => 'verify-user',
'field' => 'mobile'
],
function ($user) {
$user->update([
'mobile_verified_at' => now()
]);
}
);
if ($response !== OtpToken::ACTION_COMPLETED) {
throw ValidationException::withMessages([
'token' => trans($response)
]);
}
return response()->json([
'message' => trans($response)
], Response::HTTP_OK);
}
}