#1 add authorization with firebase
Co-authored-by: Tim <timwundenberg@outlook.de> Reviewed-on: tim/me-fit#2
This commit is contained in:
36
api/utils/auth.go
Normal file
36
api/utils/auth.go
Normal file
@@ -0,0 +1,36 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user