Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pxyup/gin-auth-middleware

Auth middleware for gin
https://github.com/pxyup/gin-auth-middleware

gin gin-gonic gin-middleware golang

Last synced: 21 days ago
JSON representation

Auth middleware for gin

Awesome Lists containing this project

README

        

Auth middleware for gin

[![codecov](https://codecov.io/gh/PxyUp/gin-auth-middleware/branch/master/graph/badge.svg)](https://codecov.io/gh/PxyUp/gin-auth-middleware)

# Use Auth middleware

```bash
go get github.com/PxyUp/gin-auth-middleware
```

```go
type UserFn func([]byte) (interface{}, error)

type User struct {
Name string `json:"name"`
}

var (
userFn = func([]byte) (interface{}, error) {
user := &User{}
err := json.Unmarshal(body, user)
if err != nil {
return nil, err
}
return user, nil
}

mw = &gin_auth_middleware.MiddleWare{
Host: "http://localhost:8080"
Path: "/auth/me"
Method: http.MethodGet
StatusCode: http.StatusOk
UserFn: userFn
Headers map[string]string{}
ProxyHeaders []string{"Authorization"}
}
)

func CreateEngine() *gin.Engine {
r := gin.New()
r.Use(mw.Auth())
return r
}

```

# Use GetUserFromContext
When user already authenticated you can user like that:

```go
func (c *gin.Context) {
userD,err := gin_auth_middleware.GetUserFromContext(c)
if err != nil {
...
}

user := userD.(User) // cast type from user function
}
```