51 lines
1.4 KiB
PHP
51 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\User;
|
|
use Auth;
|
|
use Laravel\Socialite\Facades\Socialite;
|
|
|
|
class SocialLoginController extends Controller
|
|
{
|
|
public function redirect(string $driver)
|
|
{
|
|
return Socialite::driver($driver)
|
|
->scopes(['name', 'email'])
|
|
->redirect();
|
|
}
|
|
|
|
public function handleProviderCallback(string $driver)
|
|
{
|
|
$social = Socialite::driver($driver)->user();
|
|
$user = User::where(['email' => strtolower($social->getEmail())])->first();
|
|
if ($user == null) {
|
|
$user = User::where(['username' => strtolower($social->getNickname())])->first();
|
|
}
|
|
|
|
if ($user) {
|
|
Auth::login($user);
|
|
|
|
$updates = [];
|
|
if ($user->profile_photo_path == null) {
|
|
$updates['profile_photo_path'] = $social->getAvatar();
|
|
}
|
|
if ($user->email == null) {
|
|
$updates['email'] = $social->getEmail();
|
|
}
|
|
$updates = array_purge($updates);
|
|
if (! empty($updates)) {
|
|
$user->update($updates);
|
|
}
|
|
|
|
return redirect()->route('dashboard');
|
|
} else {
|
|
$provider = ucfirst($driver);
|
|
|
|
return redirect()
|
|
->route('login')
|
|
->withErrors("Email {$social->getEmail()} not found for {$provider} login.");
|
|
}
|
|
}
|
|
}
|