https://github.com/teh9/laravel-tg-2fa
A simple implementation of a two-factor authentication via Telegram for Laravel
https://github.com/teh9/laravel-tg-2fa
2fa auth authentication laravel login php security telegram two-factor
Last synced: 11 months ago
JSON representation
A simple implementation of a two-factor authentication via Telegram for Laravel
- Host: GitHub
- URL: https://github.com/teh9/laravel-tg-2fa
- Owner: teh9
- License: mit
- Created: 2022-11-07T17:04:49.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-05-08T19:16:21.000Z (about 3 years ago)
- Last Synced: 2025-06-15T05:41:59.776Z (about 1 year ago)
- Topics: 2fa, auth, authentication, laravel, login, php, security, telegram, two-factor
- Language: PHP
- Homepage:
- Size: 35.2 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Laravel two-factor authentication via Telegram
A simple two-factor implementation using Telegram for Laravel.



## 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
```php
'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:
```php
class User extends Model implements TelegramTwoFactor
{
use HasAuth;
}
```
### Save code in database and send notification with code in telegram
```php
$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
```php
// 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.