37 lines
755 B
Go
37 lines
755 B
Go
package middleware
|
|
|
|
import (
|
|
"nixcn-cms/internal/authtoken"
|
|
"nixcn-cms/internal/exception"
|
|
"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(c, auth)
|
|
if err != nil {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.MiddlewareServiceJwt).
|
|
SetEndpoint(exception.EndpointMiddlewareService).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorUnauthorized).
|
|
SetError(err).
|
|
Throw(c).
|
|
String()
|
|
|
|
utils.HttpAbort(c, 401, errorCode)
|
|
return
|
|
}
|
|
|
|
c.Set("user_id", uid)
|
|
c.Next()
|
|
}
|
|
}
|