Forked from munierujp/echo_middleware_firebase_authentication.go
Created
August 12, 2022 02:36
-
-
Save HunterHeston/9a006101d998c420121c8082f222bfd9 to your computer and use it in GitHub Desktop.
Echo's middleware for Firebase Authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package middleware | |
import ( | |
"context" | |
"strings" | |
"github.com/labstack/echo/v4" | |
firebase "firebase.google.com/go" | |
"google.golang.org/api/option" | |
) | |
func Auth() echo.MiddlewareFunc { | |
return auth | |
} | |
func auth(next echo.HandlerFunc) echo.HandlerFunc { | |
return func(c echo.Context) error { | |
opt := option.WithCredentialsFile("firebase_secret_key.json") | |
app, err := firebase.NewApp(context.Background(), nil, opt) | |
if err != nil { | |
return err | |
} | |
client, err := app.Auth(context.Background()) | |
if err != nil { | |
return err | |
} | |
auth := c.Request().Header.Get("Authorization") | |
idToken := strings.Replace(auth, "Bearer ", "", 1) | |
token, err := client.VerifyIDToken(context.Background(), idToken) | |
if err != nil { | |
return err | |
} | |
c.Set("token", token) | |
return next(c) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment