Laravel Two-Factor Authenticated For Laravel/Ui
- Install package - using composer
composer require masoudghadimi/two-factor-auth
- Publish configuration file
php artisan vendor:publish --tag=twoFactor
- Migration
php artisan migrate
- Update User model (app/Models/User)
protected $fillable = [
'name',
'email',
'password',
'phone_number',
'two_factor_type'
];
- Add this codes to the User model (app/Models/User)
public function verifyCodes()
{
return $this->hasMany(VerifyCode::class);
}
- Add this codes to the LoginController (app/Http/Auth/LoginController)
use TwoFactorAuthenticate;
protected function authenticated(Request $request, $user)
{
return $this->loggedIn($request , $user);
}
Open configuration file - config/twoFactor.php
- Detect the channel
- change the route prefix
- Change the expiration time
- Change the number of digits in the code
Create channel file and then put it in the config file (notificationsChannels)
For example :
create SmsVerifyCodeChannel.php in app/channels then enter your desired code as below
class SmsVerifyCodeChannel
{
public function send($notifiable, Notification $notification)
{
if (! method_exists($notification , 'toSendVerifyCode')) {
throw new \Exception('toSendVerifyCode not found');
}
$data = $notification->toSendVerifyCode($notifiable);
$message = $data['message'];
$phone = $data['number'];
try{
$lineNumber = 1111111;
$api = new \Ghasedak\GhasedakApi('token');
$api->SendSimple($phone, $message, $lineNumber);
}
catch(ApiException $e){
echo $e->errorMessage();
}
catch(HttpException $e){
echo $e->errorMessage();
}
}
}
Next, enter the config/twofactor.php file and in the notificationsChannels section, enter the created channel as follows:
'notificationsChannels' => \App\channels\SmsVerifyCodeChannel::class,
Go to the link below :
localhost:8000/home/security