Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/pxyup/gin-auth-middleware
- Owner: PxyUp
- License: mit
- Created: 2019-07-23T17:04:01.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-02-09T20:42:54.000Z (almost 2 years ago)
- Last Synced: 2024-06-20T11:47:58.725Z (5 months ago)
- Topics: gin, gin-gonic, gin-middleware, golang
- Language: Go
- Size: 9.77 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
}
```