35 lines
712 B
Go
35 lines
712 B
Go
package middleware
|
|
|
|
import (
|
|
"nixcn-cms/exception"
|
|
"nixcn-cms/pkgs/authtoken"
|
|
"nixcn-cms/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func JWTAuth() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
auth := c.GetHeader("Authorization")
|
|
|
|
authtoken := new(authtoken.Token)
|
|
uid, err := authtoken.HeaderVerify(auth)
|
|
if err != nil {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusServer).
|
|
SetService(exception.MiddlewareJwtService).
|
|
SetEndpoint(exception.MiddlewareEndpoint).
|
|
SetType(exception.ErrorTypeSpecific).
|
|
SetOriginal(exception.CommonErrorUnauthorized).
|
|
Build()
|
|
|
|
utils.HttpAbort(c, 401, errorCode)
|
|
return
|
|
}
|
|
|
|
c.Set("user_id", uid)
|
|
c.Next()
|
|
}
|
|
}
|