1. Go to this page and download the library: Download benbjurstrom/otpz 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/ */
benbjurstrom / otpz example snippets
// app/Models/User.php
namespace App\Models;
//...
use BenBjurstrom\Otpz\Models\Concerns\HasOtps;
use BenBjurstrom\Otpz\Models\Concerns\Otpable;
class User extends Authenticatable implements Otpable
{
use HasFactory, Notifiable, HasOtps;
// ...
}
// routes/auth.php
use BenBjurstrom\Otpz\Http\Controllers\GetOtpController;
use BenBjurstrom\Otpz\Http\Controllers\PostOtpController;
//...
Route::get('otpz/{id}', GetOtpController::class)
->name('otpz.show')->middleware('guest');
Route::post('otpz/{id}', PostOtpController::class)
->name('otpz.post')->middleware('guest');
return [
/*
|--------------------------------------------------------------------------
| Expiration and Throttling
|--------------------------------------------------------------------------
|
| These settings control the security aspects of the generated codes,
| including their expiration time and the throttling mechanism to prevent
| abuse.
|
*/
'expiration' => 5, // Minutes
'limits' => [
['limit' => 1, 'minutes' => 1],
['limit' => 3, 'minutes' => 5],
['limit' => 5, 'minutes' => 30],
],
/*
|--------------------------------------------------------------------------
| Model Configuration
|--------------------------------------------------------------------------
|
| This setting determines the model used by Otpz to store and retrieve
| one-time passwords. By default, it uses the 'App\Models\User' model.
|
*/
'models' => [
'authenticatable' => App\Models\User::class,
],
/*
|--------------------------------------------------------------------------
| Mailable Configuration
|--------------------------------------------------------------------------
|
| This setting determines the Mailable class used by Otpz to send emails.
| Change this to your own Mailable class if you want to customize the email
| sending behavior.
|
*/
'mailable' => BenBjurstrom\Otpz\Mail\OtpzMail::class,
/*
|--------------------------------------------------------------------------
| Template Configuration
|--------------------------------------------------------------------------
|
| This setting determines the email template used by Otpz to send emails.
| Switch to 'otpz::mail.notification' if you prefer to use the default
| Laravel notification template.
|
*/
'template' => 'otpz::mail.otpz',
// 'template' => 'otpz::mail.notification',
/*
|--------------------------------------------------------------------------
| User Resolver
|--------------------------------------------------------------------------
|
| Defines the class responsible for finding or creating users by email address.
| The default implementation will create a new user when an email doesn't exist.
| Replace with your own implementation for custom user resolution logic.
|
*/
'user_resolver' => BenBjurstrom\Otpz\Actions\GetUserFromEmail::class,
];
// app/Livewire/Forms/LoginForm.php
use BenBjurstrom\Otpz\Actions\SendOtp;
use BenBjurstrom\Otpz\Exceptions\OtpThrottleException;
use BenBjurstrom\Otpz\Models\Otp;
//...
#[Validate('ed();
RateLimiter::hit($this->throttleKey(), 300);
try {
$otp = (new SendOtp)->handle($this->email, $this->remember);
} catch (OtpThrottleException $e) {
throw ValidationException::withMessages([
'form.email' => $e->getMessage(),
]);
}
RateLimiter::clear($this->throttleKey());
return $otp;
}
public function login(): void
{
$this->validate();
$otp = $this->form->sendEmail();
$this->redirect($otp->url);
}
// app/Http/Requests/Auth/LoginRequest.php
use BenBjurstrom\Otpz\Actions\SendOtp;
use BenBjurstrom\Otpz\Exceptions\OtpThrottleException;
use BenBjurstrom\Otpz\Models\Otp;
//...
public function rules(): array
{
return [
'email' => ['Exception $e) {
throw ValidationException::withMessages([
'email' => $e->getMessage(),
]);
}
RateLimiter::clear($this->throttleKey());
return $otp;
}
public function store(LoginRequest $request): \Symfony\Component\HttpFoundation\Response
{
$otp = $request->sendEmail();
return Inertia::location($otp->url);
}