this package help you to implement otp grant (register - login with verify code or two verification code ) via laravel-passport
Via Composer
$ composer require amin3536/passport-otp-grant
1-install and initial laravel passport in your project and create a password-client
2- add below two rows to your user migration ( if you want use custom rows see customising section)
//...
$table->string('phone_number')->unique();
$table->integer('otp')->nullable();
//...
add use HasOTP;
in your model :
<?php
class User extends Authenticatable
{
use HasFactory, Notifiable, HasApiTokens, HasOTP;
//...
}
below sample and logic is about login and register with otp
. (it's not about two verification )
public function userLoginOrRegister(UserLoginRequest $request)
{
$user = $this->userModel->wherePhoneNumber($request['phone_number'])->first();
if (!$user) {
$user = $this->userModel->create(['phone_number' => $request['phone_number']]);
}
$user->otp = $code_verifier = rand(10000, 99999);
//you cand send otp code via sms , email , any messanger , .....
$user->save();
this->sendOtpCodeToUser(user);
}
now you can verify user with passport like below
Request::create('/oauth/token',
'POST',
[
'grant_type' => 'otp_grant',
'client_id' => 'client_id',
'client_secret' => client_secret',
'phone_number' => 'phone_number',
'otp' => 'otp',
'scope' =>'',
]);
<?php
class User extends Authenticatable
{
use HasFactory, Notifiable, HasApiTokens, HasOTP;
$phoneNumberColumn='anything';
$OTPColumn='my_otp';
//otp expire time in minute
$OTPExpireTime=15;
//...
}
Please see the changelog for more information on what has changed recently.
$ composer test
- change phone_number to user
- add test
- add CI
Please see contributing.md for details and a todolist.
If you discover any security related issues, please email author email instead of using the issue tracker.
license. Please see the license file for more information.