-
-
Save avary/60e9fdac3a269b9921f7be06cc728140 to your computer and use it in GitHub Desktop.
This file contains hidden or 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" | |
"net/http" | |
"strings" | |
firebase "firebase.google.com/go" | |
"github.com/labstack/echo" | |
"google.golang.org/api/option" | |
rc "firebase-authentication-with-react-and-go/backend/router/context" | |
) | |
func Auth() echo.MiddlewareFunc { | |
return func(next echo.HandlerFunc) echo.HandlerFunc { | |
return func(c echo.Context) error { | |
rcc := &rc.Context{ | |
Context: c, | |
Token: nil, | |
} | |
opt := option.WithCredentialsFile("/Users/kurodamanato/.keys/firebase-service-key-2.json") | |
app, err := firebase.NewApp(context.Background(), nil, opt) | |
if err != nil { | |
return c.JSON(http.StatusInternalServerError, err.Error()) | |
} | |
auth, err := app.Auth(context.Background()) | |
if err != nil { | |
return c.JSON(http.StatusInternalServerError, err.Error()) | |
} | |
header := c.Request().Header.Get(echo.HeaderAuthorization) | |
idToken := strings.TrimSpace(strings.Replace(header, "Bearer", "", 1)) | |
token, err := auth.VerifyIDToken(context.Background(), idToken) | |
if err != nil { | |
return c.JSON(http.StatusInternalServerError, err.Error()) | |
} | |
// Set context to token | |
rcc.Token = token | |
return next(rcc) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment