2.6 KiB
2.6 KiB
Google Auth - Backend
Endpoint
POST /v1/auth/google-login
Request
{
"credential": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9..."
}
Response (200)
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "bearer",
"user": {
"id": "uuid",
"email": "user@gmail.com",
"username": "user",
"tenant_id": null,
"status": "ACTIVE",
"created_at": "...",
"profile": {
"full_name": "User Name",
"phone": null,
"timezone": "Asia/Jakarta"
}
}
}
Error Responses
| Status | Detail |
|---|---|
| 401 | Kredensial Google tidak valid atau telah kedaluwarsa. |
| 401 | Tidak dapat mengambil email dari akun Google. |
Logic
- Verify JWT token dari Google via
google.oauth2.id_token.verify_oauth2_token()denganGOOGLE_CLIENT_ID - Extract
emaildannamedari token payload - Cari user by
emaildi database - Jika user belum ada:
- Generate username dari email prefix (jika duplicate, tambah suffix angka:
user,user2,user3...) - Generate random 32-char password (tidak pernah dipakai)
- Set
email_verified_at = now(Google sudah verifikasi) - Set
status = ACTIVE - Buat
UserProfiledenganfull_namedari Google
- Generate username dari email prefix (jika duplicate, tambah suffix angka:
- Jika user sudah ada:
- Update
email_verified_atjika belum terisi - Update
full_namejika berubah - Create profile jika belum ada
- Update
- Return JWT access token + user detail
Schema
class GoogleLoginRequest(BaseModel):
credential: str
Config
Environment Variable
# api/.env
GOOGLE_CLIENT_ID=xxx.apps.googleusercontent.com
Settings
# api/app/config.py
class Settings(BaseSettings):
GOOGLE_CLIENT_ID: str = ""
Dependencies
google-auth==2.56.2
Ditambahkan ke api/requirements.txt.
Files Changed
| File | Change |
|---|---|
api/app/config.py |
Tambah GOOGLE_CLIENT_ID: str = "" |
api/.env |
Tambah GOOGLE_CLIENT_ID=xxx |
api/app/schemas/user.py |
Tambah GoogleLoginRequest schema |
api/app/routers/auth.py |
Tambah endpoint POST /v1/auth/google-login + imports |
api/requirements.txt |
Tambah google-auth==2.56.2 |
Design Decisions
- Tidak pakai kolom
google_id- User matching pakaiemaildari Google token, tidak perlu kolom baru di DB - Password field tetap NOT NULL - Google user dapat random password hash (tidak pernah dipakai)
- Auto-verify email -
email_verified_atlangsung diisi karena Google sudah verifikasi email - Status langsung ACTIVE - Tidak perlu alur onboarding untuk Google user