Skip to content

hoangphidev/google-2fa

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Google Authenticator Laravel Package

Latest Version on Packagist Total Downloads

Install

composer require hoangphi/google-2fa

Config

Add column secret_code

php artisan google-2fa:make <table_name>

Example

Add column secret_code into table users

php artisan google-2fa:make users

We have file database\migrations\{time_stamp}_add_secret_code_to_users.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddSecretCodeToUsers extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('secret_code')->nullable()->after('id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('secret_code');
        });
    }
}

Run migrate command

php artisan migrate

You must migrate table users before.

Usage

See following example:

use HoangPhi\GoogleAuthenticator\GoogleAuthenticator;

$googleAuthenticator = new GoogleAuthenticator();
$secret = $googleAuthenticator->createSecret();
echo "Secret is: " . $secret . "\n\n";

$qrCodeUrl = $googleAuthenticator->getQRCodeGoogleUrl('Blog', $secret);
echo "Google Charts URL for the QR-Code: ".$qrCodeUrl."\n\n";

$oneCode = $googleAuthenticator->getCode($secret);
echo "Checking Code '$oneCode' and Secret '$secret':\n";

$checkResult = $googleAuthenticator->verifyCode($secret, $oneCode, 2);    // 2 = 2*30sec clock tolerance
if ($checkResult) {
    echo 'OK';
} else {
    echo 'FAILED';
}

Changelog

Please see CHANGELOG for more information what has changed recently.

Security

If you discover any security-related issues, please email hoangphidev@gmail.com instead of using the issue tracker.

Credits

References

License

The Laravel framework is open-sourced software licensed under the MIT license.