A lightweight, secure, and easy-to-use HTTP middleware for Go applications that integrates Firebase Authentication. This middleware helps you protect your HTTP endpoints by validating Firebase ID tokens and making user claims available in your request context.
- 🔒 Secure Firebase ID token validation
- 🚀 Simple integration with any Go HTTP server
- 🔑 Easy access to Firebase user claims
- ⚡️ Minimal overhead
- 🛡️ Built-in error handling
- 🔄 Context-based token storage
go get github.com/kw510/http-firebase-authpackage main
import (
"context"
"log"
"net/http"
firebase "firebase.google.com/go/v4"
"github.com/kw510/http-firebase-auth"
)
func main() {
// Initialize Firebase app
app, err := firebase.NewApp(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
// Create middleware
authMiddleware := firebaseauth.New(context.Background(), app)
// Create your handler
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get user claims from context
claims := firebaseauth.ClaimsFromContext(r.Context())
if claims != nil {
// Access user information
userID := claims.UID
// ... handle authenticated request
}
})
// Apply middleware
http.Handle("/protected", authMiddleware(handler))
log.Fatal(http.ListenAndServe(":8080", nil))
}First, ensure you have Firebase credentials set up in your environment or configuration.
authMiddleware := firebaseauth.New(context.Background(), app)// Apply to specific route
http.Handle("/protected", authMiddleware(handler))
// Or apply to all routes
http.Handle("/", authMiddleware(mux))claims := firebaseauth.ClaimsFromContext(r.Context())
if claims != nil {
userID := claims.UID
email := claims.Claims["email"].(string)
// ... use claims
}The middleware:
- Validates Firebase ID tokens
- Handles token expiration
- Provides secure context-based storage of claims
- Returns 401 Unauthorized for invalid tokens
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
Give a ⭐️ if this project helped you!