Решил проблему так:
Контролер авторизации
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
]]);
}
}