Skip to content
This repository has been archived by the owner on Jul 7, 2021. It is now read-only.

Laravel Two-Factor Authenticated For Laravel/Ui

License

Notifications You must be signed in to change notification settings

masoudghadimi/twoFactorAuth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

twoFactorAuth

Laravel Two-Factor Authenticated For Laravel/Ui

This package is being developed and debugged

Install

  1. Install package - using composer

composer require masoudghadimi/two-factor-auth

  1. Publish configuration file

php artisan vendor:publish --tag=twoFactor

  1. Migration

php artisan migrate

  1. Update User model (app/Models/User)
protected $fillable = [
     'name',
     'email',
     'password',
     'phone_number',
     'two_factor_type'
];
  1. Add this codes to the User model (app/Models/User)
public function verifyCodes()
{
    return $this->hasMany(VerifyCode::class);
}
  1. Add this codes to the LoginController (app/Http/Auth/LoginController)
use TwoFactorAuthenticate;

protected function authenticated(Request $request, $user)
{
     return $this->loggedIn($request , $user);
}

Configuration

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

Next step

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,

Last step

Go to the link below :

localhost:8000/home/security

Good luck