Authorization via Yandex, VK, Telegram using Laravel Socialite
Introduction
There are many articles and tutorials on the Internet on how to integrate OAuth authorization through popular providers such as Google, Facebook and Twitter into Laravel projects. But how to do this through Yandex, VK, etc. There is no information in RuNet at all.
I will show you using Yandex as an example. Do the same with other providers.
Step 1: Create an application
First, we register our application in Yandex OAuth
![Enter the name, etc. use Laravel - select Web Services Enter the name, etc. use Laravel - select Web Services](https://habrastorage.org/getpro/habr/upload_files/f3a/33f/ff9/f3a33fff962a86fbee4e3666eac19bcc.png)
![Insert a link to the page where the user will be redirected after authorization (for example, to the main page) Insert a link to the page where the user will be redirected after authorization (for example, to the main page)](https://habrastorage.org/getpro/habr/upload_files/298/9d6/af1/2989d6af1b8b19018408079b905ac02f.png)
After registering the application, go to our application page and save these 3 fields, we will need them a little later.
![](https://habrastorage.org/getpro/habr/upload_files/426/c70/b33/426c70b33c8585085d495faefd51ec68.png)
Step 2: Install Socialite
Install Laravel Socialite in our project via composer
composer require laravel/socialite
Installing SocialiteProviders
SocialiteProviders is an extension for Laravel Socialite that adds support for many additional OAuth providers, including Yandex, VK, Telegram and others. Full list of providers at their website.
composer require socialiteproviders/yandex
Step 3. Project Setup
In file config/services.php
add settings for Yandex:
'yandex' => [
'client_id' => env('YANDEX_CLIENT_ID'),
'client_secret' => env('YANDEX_CLIENT_SECRET'),
'redirect' => env('YANDEX_REDIRECT_URI'), ],
Insert into .env
file variables that Yandex gave us:
YANDEX_CLIENT_ID=*ClientID*
YANDEX_CLIENT_SECRET=*Client secret*
YANDEX_REDIRECT_URI=*Redirect URI для веб-сервисов*
Let's add routes for redirecting to Yandex and processing the callback:
Route::get('login/yandex', [AuthenticatedSessionController::class, 'yandex'])->name('yandex');
Route::get('login/yandex/redirect', [AuthenticatedSessionController::class, 'yandexRedirect'])->name('yandexRedirect');
Let's add the necessary functions to the controller:
use Laravel\Socialite\Facades\Socialite;
use Str;
public function yandex() // перенаправляем юзера на яндекс Auth
{
return Socialite::driver('yandex')->redirect();
}
public function yandexRedirect() // принимаем возвращаемые данные и работаем с ними
{
$user = Socialite::driver('yandex')->user();
$user = User::firstOrCreate([ // используем firstOrCreate для проверки есть ли такие пользователи в нашей БД
'email' => $user->email
], [
'name' => $user->user['display_name'], // display_name - переменаая хранящая полное ФИО пользователя
// остальные переменные можете посмотреть использовав $dd('$user')
'password' => Hash::make(Str::random(24)),
]);
Auth::login($user, true);
redirect()->route('main');
}
Put our first route on the “login via Yandex” button and you’re done. Using the same scheme, you do telegram, vk and other services listed in socialiteproviders.