2023-05-10 00:14:28 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Laravel\Socialite\Facades\Socialite;
|
|
|
|
use Laravel\Socialite\Two\InvalidStateException;
|
|
|
|
|
|
|
|
|
|
|
|
class GoogleAuthController extends Controller
|
|
|
|
{
|
|
|
|
public function redirectToGoogle()
|
|
|
|
{
|
|
|
|
return Socialite::driver('google')->redirect();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handleGoogleCallback()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$socialUser = Socialite::driver('google')->user();
|
|
|
|
} catch (InvalidStateException $exception) {
|
|
|
|
|
|
|
|
return redirect()->route('login')
|
|
|
|
->withErrors(
|
|
|
|
[
|
|
|
|
'email' => [
|
|
|
|
__('Google Login failed, please try again.'),
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$user = User::where('email', $socialUser->getEmail())->first();
|
|
|
|
|
|
|
|
|
|
|
|
if ($user) {
|
|
|
|
$user->update([
|
|
|
|
'avatar' => $socialUser->avatar,
|
|
|
|
]);
|
|
|
|
|
|
|
|
Auth::login($user, true);
|
2023-05-10 00:54:14 -07:00
|
|
|
return redirect()->route('home');
|
2023-05-10 00:14:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('login')
|
|
|
|
->withErrors(
|
|
|
|
[
|
|
|
|
'email' => [
|
|
|
|
trans('admin/users/message.user_not_found'),
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|