Skip to content

Commit

Permalink
fix: firebaseAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
ken109 committed Nov 20, 2021
1 parent f47c3c6 commit 2ea66d1
Show file tree
Hide file tree
Showing 7 changed files with 511 additions and 178 deletions.
54 changes: 40 additions & 14 deletions context/context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package context

import (
"fmt"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/nimil-jp/gin-utils/errors"
"gorm.io/gorm"
Expand All @@ -9,7 +12,8 @@ import (
type Context interface {
RequestID() string
Authenticated() bool
UserID() uint
UID() uint
FirebaseUID() string

Validate(request interface{}) (ok bool)
FieldError(fieldName string, message string)
Expand All @@ -21,22 +25,40 @@ type Context interface {
}

type ctx struct {
id string
verr *errors.Error
getDB func() *gorm.DB
db *gorm.DB
userID uint
id string
verr *errors.Error
getDB func() *gorm.DB
db *gorm.DB
uid uint
firebaseUID string
}

func New(requestID string, userID uint, getDB func() *gorm.DB) Context {
func New(appName string, c *gin.Context, getDB func() *gorm.DB) Context {
requestID := c.GetHeader("X-Request-Id")
if requestID == "" {
requestID = uuid.New().String()
}

firebaseUID := ""
firebaseUIDInterface, ok := c.Get("firebase_uid")
if ok {
firebaseUID = firebaseUIDInterface.(string)
}

var uid uint
claimsInterface, ok := c.Get("claims")
if ok {
if uidInterface, ok := claimsInterface.(map[string]interface{})[fmt.Sprintf("%s_id", appName)]; ok {
uid = uint(uidInterface.(float64))
}
}

return &ctx{
id: requestID,
verr: errors.NewValidation(),
getDB: getDB,
userID: userID,
id: requestID,
verr: errors.NewValidation(),
getDB: getDB,
uid: uid,
firebaseUID: firebaseUID,
}
}

Expand All @@ -45,9 +67,13 @@ func (c ctx) RequestID() string {
}

func (c ctx) Authenticated() bool {
return c.userID != 0
return c.firebaseUID != ""
}

func (c ctx) UID() uint {
return c.uid
}

func (c ctx) UserID() uint {
return c.userID
func (c ctx) FirebaseUID() string {
return c.firebaseUID
}
24 changes: 11 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,46 @@ module github.com/nimil-jp/gin-utils
go 1.17

require (
firebase.google.com/go/v4 v4.6.1
github.com/gin-contrib/cors v1.3.1
github.com/gin-contrib/sessions v0.0.4
github.com/gin-gonic/gin v1.7.4
github.com/go-playground/assert/v2 v2.0.1
github.com/go-playground/locales v0.14.0
github.com/go-playground/universal-translator v0.18.0
github.com/go-playground/validator/v10 v10.9.0
github.com/google/uuid v1.3.0
github.com/iancoleman/strcase v0.2.0
github.com/ken109/gin-jwt v1.1.4
github.com/pkg/errors v0.9.1
go.uber.org/zap v1.19.1
gorm.io/gorm v1.22.1
)

require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
cloud.google.com/go v0.75.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/goccy/go-json v0.7.10 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/securecookie v1.1.1 // indirect
github.com/gorilla/sessions v1.2.0 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lestrrat-go/backoff/v2 v2.0.8 // indirect
github.com/lestrrat-go/blackmagic v1.0.0 // indirect
github.com/lestrrat-go/httpcc v1.0.0 // indirect
github.com/lestrrat-go/iter v1.0.1 // indirect
github.com/lestrrat-go/jwx v1.2.9 // indirect
github.com/lestrrat-go/option v1.0.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect
go.opencensus.io v0.22.5 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99 // indirect
golang.org/x/sys v0.0.0-20211107104306-e0b2ad06fe42 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/api v0.40.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210222152913-aa3ee6e6a81c // indirect
google.golang.org/grpc v1.35.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Expand Down
Loading

0 comments on commit 2ea66d1

Please sign in to comment.