В этой статье я хотел бы рассказать о том, как реализовать авторизацию в веб-проекте через телеграмм. Исходный проект реализован на фреймворке ларавел 10. Итак начнем, обсуждать проблему по шагам.
1. Первое выполним в консоли команду
composer require socialiteproviders/telegram
2. Далее в файле config/services.php
прописываем следующий код:
'telegram' => ['bot' => env('TELEGRAM_BOT_NAME'), // The bot's username'client_id' => null,'client_secret' => env('TELEGRAM_TOKEN'),'redirect' => env('TELEGRAM_REDIRECT_URI'),],
TELEGRAM_BOT_NAME=MyFirstBotTELEGRAM_TOKEN=6342418556:AAH3hghjTT7nU9GfBeXzIXFDdJCmLLaS9bvUTELEGRAM_REDIRECT_URI=auth/telegram/callback
Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) {$event->extendSocialite('telegram', \SocialiteProviders\Telegram\Provider::class);});
Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) {$event->extendSocialite('telegram', \SocialiteProviders\Telegram\Provider::class);});
// Авторизация через телеграмм Route::get('auth/telegram/redirect', function() { return Socialite::driver('telegram')->redirect(); })->name('soc_telegram'); Route::get('auth/telegram/callback', function () { $telegramUser = Socialite::driver('telegram')->user(); $user = \App\Models\User::updateOrCreate([ 'telegram_id' => $telegramUser->getId() ], [ 'telegram_id' => $telegramUser->getId(), 'name' => $telegramUser->getNickname(), 'email' => $telegramUser->getEmail() ?? $telegramUser->getId()."@telegram.com" ]); \Illuminate\Support\Facades\Auth::login($user); return redirect()->route('home'); })->name('back_telegram');
{!! Socialite::driver('telegram')->getButton() !!}
return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::table('users', function (Blueprint $table) { // $table->unsignedBigInteger('telegram_id')->nullable(); }); } /** * Reverse the migrations. */ public function down(): void { Schema::table('users', function (Blueprint $table) { $table->dropColumn('telegram_id'); }); } };
ServerAdmin webmaster@157.90.119.134/public/ProxyPreserveHost OnProxyPass / http://127.0.0.1:80/ProxyPassReverse / http://127.0.0.1:80/ServerName https://157.90.119.134/public/ServerAdmin webmaster@mysite.ruProxyPreserveHost OnProxyPass / http://127.0.0.1:80/ProxyPassReverse / http://127.0.0.1:80/ServerName https://mysite.ru
127.0.0.1 https://mysite.ru