mirror of
https://github.com/snipe/snipe-it.git
synced 2024-11-10 15:44:11 -08:00
b7e8ce3064
Signed-off-by: snipe <snipe@snipe.net>
57 lines
1.3 KiB
PHP
57 lines
1.3 KiB
PHP
<?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);
|
|
return redirect()->route('home');
|
|
}
|
|
|
|
return redirect()->route('login')
|
|
->withErrors(
|
|
[
|
|
'email' => [
|
|
trans('admin/users/message.user_not_found'),
|
|
],
|
|
]
|
|
);
|
|
}
|
|
} |