37 lines
710 B
Go
37 lines
710 B
Go
package utils
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
|
|
firebase "firebase.google.com/go"
|
|
"firebase.google.com/go/auth"
|
|
"google.golang.org/api/option"
|
|
)
|
|
|
|
var app *firebase.App
|
|
|
|
func VerifyToken(token string) (*auth.Token, error) {
|
|
if app == nil {
|
|
setup()
|
|
}
|
|
|
|
client, err := app.Auth(context.Background())
|
|
if err != nil {
|
|
log.Fatalf("error getting Auth client: %v\n", err)
|
|
}
|
|
return client.VerifyIDToken(context.Background(), token)
|
|
}
|
|
|
|
func setup() {
|
|
opt := option.WithCredentialsFile("./secrets/me-fit-a9365-firebase-adminsdk-8065w-7cf8443902.json")
|
|
|
|
firebaseApp, err := firebase.NewApp(context.Background(), nil, opt)
|
|
|
|
if err != nil {
|
|
log.Fatalf("error initializing app: %v", err)
|
|
}
|
|
|
|
app = firebaseApp
|
|
}
|