teh9/laravel2fa

A simple two-factor authentication via telegram implementation for Laravel

v1.1.1 2023-05-08 19:14 UTC

This package is auto-updated.

Last update: 2024-06-08 21:50:35 UTC


README

A simple two-factor implementation using Telegram for Laravel.

Packagist Downloads Packagist Version Packagist License

Installation

You can install the package via composer:

composer require teh9/laravel2fa

Publish the package config, migrations & localizations files:

php artisan vendor:publish --provider="Teh9\Laravel2fa\TelegramTwoFactorServiceProvider"

Usage

Set up

In your project folder config/laravel2fa.php, provide bot api key it can be received by official telegram bot @BotFather

'api_key' => 'YOUR_BOT_API_KEY'

Migrations

After publishing, you will have migration, execute:

php artisan migrate

Will be added 2 columns for users table, if you want change table you can do it in migration file: database/migrations/add_two_factor_columns_to_model_table.php

chat_id - big integer|nullable|deafult-null
secret  - string     |nullable|default-null

Languages

2 languages are available in files /resources/lang/[lang]/2fa.php:

  • en;
  • ru;

You can add any else but watch on existed implementations on how to make it correctly

Prepare Users Model:

class User extends Model implements TelegramTwoFactor
{
    use HasAuth;
}

Save code in database and send notification with code in telegram

$user = User::first();
// Might be passed 2 params
// 1-st preffered length of code by default 6
// 2-nd is language by default en
$user->sendCode(4, 'ru'); // return boolean

Validate code

// The received code from the telegram must be passed to the method, which is described below
$code = 'CODE_FROM_TELEGRAM'; 
$user = User::first();
$user->validateCode($code); // return boolean

License

The MIT License (MIT). Please see License File for more information.