Halaman login laravel dengan tambahan gmail register
Setelah laravel 12 terinstall
ikuti langkah-langkah berikut untuk membuat halaman login
Masuk direktori dimana laravel 12 terinstall, lalu ketik perintah
composer require laravel/breeze
setelah itu
php artisan breeze:install lalu pilih blade
Ubah Tabel Users dan Model User
Tabel User (databases/migration/.....user)
Model User (app/Model/User.php)
Lalu ketikan perintah
php artisan migrate
jika berhasil maka akan muncul tampilan seperti berikut
Install Socialite Package
Jalankan perintah
composer require laravel/socialite
info packages yang terinstall akan muncul seperti berikut
Buat Controller SocialController
php artisan make:controller SocialController
Tambahkan kode berikut di SocialController.php
Import Kelas
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;
Masukkan kode berikut ke dalam file SocialController.php
public function googleRedirect()
{
return Socialite::driver('google')->redirect();
}
public function loginWithGoogle()
{
try {
$googleUser = Socialite::driver('google')->stateless()->user();
$existingUser = User::where('google_id',$googleUser->id)
->orWhere('email',$googleUser->email)
->firest();
if ($existingUser){
if($existingUser->google_id !== $googleUser->id){
$existingUser->google_id = $googleUser->id;
$existingUser->save();
}
Auth::login($existingUser);
} else {
$createUser = User::create([
'name' => $googleUser->name,
'email' => $googleUser->email,
'google_id' => $googleUser->id,
'password' =>bcrypt('password'),
]);
Auth::login($createUser);
}
return redirect()->to('/dashboard');
} catch (\Throwable $th){
throw $th;
}
}
Menyiapkan Route
Import kelas terleih dahulu
use App\Http\Controllers\SocialController;
Berikutnya tentukan rute untuk autentikasi Google
Route::get('auth/redirect',[SocialController::class, 'googleRedirect']);
Route::get('auth/callback',[SocialController::class, 'loginWithGoogle']);
Tambahkan Tautan Login Google ke Balde Registrasi/Login
Jika muncul error saat mengakses halaman login seperti ini
Vite manifest not found at: /home/website.laravel/public_html/public/build/manifest.json
lakukan perintah berikut
apt install npm lalu
npm run build
kemudian ketika perintah berikut
npm install
npm run dev


