40 lines
1.0 KiB
PHP
40 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Laravel\Socialite\Facades\Socialite;
|
|
use App\Models\User;
|
|
|
|
class GoogleController extends Controller
|
|
{
|
|
public function redirectToGoogle()
|
|
{
|
|
return Socialite::driver('google')->redirect();
|
|
}
|
|
|
|
public function handleGoogleCallback()
|
|
{
|
|
try {
|
|
$googleUser = Socialite::driver('google')->stateless()->user();
|
|
|
|
$user = User::where('email', $googleUser->getEmail())->first();
|
|
|
|
if (!$user) {
|
|
$user = User::create([
|
|
'name' => $googleUser->getName(),
|
|
'email' => $googleUser->getEmail(),
|
|
'password' => bcrypt(uniqid()),
|
|
]);
|
|
}
|
|
|
|
Auth::login($user);
|
|
|
|
return redirect()->route('home');
|
|
} catch (\Exception $e) {
|
|
return redirect()->route('login')->with('error', 'Login Google gagal!');
|
|
}
|
|
}
|
|
}
|