mirror of
https://github.com/coaidev/coai.git
synced 2025-05-20 05:20:15 +09:00
27 lines
460 B
Go
27 lines
460 B
Go
package auth
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"strings"
|
|
)
|
|
|
|
func Middleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
token := strings.TrimSpace(c.GetHeader("Authorization"))
|
|
if token != "" {
|
|
if user := ParseToken(c, token); user != nil {
|
|
c.Set("token", token)
|
|
c.Set("auth", true)
|
|
c.Set("user", user.Username)
|
|
c.Next()
|
|
return
|
|
}
|
|
}
|
|
|
|
c.Set("token", token)
|
|
c.Set("auth", false)
|
|
c.Set("user", "")
|
|
c.Next()
|
|
}
|
|
}
|