Answer the question
In order to leave comments, you need to log in
How to override REDIRECT_URI for Laravel Socialite depending on the type of user being authenticated?
Good day. I ran into a problem when authenticating through social networks for several entities. There are two entities: Student and Teacher. To begin with, I will describe a working version for one entity.
My authentication routes:
//Socialite auth routes...
Route::middleware('guest')->group(function () {
Route::get('auth/{provider}', '[email protected]')->name('auth.provider');
Route::get('auth/{provider}/callback', '[email protected]');
});
class SocialController extends Controller
{
public function index($provider)
{
return Socialite::driver($provider)->redirect();
}
public function callback(SocialService $socialService, $provider)
{
$user = Socialite::driver($provider)->user();
if ($u = $socialService->saveSocialData($user)) {
Auth::guard('student')->login($u);
return redirect()->route('profile.index');
}
return back();
}
}
VKONTAKTE_CLIENT_ID=7864077
VKONTAKTE_CLIENT_SECRET=Z2i92xXe47mGJY3D5DQE
VKONTAKTE_REDIRECT_URI=http://127.0.0.1:8000/auth/vkontakte/callback
//Socialite auth routes...
Route::middleware('guest')->group(function () {
Route::get('auth/{client}/{provider}', '[email protected]')->name('auth.provider');
Route::get('auth/{client}/{provider}/callback', '[email protected]');
});
VKONTAKTE_REDIRECT_URI=http://127.0.0.1:8000/auth/student/vkontakte/callback
VKONTAKTE_REDIRECT_URI=http://127.0.0.1:8000/auth/teacher/vkontakte/callback
Answer the question
In order to leave comments, you need to log in
Solved the problem like this:
Authorization controller
class SocialController extends Controller
{
public function index(SocialService $socialService, $client, $provider)
{
$socialService->setConfig($client, $provider);
return Socialite::driver($provider)->redirect();
}
public function callback(SocialService $socialService, $client, $provider)
{
$socialService->setConfig($client, $provider);
$user = Socialite::driver($provider)->user();
if ($u = $socialService->saveSocialData($client, $user)) {
Auth::guard($client)->login($u);
return redirect()->route('profile.index');
}
return back();
}
}
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Hash;
class SocialService
{
public function saveSocialData($client, $user)
{
if ($client == 'student') {
$model = new Student();
}
if ($client == 'teacher') {
$model = new Teacher();
}
$email = $user->getEmail();
$u = $model->where('email', $email)->first();
if ($u == null) {
$data = [
'first_name' => 'First',
'middle_name' => 'Middle',
'last_name' => 'Last',
'birth' => today(),
'email' => $email,
'password' => Hash::make('secret123'),
];
return $model->create($data);
}
return $u;
}
public function setConfig($client, $provider)
{
if ($provider == 'vkontakte') {
$clientId = env('VKONTAKTE_CLIENT_ID');
$clientSecret = env('VKONTAKTE_CLIENT_SECRET');
$redirectUrl = 'http://127.0.0.1:8000/auth/'.$client.'/vkontakte/callback';
}
if ($provider == 'google') {
$clientId = env('GOOGLE_CLIENT_ID');
$clientSecret = env('GOOGLE_CLIENT_SECRET');
$redirectUrl = 'http://127.0.0.1:8000/auth/'.$client.'/google/callback';
}
Config::set(['services.'.$provider => [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'redirect' => $redirectUrl
]]);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question