fire_verify_auth

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2022 License: MIT Imports: 9 Imported by: 0

README

Firebase Middleware Verify Id Token

Install

go get github.com/duongdam/fire-verify-auth

Usage

package firebase_verify_accessToken

import (
	"github.com/gofiber/fiber/v2"
	"os"
	"strings"

	authVerify "github.com/duongdam/fire-verify-auth"
)

type Config struct {
	Key []byte
}

const (
	authorizationHeaderKey  = "authorization"
	authorizationTypeBearer = "bearer"
	userId                  = "user_id"
)

func authMiddleware() fiber.Handler {
	return func(ctx *fiber.Ctx) error {
		authorizationHeader := ctx.Get(authorizationHeaderKey)
		fields := strings.Fields(authorizationHeader)
		authorizationType := strings.ToLower(fields[0])
  
		accessToken := fields[1]

		// Verify token
		claims, err := authVerify.VerifyIDToken(accessToken, os.Getenv("GO_PROJECT_ID"))
		if err != nil {
			return ctx.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
				"error": err.Error(),
			})
		}
		signInProvider := claims["firebase"].(map[string]interface{})["sign_in_provider"].(string)

		println("signInProvider", signInProvider)

		ctx.Next()
		return nil
	}
}

https://github.com/duongdam/fire-verify-auth

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func VerifyIDToken

func VerifyIDToken(idToken string, projectID string) (map[string]interface{}, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL