Google OAuth Setup Guide โ
Panduan lengkap untuk mengaktifkan Google Login di aplikasi SvelteKit ini.
๐ Overview โ
Google OAuth memungkinkan user login dengan satu klik menggunakan akun Google mereka. Lebih cepat dan user-friendly daripada register manual.
๐ Langkah Setup โ
1. Buka Google Cloud Console โ
- Kunjungi Google Cloud Console
- Login dengan akun Google Anda
- Buat project baru atau pilih project existing
2. Enable Google+ API โ
- Di sidebar, klik "APIs & Services" > "Library"
- Cari "Google+ API" atau "Google Identity Toolkit API"
- Klik "Enable"
Note: Google+ API sudah deprecated, yang aktif sekarang adalah Google Identity Services yang otomatis enabled saat setup OAuth consent screen.
3. Configure OAuth Consent Screen โ
- Di sidebar, klik "APIs & Services" > "OAuth consent screen"
- Pilih "External" (untuk testing) atau "Internal" (jika pakai Google Workspace)
- Klik "Create"
Isi App Information: โ
- App name: Nama aplikasi Anda (contoh: "My SvelteKit App")
- User support email: Email support
- App logo: (Opsional) Upload logo 120x120px
- App domain: Domain production Anda
- Developer contact information: Email developer
- Klik "Save and Continue"
4. Add Scopes โ
- Di halaman Scopes, klik "Add or Remove Scopes"
- Centang scope berikut:
- โ
openid- OpenID Connect - โ
userinfo.email- Email address - โ
userinfo.profile- Profile info
- โ
- Klik "Update" kemudian "Save and Continue"
5. Add Test Users (Untuk Development) โ
- Di halaman Test users, klik "Add Users"
- Masukkan email address untuk testing (bisa email Anda sendiri)
- Klik "Add" kemudian "Save and Continue"
Penting: Saat masih dalam status "Testing", hanya test users yang bisa login!
6. Create OAuth 2.0 Credentials โ
- Di sidebar, klik "APIs & Services" > "Credentials"
- Klik "+ Create Credentials" > "OAuth 2.0 Client ID"
- Pilih Application type: "Web application"
- Isi Name: "SvelteKit Web Client"
Authorized Redirect URIs โ
Tambahkan redirect URIs berikut:
Untuk Local Development:
http://localhost:5173/auth/google/callbackUntuk Production:
https://your-domain.pages.dev/auth/google/callback
https://www.your-domain.com/auth/google/callbackCatatan: Ganti
your-domaindengan domain Anda yang sebenarnya.
- Klik "Create"
7. Copy Credentials โ
Setelah create, Anda akan melihat:
- Client ID (contoh:
123456789-abc123def456.apps.googleusercontent.com) - Client Secret (contoh:
GOCSPX-xxxxxxxxxxxxxxxx)
Simpan keduanya dengan aman!
๐ง Konfigurasi di Project โ
1. Update Environment Variables โ
Edit file .env:
# Google OAuth Configuration
GOOGLE_CLIENT_ID=123456789-abc123def456.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=GOCSPX-xxxxxxxxxxxxxxxx2. Update wrangler.toml (Production) โ
Untuk production, tambahkan ke wrangler.toml:
[env.production.vars]
GOOGLE_CLIENT_ID = "your-production-client-id"Atau set via Cloudflare Dashboard:
- Buka Pages > Your Project > Settings > Functions
- Tambahkan environment variables:
GOOGLE_CLIENT_IDGOOGLE_CLIENT_SECRET
๐งช Testing โ
1. Local Development โ
npm run dev- Buka http://localhost:5173/login
- Klik "Continue with Google"
- Pilih akun Google Anda
- Jika berhasil, akan redirect ke dashboard
2. Production โ
npm run deploy- Buka https://your-domain.pages.dev/login
- Test Google Login
โ ๏ธ Common Issues โ
"Error 400: redirect_uri_mismatch" โ
Penyebab: Redirect URI tidak match dengan yang didaftarkan di Google Cloud Console.
Solusi:
- Pastikan URI di Google Cloud Console sama persis dengan callback URL
- Perhatikan
httpvshttps - Perhatikan trailing slash (jangan ada
/di akhir)
"Error 403: access_denied" โ
Penyebab: User belum ditambahkan sebagai test user.
Solusi:
- Tambahkan email user ke Test users di OAuth consent screen
- Atau publish app ke production (lihat di bawah)
"This app isn't verified" โ
Penyebab: App masih dalam status testing.
Solusi:
- Klik "Advanced" > "Go to [Your App] (unsafe)"
- Atau publish app (lihat di bawah)
๐ Publish ke Production โ
Jika app sudah siap untuk public:
- Di OAuth consent screen, klik "PUBLISH APP"
- Konfirmasi publish
- Tunggu review dari Google (biasanya instan untuk scope basic)
Setelah published, semua user dengan akun Google bisa login.
๐ Security Best Practices โ
- Jangan commit Client Secret - Selalu pakai environment variables
- Gunakan HTTPS - Wajib untuk production
- Validasi State Parameter - Sudah diimplementasikan di code
- Restrict OAuth Usage - Di Google Cloud Console, restrict ke domain Anda saja
๐ Tambahan โ
Custom Branding โ
Di OAuth consent screen, Anda bisa:
- Upload logo aplikasi
- Set custom domain untuk consent screen
- Tambahkan privacy policy dan terms of service links
Multiple Environments โ
Buat credentials terpisah untuk:
- Development (localhost)
- Staging (staging.yourdomain.com)
- Production (yourdomain.com)
Setiap environment punya Client ID dan Secret yang berbeda.
๐ Butuh Bantuan? โ
Setelah setup selesai, user bisa login dengan Google di aplikasi Anda! ๐