|
| 1 | +--- |
| 2 | +topic: auth |
| 3 | +last_verified: 2026-06-15 |
| 4 | +sources: |
| 5 | + - internal/usecase/auth_usecase.go |
| 6 | + - internal/transport/middleware/auth.go |
| 7 | + - internal/transport/handlers/auth_handler.go |
| 8 | + - pkg/firebase/admin.go |
| 9 | + - internal/bootstrap/bootstrap.go |
| 10 | +--- |
| 11 | + |
| 12 | +# Firebase Auth |
| 13 | + |
| 14 | +## Domain type |
| 15 | + |
| 16 | +`FirebaseToken` is defined in `internal/usecase/auth_usecase.go` and holds the claims extracted from a verified Firebase ID token: |
| 17 | + |
| 18 | +```go |
| 19 | +type FirebaseToken struct { |
| 20 | + UID string `json:"uid"` |
| 21 | + Email string `json:"email"` |
| 22 | + Name string `json:"name"` |
| 23 | + PhotoURL string `json:"photoUrl"` |
| 24 | + Claims map[string]any `json:"claims"` |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +`UID`, `Email`, `Name`, and `PhotoURL` are promoted from the raw Firebase JWT claims (`uid`, `email`, `name`, `picture`). `Claims` contains the full unmodified payload. |
| 29 | + |
| 30 | +## Usecase interfaces |
| 31 | + |
| 32 | +Both interfaces live in `internal/usecase/auth_usecase.go`. |
| 33 | + |
| 34 | +```go |
| 35 | +// FirebaseTokenVerifier can verify a raw Firebase ID token string. |
| 36 | +type FirebaseTokenVerifier interface { |
| 37 | + VerifyIDToken(ctx context.Context, idToken string) (*FirebaseToken, error) |
| 38 | +} |
| 39 | + |
| 40 | +// FirebaseAdminClient extends FirebaseTokenVerifier with user-management operations. |
| 41 | +type FirebaseAdminClient interface { |
| 42 | + FirebaseTokenVerifier |
| 43 | + GetUserByEmail(ctx context.Context, email string) (string, error) |
| 44 | + UpdateUserPassword(ctx context.Context, uid, newPassword string) error |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +`FirebaseTokenVerifier` is the narrow interface used by the `FirebaseAuth` middleware. `FirebaseAdminClient` is the broader interface stored on `bootstrap.App` and wired into the server. |
| 49 | + |
| 50 | +## pkg/firebase/admin.go |
| 51 | + |
| 52 | +`NewAuthClient` initialises the Firebase Admin SDK and returns a `usecase.FirebaseAdminClient`: |
| 53 | + |
| 54 | +```go |
| 55 | +func NewAuthClient(ctx context.Context, projectID, credentialsJSON string) (usecase.FirebaseAdminClient, error) |
| 56 | +``` |
| 57 | + |
| 58 | +- When `credentialsJSON` is non-empty the SDK is initialised with `option.WithCredentialsJSON` (service account key). |
| 59 | +- When `credentialsJSON` is empty the SDK falls back to Application Default Credentials (ADC) — used on GCP. |
| 60 | + |
| 61 | +The returned value is `*authClientAdapter`, a private type that wraps `*auth.Client` from the Firebase Admin SDK. This adapter satisfies both `FirebaseTokenVerifier` and `FirebaseAdminClient` without leaking the SDK type into the application layer. |
| 62 | + |
| 63 | +## FirebaseAuth middleware |
| 64 | + |
| 65 | +Defined in `internal/transport/middleware/auth.go`. |
| 66 | + |
| 67 | +```go |
| 68 | +const FirebaseClaimsKey = "firebase_claims" |
| 69 | + |
| 70 | +func FirebaseAuth(verifier usecase.FirebaseTokenVerifier) gin.HandlerFunc |
| 71 | +``` |
| 72 | + |
| 73 | +For every request the middleware: |
| 74 | +1. Reads the `Authorization` header; aborts with `401` if the header is missing or does not start with `Bearer `. |
| 75 | +2. Calls `verifier.VerifyIDToken(ctx, idToken)`. |
| 76 | +3. On success: stores `*usecase.FirebaseToken` in the Gin context under `FirebaseClaimsKey` and calls `c.Next()`. |
| 77 | +4. On error: aborts with `401` and `{"error": "invalid or expired token"}`. |
| 78 | +
|
| 79 | +Retrieve claims inside a handler: |
| 80 | +```go |
| 81 | +val, _ := c.Get(middleware.FirebaseClaimsKey) |
| 82 | +token, ok := val.(*usecase.FirebaseToken) |
| 83 | +``` |
| 84 | + |
| 85 | +## MeHandler (GET /api/v1/me) |
| 86 | + |
| 87 | +Defined in `internal/transport/handlers/auth_handler.go`. |
| 88 | + |
| 89 | +```go |
| 90 | +func (h *Handler) MeHandler(c *gin.Context) |
| 91 | +``` |
| 92 | + |
| 93 | +- Reads `*usecase.FirebaseToken` from the Gin context (`FirebaseClaimsKey`). |
| 94 | +- Returns `200 OK` with the token struct serialised as JSON. |
| 95 | +- Returns `401 Unauthorized` with `{"error": "unauthorized"}` if the context value is missing or of the wrong type (should not happen when `FirebaseAuth` is applied to the group). |
| 96 | +
|
| 97 | +The handler is registered on the `/api/v1` group in `RegisterRoutes`: |
| 98 | +```go |
| 99 | +api := r.Group("/api/v1") |
| 100 | +if verifier != nil { |
| 101 | + api.Use(middleware.FirebaseAuth(verifier)) |
| 102 | +} |
| 103 | +api.GET("/me", h.MeHandler) |
| 104 | +``` |
| 105 | + |
| 106 | +## Disabling auth in development |
| 107 | + |
| 108 | +When `FIREBASE_PROJECT_ID` is not set `bootstrap.Run` skips Firebase initialisation and `app.Firebase` is `nil`. `server.NewServer` passes `app.Firebase` directly to `RegisterRoutes` as the `verifier` argument. When `verifier` is `nil` the `if verifier != nil` guard in `RegisterRoutes` skips `api.Use(middleware.FirebaseAuth(...))`, so `/api/v1/me` is reachable without a token. |
| 109 | + |
| 110 | +To enable auth locally set both `FIREBASE_PROJECT_ID` and `FIREBASE_SERVICE_ACCOUNT_JSON` in `backend/.env`. |
0 commit comments