1. Go to this page and download the library: Download walkweltech/laravel-otp 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/ */
walkweltech / laravel-otp example snippets
Route::get('secret', function (\Illuminate\Http\Request $request): string {
$token = $request->otpToken();
$messages[] = "The otp token {$token} has {$token->timeLeft()} out of {$token->expiryTime()} seconds.";
$token->refresh();
$messages[] = "The time left can be reset using the refresh method: {$token->timeLeft()}/{$token->expiryTime()}";
$token->extend(30);
$messages[] = "The expiry time can be increased using the extend method: {$token->timeLeft()}/{$token->expiryTime()}";
$messages[] = "You can also invalidate the token immediately. Try refreshing the page ;)";
$request->otpToken()->invalidate();
return implode('<br>', $messages);
})->middleware('auth', 'otp');
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
// [...]
'otp' => \WalkwelTech\Otp\Http\Middleware\Otp::class,
];
Route::get('secret', function (Request $request): string {
$request->otpToken()->refresh();
return 'The secret of immortality';
})->middleware('auth', 'otp');
// AppServiceProvider::register():
TokenNotification::macro('AcmeSms', function () {
// $this is TokenNotification class.
return $this->notification->code;
});
namespace App\Acme\PasswordGenerators;
use WalkwelTech\Otp\PasswordGeneratorInterface;
class AcmePasswordGenerator implements PasswordGeneratorInterface
{
/**
* Generate an acme password with the given length.
*
* @param int $length
* @return string
*/
public function generate(int $length): string
{
return 'your implementation';
}
}
$token = Otp::create(auth()->user(), $length = 6);
// See what can be done with tokens below.
$token = Otp::retrieveByPlainText(auth()->id(), $otpPassword);
// See what can be done with tokens below.
$token = Otp::retrieveByCipherText(auth()->id(), $otpPassword);
// See what can be done with tokens below.
public function authenticableId();
public function cipherText(): string;
public function plainText(): ?string; // If you have just created the token, plain text will be accessable. If you retrieved it; it won't.
public function createdAt(): Carbon;
public function updatedAt(): Carbon;
public function expiryTime(): int;
public function expiresAt(): Carbon;
public function timeLeft(): int;
public function expired(): bool;
public function revoke(): void;
public function invalidate(): void;
public function show(Request $request, $id) {
if($request->input('revoke_session', false)) {
$request->otpToken()->revoke();
}
return view('heaven');
}
// Extend the usage time of the token for the given seconds:
public function extend(?int $seconds = null): bool;
// Make the token function like it has just been created:
public function refresh(): bool;