Dokumentasi ini menjelaskan cara mengkonfigurasi Google OAuth untuk login di aplikasi D-Form.
- Kunjungi Google Cloud Console
- Buat project baru atau pilih project yang sudah ada
- Pastikan project Anda sudah aktif
- Di Google Cloud Console, buka menu APIs & Services → Library
- Cari "Google+ API" atau "Google People API"
- Klik Enable untuk mengaktifkan API
-
Buka menu APIs & Services → Credentials
-
Klik Create Credentials → OAuth client ID
-
Jika diminta, konfigurasikan OAuth consent screen terlebih dahulu:
- Pilih External (kecuali Anda menggunakan Google Workspace)
- Isi informasi aplikasi:
- App name: D-Form (atau nama aplikasi Anda)
- User support email: email Anda
- Developer contact information: email Anda
- Klik Save and Continue
- Di bagian Scopes, tambahkan scope berikut:
.../auth/userinfo.email.../auth/userinfo.profile
- Klik Save and Continue
- Tambahkan test users jika masih dalam mode testing
- Klik Save and Continue
-
Kembali ke Credentials → Create Credentials → OAuth client ID
-
Pilih Application type: Web application
-
Isi informasi:
- Name: D-Form Web Client (atau nama lain)
- Authorized JavaScript origins:
http://localhost:8000 http://127.0.0.1:8000 https://yourdomain.com (untuk production) - Authorized redirect URIs:
http://localhost:8000/auth/google/callback http://127.0.0.1:8000/auth/google/callback https://yourdomain.com/auth/google/callback (untuk production)
-
Klik Create
-
Salin Client ID dan Client Secret yang muncul
- Buka file
.envdi root project Anda - Tambahkan konfigurasi Google OAuth:
# Google OAuth Configuration GOOGLE_CLIENT_ID=your-client-id-here.apps.googleusercontent.com GOOGLE_CLIENT_SECRET=your-client-secret-here GOOGLE_REDIRECT_URI=http://localhost:8000/auth/google/callback # Untuk production # GOOGLE_REDIRECT_URI=https://yourdomain.com/auth/google/callback
- Ganti
your-client-id-heredanyour-client-secret-heredengan credentials yang Anda dapatkan dari Google Cloud Console
-
Jalankan aplikasi Laravel:
php artisan serve
-
Buka browser dan akses halaman login:
http://localhost:8000/admin/login -
Klik tombol Sign in with Google
-
Anda akan diarahkan ke halaman login Google
-
Setelah berhasil login, Anda akan diarahkan kembali ke dashboard admin
Solusi: Pastikan URL redirect yang dikonfigurasi di Google Cloud Console sama persis dengan URL di aplikasi Anda (termasuk protokol http/https dan port).
Solusi: Periksa kembali Client ID dan Client Secret di file .env. Pastikan tidak ada spasi atau karakter tambahan.
Solusi:
- Pastikan Google+ API atau Google People API sudah diaktifkan
- Periksa konfigurasi OAuth consent screen
- Jika masih dalam mode testing, pastikan email Anda sudah ditambahkan sebagai test user
Catatan: Secara default, user yang login melalui Google OAuth akan dibuat sebagai user biasa. Jika Anda ingin user tertentu memiliki akses admin, Anda perlu:
-
Assign role admin melalui database atau seeder:
$user = User::where('email', '[email protected]')->first(); $user->assignRole('admin');
-
Atau modifikasi controller
GoogleController.phpuntuk assign role secara otomatis berdasarkan email tertentu.
Ketika deploy ke production:
-
Update Authorized redirect URIs di Google Cloud Console dengan domain production Anda
-
Update file
.envdi server production:GOOGLE_REDIRECT_URI=https://yourdomain.com/auth/google/callback APP_URL=https://yourdomain.com
-
Publish OAuth consent screen (dari Testing ke Production) jika diperlukan
- JANGAN commit file
.envke repository - JANGAN share Client Secret secara publik
- Gunakan HTTPS di production
- Pertimbangkan untuk menambahkan rate limiting pada route OAuth
Untuk menambahkan fungsi logout, tambahkan route di routes/web.php:
Route::post('/logout', function () { Auth::logout(); request()->session()->invalidate(); request()->session()->regenerateToken(); return redirect('/'); })->name('logout');
Avatar user dari Google sudah disimpan di database. Untuk menampilkannya di UI, Anda bisa menggunakan:
@auth @if(auth()->user()->avatar) <img src="{{ auth()->user()->avatar }}" alt="{{ auth()->user()->name }}" class="rounded-full w-10 h-10"> @else <div class="rounded-full w-10 h-10 bg-gray-300 flex items-center justify-center"> {{ substr(auth()->user()->name, 0, 1) }} </div> @endif @endauth
Jika mengalami masalah, silakan:
- Periksa log Laravel di
storage/logs/laravel.log - Periksa console browser untuk error JavaScript
- Pastikan semua migration sudah dijalankan:
php artisan migrate
Dibuat untuk D-Form (DOSCOM Form)
Laravel 12 + Livewire + Google OAuth